$(document).ready(function() {
	$('.orders li').click(function(e){
		if ($(this).children('.order_details').css('display')=='none') {
			$(this).children('.order_details').show();
		} else {
			$(this).children('.order_details').hide();
		}		
	});
	$('.button_resetpassword_go').click(function(e){
		var rp_password = $('#rp_password').val();
		var rp_passwordag = $('#rp_passwordag').val();
		if (rp_password!=rp_passwordag) {
			alert('Password do not match');
		} else {
			if (rp_password.length<6) {
				alert('Password must contain at least 6 chars');
			} else {
				var salt = $('#salt').val();
				$.post('include/libs/ajax/spupdate_account.php',
				{
					method: 'password',
					salt: salt,
					password: rp_password
				}, function(e){
					if (e=='true') {
						alert('Password updated.');
						window.location='myaccount.php';
					} else {
						alert('Update failed.');
					}					
				});
			}
		}
	});
	$('.graysubmitbutton').click(function(e){
		var firstname = $('#firstname').val();
		var surname = $('#surname').val();
		var emailaddress = $('#emailaddress').val();
		var telephone = $('#telephone').val();
		var notes = $('#notes').val();
		if ((firstname=='')||(surname=='')||(emailaddress=='')) {
			alert('Please fill in all mandatory fields');
		} else {
			$.post('include/libs/ajax/sendmail.php',
			{
				firstname: firstname,
				surname: surname,
				emailaddress: emailaddress,
				telephone: telephone,
				notes: notes
			},
			function(e){
				$('#firstname').val('');
				$('#surname').val('');
				$('#emailaddress').val('');
				$('#telephone').val('');
				$('#notes').val('');
				$('.frm').slideToggle('slow',function(e){
					$('.thanks').slideToggle('slow');
				});
			});
		}
	});
	$('.apply_promotioncode').click(function(e){
		var promotionalcode = $('#promotioncode').val();
		$.post('/include/libs/ajax/splist_promotioncode.php',{
				promotionalcode: promotionalcode
			},function(e){
				if (e=='0') {
					alert('Promotional code is not valid');
				} else {
					window.location='cart.php';
				}
			});
	});
	$('.cancel_promotioncode').click(function(e){
		var promotionalcode = $('#promotioncode').val();
		$.post('/include/libs/ajax/splist_promotioncode.php?cmd=cancel',{
			},function(e){
				window.location='cart.php';
			});
	});
	$('.button_resetpassword').click(function(e){
		var resetea = $('#log_forgotpassword').val();
		if (emailvalidation(resetea)) {
			$.post('/include/libs/ajax/spreset_password.php',{
				emailaddress: resetea
			},function(e){
				if (e=='true') {
					alert('Email sent successfully');
					$('#log_forgotpassword').val('Enter your email address here...');
				} else {
					alert('Account does not exist');
				}
			});
		} else {
			alert('Please enter a valid email address');
		}
	});
	$('.checkwithpaypal').click(function(e){
		$("#shadow").css("height", $(document).height());
		$('.msg').html('<div class="msg"><div class="in"><table><tr><td width="31">&nbsp;</td><td style="padding-top: 70px;"><div class="title">+ The System is loading PayPal data</div><div class="sep"></div><div class="pleasewait">You will be redirected once the process is done</div></td><td><img src="images/msgbird.png" /></td></tr></table></div></div>');
		$.post('/include/libs/ajax/paypal_polltokenid.php',{
		},function(e){
			if (e=='false') {
				$('.pleasewait').html('<font style="color: red;">An error occured, please <a href="#" class="removepopup" style="color: red;">Close</a> window and try again.</font>');
				$('.removepopup').click(function(e){
					$('.msg').html('');
					$('#shadow').hide();					
				});
			} else {
				window.location=e;
			}
		});
	});
	$('.sfc').click(function(e){
		$("#shadow").css("height", $(document).height());
		var sfc = '<div class="sfc"><div align="right"><a href="#" class="closesfc"><img src="images/closebutton.png" border="0" /></a></div><table style="padding-top: 25px;"><tr><td width="24">&nbsp;</td><td><h1>Size Chart</h1>Before you slip into a pair of our soft, sensual yoga pants or wrap yourself in one of our smart yet snug-fitting jackets, make sure you know the exact sizes of your body.<br /><br />Take measurements of your bust, waist, hip and inside leg then use our size guide below to help you determine the right size for you.<br /><br /><h2>Pants</h2><div class="sep"></div>';
		sfc+='<table cellpadding="0" cellspacing="0" width="338" class="sfctbl">';
		sfc+='<tr class="t"><td>&nbsp;</td><td colspan="2">Tight</td><td colspan="2">Loose</td></tr>';
		sfc+='<tr class="tsep"><td></td><td colspan="2"><div class="smallsep"></div></td><td colspan="2"><div class="smallsep"></div></td></tr>';
		sfc+='<tr class="t"><td>&nbsp;</td><td>Waist</td><td>Hip</td><td>Waist</td><td>Hip</td></tr>';
		sfc+='<tr class="odd"><td><strong>S</strong> (Eu = 36)</td><td>33-35</td><td>48-50</td><td>33-35</td><td>53-55</td></tr>';
		sfc+='<tr><td><strong>M</strong> (Eu = 38)</td><td>36-38</td><td>51-53</td><td>36-38</td><td>56-58</td></tr>';
		sfc+='<tr class="odd"><td><strong>L</strong> (Eu = 40)</td><td>39-40</td><td>54-56</td><td>39-40</td><td>59-61</td></tr>';
		sfc+='<tr><td><strong>XL</strong> (Eu = 42)</td><td>41-42</td><td>56-58</td><td>41-42</td><td>62-64</td></tr>';
		sfc+='</table>';
		sfc+='<br /><h2>Tanks & Shirts</h2><div class="sep"></div>';
		sfc+='<table cellpadding="0" cellspacing="0" width="338" class="sfctbl2">';
		sfc+='<tr class="t"><td>&nbsp;</td><td>Fitted</td><td>Standard</td></tr>';
		sfc+='<tr class="tsep"><td></td><td><div class="smallsep"></div></td><td><div class="smallsep"></div></td></tr>';
		sfc+='<tr class="t"><td>&nbsp;</td><td>Bust</td><td>Bust</td></tr>';
		sfc+='<tr class="odd"><td><strong>S</strong> (Eu = 36)</td><td>36-38</td><td>38-40</td></tr>';
		sfc+='<tr><td><strong>M</strong> (Eu = 38)</td><td>38-40</td><td>40-44</td></tr>';
		sfc+='<tr class="odd"><td><strong>L</strong> (Eu = 40)</td><td>40-42</td><td>44-46</td></tr>';
		sfc+='<tr><td><strong>XL</strong> (Eu = 42)</td><td>42-44</td><td>46-48</td></tr>';
		sfc+='</table>';
		sfc+='</td><td width="260"><img height="350" hspace="55" src="images/sfc.png" /><br /><ul style="font-size:11px;padding:0;padding-right: 15px;margin:0;"><li style="padding-bottom: 5px;"><strong>Bust</strong>: Place your tape measure across the fullest part of your bust and across your shoulder blades</li><li style="padding-bottom: 5px;"><strong>Waist</strong>: Measure around your natural waistline (between your bottom ribs and hip bones). To do this, breathe in, relax your stomach and lay the tape measure flat around your waist. Allow ¼ inch of slack for a more accurate measurement. </li><li style="padding-bottom: 5px;"><strong>Hips</strong>: Measure the widest part of your hips and place the tape measure around it. </li><li><Strong>Inside leg</strong>: Stand with your feet shoulder-width apart, wearing running shoes. The measurement needs to be taken from the top of your inside leg – from the crutch down to the inner ankle bone.</li></ul><br /><br /></td></tr></table></div>';
		$('.msg').html(sfc);
		$('.closesfc').click(function(e){
			$('.msg').html('');
			$("#shadow").css("height", '0px');
		});
	});
    $('.gbp').click(function(e){
		$("#shadow").css("height", $(document).height());
		$('.msg').html('<div class="msg"><div class="in"><table><tr><td width="31">&nbsp;</td><td style="padding-top: 70px;"><div class="title">+ The System is loading GBP data</div><div class="sep"></div>Please be patient</td><td><img src="images/msgbird.png" /></td></tr></table></div></div>');
		$('.eurprice').css('display','none');
		$('.gbpprice').css('display','block');
		$('.eur').addClass('unsel');
		$('.gbp').removeClass('unsel');
		$.post('/include/libs/ajax/setcurrency.php',{
			data: 'gbp'
		},function(e){
			location.reload(true);
		});
	});
	$('.eur').click(function(e){
		$("#shadow").css("height", $(document).height());
		$('.msg').html('<div class="msg"><div class="in"><table><tr><td width="31">&nbsp;</td><td style="padding-top: 70px;"><div class="title">+ The System is loading EUR data</div><div class="sep"></div>Please be patient</td><td><img src="images/msgbird.png" /></td></tr></table></div></div>');
		$('.eurprice').css('display','block');
		$('.gbpprice').css('display','none');
		$('.gbp').addClass('unsel');
		$('.eur').removeClass('unsel');
		$.post('/include/libs/ajax/setcurrency.php',{
			data: 'eur'
		},function(e){			
			location.reload(true);
		});
	});
});
function emailvalidation(emailaddress) {
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if(!emailReg.test(emailaddress)) {
       return false;
    } else {
	   return true;
	}
}
function createaddress(aid) {
	if (aid==0) {
		$(".currentaddresses").slideToggle('slow',function(){			
		});
		$(".addressvalue").slideToggle('slow',function(){
				$("#addressid").val(0);
		});		
	} else {
		$.ajax({
			type: "GET",
			url: "include/libs/ajax/splist_address.php",
			data: "aid="+aid,
			dataType: "json",
			contentType: "application/json; charset=utf-8",
			error: function(data) {
				/*
				alert(data.responseText);
				alert('error');
				*/
			},
			success: function(data) {
				$.each(data, function(i, object) {
				  $.each(this, function(k, v) {
					if (k=='id') { $("#addressid").val(v); }
					if (k=='firstname') { $("#firstname").val(v); }
					if (k=='surname') {	$("#surname").val(v); }
					if (k=='address') {	$("#address").val(v); }
					if (k=='city') {	$("#city").val(v); }
					if (k=='zipcode') {	$("#zipcode").val(v); }
					if (k=='phone') {	$("#phone").val(v); }
					if (k=='additionalphone') {	$("#additionalphone").val(v); }
					if (k=='ccode') {	$("#ccode").val(v); }
				  });
				});
				$(".currentaddresses").slideToggle('slow',function(){
					$(".addressvalue").slideToggle('slow',function(){
					});		
				});
			}	 
		});		
	}	
}
function isnumeric(input)
{
   return (input - 0) == input && input.length > 0;
}
function myaccount_cancel() {
	$(".addressvalue").slideToggle('slow',function(){
	  $(".currentaddresses").slideToggle('slow',function(){
	  });		
	});
}
function myaccount_remove() {
	$.post('include/libs/ajax/spupdate_addressbook.php',
	{
		cmd: 'removeaddress',
		id: $("#addressid").val()
	},
	function(e){
		alert('Address removed successfully');
		window.location='myaccount.php?p=addressbook';
	});
}
function myaccount_saveaddress() {
	var firstname = $("#firstname").val();
	var surname = $("#surname").val();
	var address = $("#address").val();
	var city = $("#city").val();
	var zipcode = $("#zipcode").val();
	var phone = $("#phone").val();
	var additionalphone = $("#additionalphone").val();
	var ccode = $("#ccode").val();
	var addressid= $("#addressid").val();
	if ((firstname=='')||(surname=='')||(address=='')||(city=='')||(zipcode=='')) {
		alert('Please fill all mandatory fields');
	} else {
		$.ajax({
			type: "POST",
			url: "include/libs/ajax/spupdate_addressbook.php",
			data: "addressid="+addressid+"&firstname="+firstname+"&surname="+surname+"&address="+address+"&city="+city+"&zipcode="+zipcode+"&phone="+phone+"&additionalphone="+additionalphone+"&ccode="+ccode,
			dataType: "text",
			error: function(data) {
				/*
				alert(data.responseText);
				alert('error');
				*/
			},
			success: function(data) {
				$(".status").html('Account updated.');
			}	 
		});
	}
}
function myaccount_processsave() {
	var firstname = $("#firstname").val();
	var surname = $("#surname").val();
	var address = $("#address").val();
	var city = $("#city").val();
	var zipcode = $("#zipcode").val();
	var phone = $("#phone").val();
	var additionalphone = $("#additionalphone").val();
	var ccode = $("#ccode").val();
	var isnewsletter = $("#isnewsletter").attr('checked');
	$.ajax({
		type: "POST",
		url: "include/libs/ajax/spupdate_account.php",
		data: "firstname="+firstname+"&surname="+surname+"&address="+address+"&city="+city+"&zipcode="+zipcode+"&phone="+phone+"&additionalphone="+additionalphone+"&isnewsletter="+isnewsletter+"&ccode="+ccode,
		dataType: "text",
		error: function(data) {
			/*
			alert(data.responseText);
			alert('error');
			*/
		},
		success: function(data) {
			$(".status").html('Account updated.');
		}	 
	});
}
function processlogin(back_url) {
	var log_emailaddress = $("#log_emailaddress").val();
	var log_password = $("#log_password").val();
	if ((log_password=='')||(log_emailaddress=='')) {
	} else {
		$.ajax({
			type: "POST",
			url: "include/libs/ajax/splist_account.php",
			data: "e="+log_emailaddress+"&p="+log_password,
			dataType: "text",
			error: function(data) {
				/*
				alert(data.responseText);
				alert('error');
				*/
			},
			success: function(data) {
				if (data=='correct') {
					window.location=back_url;
				} else {
					if (data=='incorrect') {
						alert('Incorrect password');
					} else {
						alert('Account does not exist');
					}
				};
			}	 
		});
	}
}
function setaccountregistrationvalidation() {
	var firstname = $("#firstname").val();
	var surname = $("#surname").val();
	var emailaddress = $("#emailaddress").val();
	var password = $("#password").val();
	/*
	var address = $("#address").val();
	var city = $("#city").val();
	var zipcode = $("#zipcode").val();
	var phone = $("#phone").val();
	*/
	var ccode = $("#ccode").val();
	if ((firstname=='') ||
	   (surname=='') ||
	   (emailaddress=='') ||
	   (password=='') ||
	   (ccode==0)) {
		   alert('Please fill in all required fields');
	} else {
		$("#anjregistrationprocess").submit();
	}
}
function clearfieldcontent(fieldcontent,fieldid) {
	if ($("#"+fieldid).val()==fieldcontent) {
		$("#"+fieldid).val('');
	}
}
function updateitemdetails(countposition) {
	var q = $("#q"+countposition).val();
	var p = $("#p"+countposition).val();
	if (q<1) {
		alert('Please enter a valid quantity');
	} else {
		$.post('include/libs/ajax/setcartsession.php',
			{
				cmd: 'update',
				catid: 'none',
				p: p,
				countposition: countposition,
				q: q
			},
			function(e) {
				if (e==0) {
					alert('Product has limited quantity, please try less items');
					window.location='cart.php';
				} else {
					window.location='cart.php';
				}
			});
	}
}
function removefromcart(countposition) {
	$.ajax({
        type: "POST",
        url: "include/libs/ajax/setcartsession.php",
        data: "cmd=remove&catid=none&countposition="+countposition,
        dataType: "json",
        error: function(data) {
        	//alert(data.responseText);
        },
        success: function(data) {
 			window.location='cart.php';		             
        }	 
    });
}
function addtocart(catid) {
	var qty = $("#quantity").val();
	var productid = $("#productid").val();
	if (!isnumeric(qty)) {		
		qty=1;
	}
	var productcolour = $("#productcolor").val();
	if (productcolour==0) {
		document.getElementById('choosesizecolor').style.display='';
		document.getElementById('addtocartbutton').onmouseout=function() {
			document.getElementById('choosesizecolor').style.display='none';
		}
	} else {
		var productsize = $("#productsize").val();
		if (productsize==0) {
			document.getElementById('choosesizecolor').style.display='';
			document.getElementById('addtocartbutton').onmouseout=function() {
				document.getElementById('choosesizecolor').style.display='none';
			}
		} else {
			$.post('include/libs/ajax/setcartsession.php',
				{
					cmd: "add",
					productid: productid,
					size: productsize,
					colour: productcolour,
					qty: qty,
					catid: catid
				}, function(data){
					if (data=='missing') {
						alert('Product has limited quantity, please try less items');
					} else {
						window.location='cart.php';
					}
				});
		}
	}
}
function setmainimg(cp,imgfilename,dir) {
	$(".jqZoomWindow").remove();
	$(".jqZoomPup").remove();
	$(".jqzoom").remove();
	$('.main_img').html('<a rel="prettyPhoto" id="mainimghref" href="/images/'+dir+'/huge_'+escape(imgfilename)+'" class="jqzoom"><img id="mainimg" src="/images/'+dir+'/'+escape(imgfilename)+'" width="280" height="420" alt="" border="0" /></a>');
/*
	document.getElementById('mainimghref').href='images/'+dir+'/huge_'+imgfilename;
	document.getElementById('mainimg').src='images/'+dir+'/'+imgfilename;
*/
	for (x=1;x<10;x++) {
		if (document.getElementById('ps'+x)!=null) {
			document.getElementById('ps'+x).className='';
		}
	}
	cp.className='selected';
	$(".jqzoom").jqzoom();
}
