// JavaScript Document
function cartAdd(eveid, albid, prid, me){
	qsarg = "use=add&eveid="+eveid+"&albid="+albid+"&prid="+prid+"&me="+me;
	$.get('/includes/functions/cart_ajax.php', qsarg, 
		  function(data){
			  ret = data.split("/");
			  if(ret[0].match(/\bdone\b/)){
				  $("#cart_count").html(ret[1]);
				  tot_price = parseFloat(ret[2]);
				  tot_price = tot_price.toFixed(2);
				  $("#cart_price").html("&pound;"+tot_price);
				  plurl = "/template/pricelist.php?img=" + $("#scrollto").val();
				  $("#priceside").load(plurl);
				  if($("#cartbox").hasClass("hide")){
					  $("#cartbox").removeClass("hide");
					  $("#breadcrumb").width(955-$("#cartbox").width());
				  }
				  if($("#cart_tots").hasClass("hide")){
					  $("#cart_tots").removeClass("hide");
					  $("#cdall_hint").addClass("hide");
				  }
				  $("#cartbox").addClass("hlgt", 500);
				  $("#cartbox").effect("pulsate", { times:3 }, 1200, function(){
																		$("#cartbox").removeClass("hlgt", 500);
																	 });
				  if(ret[4] != "false"){
					  GB_show('PicMan.co.uk :: Cart','/cart/restwarn.php?'+ret[4],550,925);
				  }
			  } else if(ret[0] == "checkme") {
				  GB_show('PicMan.co.uk :: Cart','/cart/restwarn.php?'+ret[1],470,600);
			  } else {
				//alert (data);
			  }
		  }
	);
}

function popCartAdd(eveid, albid, prid, me){
	qsarg = "use=add&eveid="+eveid+"&albid="+albid+"&prid="+prid+"&me="+me;
	$.get('/includes/functions/cart_ajax.php', qsarg, 
		  function(data){
			  ret = data.split("/");
			  if(ret[0].match(/\bdone\b/)){
				  parent.location.reload();
			  } else {
				  //alert (data);
			  }
		  }
	);
}

function updateQty(id){
	qty = $('#Q_'+id).val();
	qsarg = "use=qty&qty="+qty+"&id="+id;
	$.get('/includes/functions/cart_ajax.php', qsarg, 
		  function(data){
			  if(data == "done"){
				  window.location.reload();
			  } else {
				alert (data);
			  }
		  }
	);	
}

function deleteItem(id){
	qsarg = "use=remove&id="+id;
	$.get('/includes/functions/cart_ajax.php', qsarg, 
		  function(data){
			  if(data.match(/\bdone\b/)){
				  window.location.reload();
			  } else {
				alert (data);
			  }
		  }
	);	
}

function changeAddress(type, id){
	if(confirm("Would you like to make this the default "+type+" address for your account?")){
		def="yes";
	} else {
		def="no";
	}
	qsarg = "use=changeaddress&type="+type+"&id="+id+"&default="+def;
	$.get('/includes/functions/cart_ajax.php', qsarg, 
		  function(data){
			  if(data == "done"){
				  parent.location.reload();
			  } else {
				alert (data);
			  }
		  }
	);	
}

function checkJsval(){
	if($("#jsval").val() == "false"){
		if(btnClicked == 'cancel'){
			btnClicked = '';
			return true;
		} else {
			return false;
		}
	} else {
		return true;
	}
}

function valTermsAgreed(){
	if ($('#agree').is(':checked')) {
		return true;
	} else {
		alert("You must read and accept our terms and conditions before your order can be placed");
		return false;
	}
}

function valUserAndAddressForm(type){
	tit = $("#title").val();
	fnm = $("#firstname").val();
	lnm = $("#lastname").val();
	tel = $("#phone").val();
	add = $("#address1").val();
	twn = $("#town").val();
	cty = $("#county").val();
	pcd = $("#postcode").val();
	ctr = $("#country").val();
	if(type == "acc"){
		ema = $("#email").val();
		pw1 = $("#pass1").val();
		pw2 = $("#pass2").val();
	}
	valpass = true;
	
	if(tit == "Please Select..."){
		valpass = false;
		$("#title_val").html("required");
	} else {
		$("#title_val").html("");
	}	
	if(fnm == ""){
		valpass = false;
		$("#firstname_val").html("required");
	} else {
		if (!fnm.match(/^[-\.,' \w]+$/i)){
			valpass = false;
			$("#firstname_val").html("invalid entry");
		} else {
			$("#firstname_val").html("");
		}
	}
	if(lnm == ""){
		valpass = false;
		$("#lastname_val").html("required");
	} else {
		if (!lnm.match(/^[-\.,' \w]+$/i)){
			valpass = false;
			$("#lastname_val").html("invalid entry");
		} else {
			$("#lastname_val").html("");
		}
	}
	pnr = new RegExp(/(^\(?0[0-9]{2}\)?[ ]?[0-9]{2}[ ]?(([0-9]{2}[ ]?[0-9]{4})|([0-9]{3}[ ]?[0-9]{3}))\b$)|(^\(?0[0-9]{4}\)?[ ]?[0-9]{3}[ ]?[0-9]{3}\b$)|(^\(?0[0-9]{3}\)?[ ]?([0-9]{3}[ ]?[0-9]{4}|[0-9]{4}[ ]?[0-9]{3})\b$)/)
	if(tel != ""){
		if(ctr == "GB"){
			if (!tel.match(pnr)){
				valpass = false;
				$("#phone_val").html("invalid entry, please check &quot;Country&quot; is correct");
			} else {
				$("#phone_val").html("");
			}
		} else {
			if (!tel.match(/[-\+ \(\)\d]+/)){
				valpass = false;
				$("#phone_val").html("invalid entry");
			} else {
				$("#phone_val").html("");
			}
		}
	} else {
		$("#phone_val").html("");
	}
	if(add != ""){
		if(!add.match(/^[-\.,' \w\d]+$/)){
			valpass = false;
			$("#address1_val").html("invalid entry");
		} else {
			$("#address1_val").html("");
		}
	} else {
		valpass = false;
		$("#address1_val").html("required");
	}
	if(twn != ""){
		if(!twn.match(/^[-\.,' \w\d]+$/)){
			valpass = false;
			$("#town_val").html("invalid entry");
		} else {
			$("#town_val").html("");
		}
	} else {
		valpass = false;
		$("#town_val").html("required");
	}
	if(cty != ""){
		if(ctr == "GB"){
			if(!cty.match(/^[-\.,' \w]+$/)){
				valpass = false;
				$("#county_val").html("invalid entry");
			} else {
				$("#county_val").html("");
			}
		} else {
			$("#county_val").html("");
		}
	} else {
		if(ctr == "GB"){
			valpass = false;
			$("#county_val").html("required");
		} else {
			$("#county_val").html("");
		}
	}
	if(pcd != ""){
		if(ctr == "GB"){
			stduk = new RegExp(/^((GIR( )?0AA)|(BFPO( )?[0-9]{1,4})|(AI(-| )?2640)|((ASCN|BBND|BIQQ|FIQQ|PCRN|SIQQ|STHL|TDCU|TKCA)( )?(1ZZ))|([A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKPS-UW])( )?[0-9][ABD-HJLNP-UW-Z]{2}))$/i);
			if(!pcd.match(stduk)){
				valpass = false;
				$("#postcode_val").html("invalid entry");
			} else {
				$("#postcode_val").html("");
			}
		} else {
			$("#postcode_val").html("");
		}
	} else {
		valpass = false;
		$("#postcode_val").html("required");
	}
	if(type == "acc"){
		if(ema == ""){
			valpass = false;
			$("#email_val").html("required");
		} else if(!ema.match(/^([0-9\w]([-.\w]*[0-9\w])*@([0-9\w][-\w]*[0-9\w]\.)+[\w]{2,4})$/)){
			valpass = false;
			$("#email_val").html("invalid entry");
		} else {
			$("#email_val").html("");
		}
		if(pw1 == ""){
			valpass = false;
			$("#pass1_val").html("required");
		} else {
			$("#pass1_val").html("");
		}
		if(pw2 == ""){
			valpass = false;
			$("#pass2_val").html("required");
		} else {
			if(pw1 != pw2){
				valpass = false;
				$("#pass2_val").html("passwords must match");
			} else {
				$("#pass2_val").html("");
			}
		}
	}
	if(valpass){
		$("#subbut").attr("disabled", "");
		$("#jsval").val("true");
	} else {
		$("#subbut").attr("disabled", "disabled");
		$("#jsval").val("false");
	}
}

function valPassChange(){
	opw = $("#oldpw").val();
	pw1 = $("#newpw1").val();
	pw2 = $("#newpw2").val();

	valpass = true;
	if(opw == ""){
		valpass = false;
		$("#oldpw_val").html("required");
	} else {
		$("#oldpw_val").html("");
	}	
	if(pw1 == ""){
		valpass = false;
		$("#newpw1_val").html("required");
	} else {
		$("#newpw1_val").html("");
	}
	if(pw2 == ""){
		valpass = false;
		$("#newpw2_val").html("required");
	} else {
		if(pw1 != pw2){
			valpass = false;
			$("#newpw2_val").html("passwords must match");
		} else {
			$("#newpw2_val").html("");
		}
	}
	if(valpass){
		$("#subbut").attr("disabled", "");
		$("#jsval").val("true");
	} else {
		$("#subbut").attr("disabled", "disabled");
		$("#jsval").val("false");
	}
}

function saveCart(){
	$('#btn').html('<img src="/includes/images/spinner.gif" width="16" height="16" />');
	qsarg = "use=savecart&ref="+$('#save_ref').val();
	$.get('/includes/functions/cart_ajax.php', qsarg, 
		  function(data){
			  if(data == "done"){
				  alert("Thank you, your current cart contents have been saved.\nTo retrieve this cart in the future just login to your account and visit the \"Saved Carts\" page");
				  window.location='/cart/index.php';
			  } else {
				alert (data);
			  }
		  }
	);
}

function openCart(cartid){
	if(confirm("This will replace any items currently in your cart with those in the saved cart.\n\nDo you wish to continue?")){
		qsarg = "use=opencart&id="+cartid;
		$.get('/includes/functions/cart_ajax.php', qsarg, 
			  function(data){
				  if(data == "done"){
					  window.location='/cart/index.php';
				  } else {
					alert (data);
				  }
			  }
		);
	}
}

function pwchange(){
	if($('#jsval').val() == "true"){
		opw=$('#oldpw').val();
		npw=$('#newpw1').val();
		$('#fback').html('<img src="/includes/images/spinner.gif" width="16" height="16" /> Please Wait');
		params="use=changepw&opw="+opw+"&npw="+npw;
		$.ajax({  
			url: '/includes/functions/cart_ajax.php',  
	        data: params,  
			dataType: "text",
			type: "GET",
			success: function(data){
				$('#fback').html(data);
			}
		});
	}	
}

function valEMailChange(){
	ema = $("#email").val();

	valpass = true;
	if(ema == ""){
		valpass = false;
		$("#email_val").html("required");
	} else if(!ema.match(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,4})$/)){
		valpass = false;
		$("#email_val").html("invalid entry");
	} else {
		$("#email_val").html("");
	}
	if(valpass){
		$("#subbut").attr("disabled", "");
		$("#jsval").val("true");
	} else {
		$("#subbut").attr("disabled", "disabled");
		$("#jsval").val("false");
	}
}

function changeMail(){
	if($('#jsval').val() == "true"){
		ema=$('#email').val();
		$('#fback').html('<img src="/includes/images/spinner.gif" width="16" height="16" /> Please Wait');
		params="use=changeem&add="+ema;
		$.ajax({  
			url: '/includes/functions/cart_ajax.php',  
	        data: params,  
			dataType: "text",
			type: "GET",
			success: function(data){
				$('#fback').html(data);
			}
		});
	}	
}

function forgotPW(){
	if($('#jsval').val() == "true"){
		ema=$('#email').val();
		$('#fback').html('<img src="/includes/images/spinner.gif" width="16" height="16" /> Please Wait');
		params="use=forgot&add="+ema;
		$.ajax({  
			url: '/includes/functions/cart_ajax.php',  
	        data: params,  
			dataType: "text",
			type: "GET",
			success: function(data){
				$('#fback').html(data);
			}
		});
	}	
}

function cdType(id, type){
	alert("CD "+id+" will be changed to a "+type+" CD, please wait a moment.\n\nYour browser will automatically refresh once this process is complete.");
	params="use=cdtype&cdid="+id+"&type="+type;
	$.ajax({  
		url: '/includes/functions/cart_ajax.php',  
	    data: params,  
		dataType: "text",
		type: "GET",
		success: function(data){
			window.location.reload();
		}
	});
}
