// JavaScript Document
$(document).ready(
	function() {
		//
		setupNav();
		init();
		//
		if($('#bannerHome').length > 0){
			$('#bannerHome').cycle();
		}
		//
		if($('#popup').length > 0){
			$("#popup").dialog({
				autoOpen: false,
				width:400,
				modal: true,
				resizable: false
			})		
		}
		//
		if($('.quantitySelector').length > 0){
			$('.quantitySelector').bind("keyup",calculateQ);
		}
		///
		Cufon.replace('h1'); 
		Cufon.replace('h2'); 
		Cufon.replace('h3'); 
		Cufon.replace('.prodheadLeft'); 
		Cufon.replace('.contentHeader .left'); 
		//
		$('.btn').corner("4px");
		$('.submitBTN').corner("4px");
		$('.main .clientNav a').corner("4px");
		//
		$('.BTNaddSelectedProduct').bind("click",addSelectedProduct);
		//
		$('.BTNsubmitWishList').bind("click", submitWishList);
		//
		Shadowbox.init();
		//
		if($('#clientDetailsForm').length > 0){
			v = jQuery('#clientDetailsForm').validate({
				submitHandler: function(form) {
					jQuery(form).ajaxSubmit({
						success: function() {clientDetails_callback(); }
					});
				}
			});
		}
		//
		if($('#wlForm').length > 0){
			v = jQuery("#wlForm").validate({
				submitHandler: function(form) {
					jQuery(form).ajaxSubmit({
						target: "#wlFormResponse" 
					});
				}
			});
		}
		//
		if($('#updateWish').length > 0){
			//
			v = jQuery("#updateWish").validate({
				submitHandler: function(form) {
					jQuery(form).ajaxSubmit({
						success: function() {reloadWishList(); }
					});
				}
			});
			//
		}
		///
		$('.BTNsearch').bind("click",searchProducts);
		$('.BTNdel').bind("click",delWishlist);
		$('.BTNdownloadXLS').bind("click",downloadXLS);
		$('.BTNdownloadPDF').bind("click",downloadPDF);
		$('.BTNcopyToNew').bind("click",copyWishlist);
		$('.BTNresume').bind("click",resumeWishlist);
		$('.BTNview').bind("click",viewWishlist);
		
		
		$('#changePassword').bind("click",showChangePassword);
	}	
);
//
function showChangePassword(){
	//
	var html = '<form action="scripts/ajax_calls/ajax_update_password.php" method="post" name="phooForm" id="changePasswordForm">';
	html += '<div class="tableRow" style="width:380px;overflow:hidden;">';
	html += '<div class="tableLabel">';
	html += '<label for="old_pass">Old password:</label>';
	html += ' </div>';
	html += '  <div class="tableInput" style="width:180px;overflow:hidden;">';
	html += '   <input id="old_pass" name="old_pass" type="password" class="required">';
	html += '  </div>';
	html += ' </div>';
	html += '  <div class="tableRow" style="width:380px;overflow:hidden;">';
	html += ' <div class="tableLabel">';
	html += '     <label for="new_pass">New password:</label>';
	html += ' </div>';
	html += '   <div class="tableInput" style="width:180px;overflow:hidden;">';
	html += '       <input id="new_pass" name="new_pass" type="password" class="required">';
	html += '   </div>';
	html += '   <div id="result" class="tableInput" style="width:80px;overflow:hidden;">';
	html += ' </div>';
	html += '	<div class="tableRow" style="width:380px;overflow:hidden;">';
	html += ' 	<button id="changePasswordBTN" class="BTNgreen btn">Change password ></button>';
	html += ' </div>';
	html += ' </form>';
	//
	$('#popup').dialog( "option", "title", "Change Password");
	$('#popup').html(html);
	$('#popup').dialog('open');
	//
	cp = jQuery('#changePasswordForm').validate({
				submitHandler: function(form) {
					jQuery(form).ajaxSubmit({
						success: function(data) {changePassword_callback(data); }
					});
				}
			});
}
function changePassword_callback(data){
	$('#popup').html(data);
}
//
function clientDetails_callback(){
	$('#popup').dialog( "option", "title", "Your Details");
	$('#popup').html("Your details have been updated.");
	$('#popup').dialog('open');
	closeWindow();
}
//
function searchProducts(e){
	if ($('#sKeyword').val() != "" || $('#sType').val() != "" || $('#sAge').val() != "" || $('#sGender').val() != ""){
		window.location = "/Product-Search_"+$('#sKeyword').val()+"_"+$('#sType').val()+"_"+$('#sAge').val()+"_"+$('#sGender').val();
	} else {
		$('#popup').dialog( "option", "title", "Product search");
		$('#popup').html("Please enter a search term or select a type.");
		$('#popup').dialog('open');
		closeWindow();
	}
}
//
function downloadXLS(e){
	var wish_id = getID($(this).attr("id"));
	window.location = "/get_xls.php?wish_id="+wish_id;
}
//
function downloadPDF(e){
	var wish_id = getID($(this).attr("id"));
	window.location = "/get_pdf.php?wish_id="+wish_id;
}
//
function delWishlist(e){
	var wish_id = getID($(this).attr("id"));
	$.post("scripts/ajax_calls/ajax_tmc_functions.php", {wish_id:wish_id, ajax_function:'deleteWishlist'}, function(data){reloadWishList();}, "json");
}
//
function submitWishList(e){
	//
	//alert($(".orderType:checked").val());
	$.post("scripts/ajax_calls/ajax_tmc_functions.php", {wish_type:$(".orderType:checked").val(), wish_notes:$("#wish_notes").val(), ajax_function:'submitWishlist'}, function(data){submitWishList_callback(data);}, "json");
}
function submitWishList_callback(data){
	$('#WishlistHolder').html(data[0].response);
}
//
function viewWishlist(e){
	var wish_id = getID($(this).attr("id"));
	window.location = "/View-Wish-List_"+wish_id;
}
//
function resumeWishlist(e){
	var wish_id = getID($(this).attr("id"));
	$.post("scripts/ajax_calls/ajax_tmc_functions.php", {wish_id:wish_id, ajax_function:'resumeWishlist'}, function(data){resumeWishlist_callback(data);}, "json");
}
function resumeWishlist_callback(data){
	var op='<p>This Wish List has been resumed click below to view it.</p>';
	op+='<a href="/View-Wish-List" class="BTNblue btn">View Wish List ></a>';
	//
	$('#popup').dialog( "option", "title", "Wish List copied");
	$('#popup').html(op);
	$('#popup').dialog('open');	
}
//
function copyWishlist(e){
	var wish_id = getID($(this).attr("id"));
	$.post("scripts/ajax_calls/ajax_tmc_functions.php", {wish_id:wish_id, ajax_function:'copyWishlist'}, function(data){copyWishlist_callback(data);}, "json");
}
function copyWishlist_callback(data){
	//window.location = "/View-Wish-List";
	//
	var op='<p>A new Wish List has been created click below to view it.</p>';
	op+='<a href="/View-Wish-List" class="BTNblue btn">View new Wish List ></a>';
	//
	$('#popup').dialog( "option", "title", "Wish List copied");
	$('#popup').html(op);
	$('#popup').dialog('open');	
	//closeWindow();
}
//
function reloadWishList(){
	window.location.reload();
	//$.post("scripts/ajax_calls/ajax_tmc_functions.php", {ajax_function:'getWishlist'}, function(data){getWishlist_callback(data);}, "html");
}
function getWishlist_callback(data){
	//window.location.reload();
}
//
function addSelectedProduct(e){
	var P_ID = getID($(this).parents('tr').attr("id"));
	var q = parseInt($(this).parents('tr').find('.quantitySelector').val());
	var tq = parseInt($(this).parents('tr').find('.totalQ').val());
	//
	if (q > 0){
		$.post("scripts/ajax_calls/ajax_tmc_functions.php", {P_ID:P_ID, icq:q, ajax_function:'addSelectedProduct'}, function(data){addSelectedProduct_callback(data);}, "json");
	} else {
		$('#popup').dialog( "option", "title", "Error");
		$('#popup').html("Please enter a quantity");
		$('#popup').dialog('open');		
	}
}
function addSelectedProduct_callback(data){
	$('#popup').dialog( "option", "title", "Wish List");
	$('#popup').html(urldecode(data[0].response));
	$('#popup').dialog('open');	
	closeWindow();
}
///
function calculateQ(e){
	var q = parseInt($(this).parents('tr').find('.innerQ').val()) * parseInt($(this).val());
	//
	isNaN(q)?q='':q=q;
	//
	$(this).parents('tr').find('.totalQ').val(q);
	$(this).parents('tr').find('.productSelector').attr('checked', true);
}
//
