

// toggle comment view



// switch product tabs
$(".tab").live('click', function() {	
	$(".tab").removeClass("active");												 
	$(this).addClass("active");
	$(".tab-item").css("display", "none");	
	$("#" + this.id + "-content").css("display", "block");
});

// load review form
if ($('#review-form').length > 0) {
	$.post("handlers/review-form.php", {
		cache : false
	},
	function(data) {
		$("#review-form").html(data);
	});
}

function changeOrder(order, url, section) {
	document.location = url + "&section=" + section + "&order=" + order;
}

function changeManOrder(order, url) {
	document.location = url + "&order=" + order;
}

$.post(coreURL + "/handlers/get-cart-items.php", {
	cache 				: false,
	"product_id"	:	cartID
},
function(data) {
	$("#mini-cart-ajax").html(data);
});


$('#addToCart').submit(function() {
		var values = {};
		$.each($('#addToCart').serializeArray(), function(i, field) {
    	values[field.name] = field.value;
		});
	  $.post(coreURL + "/handlers/add-to-cart.php", {
			cache 				: false,
			"quantity" 		: values["quantity"],
			"variant1" 		: values["variant1"],
			"variant1" 		: values["variant1"],
			"variant2" 		: values["variant2"],
			"variant3" 		: values["variant3"],
			"ref"					:	values["ref"],
			"price"				:	values["price"],
			"product_id"	:	values["product_id"]
		},
		function(data) {
			$("#mini-cart-ajax").html(data);
		}
		);
  return false;
});

/* Language switch */
$("#language").live('change', function() {
	$("#language-form").submit();														 
});





/* Clears text field on selection */
function clearFieldxxx(input,type,text) {
	input.type = type;
	if (input.value == text) {
		input.value = "";
	}
	input.style.color = "#000";
}

/* Populates with default value if text field is empty */
function redoField(input,type,text) {
	if (input.value == "") {
		input.type = type;
		input.value = text;
		input.style.color = "#000";
	}
}

/* Image change for product information */
function changeImage(target, source) {
	document.getElementById(target).style.background = "url(" + source + ") no-repeat 50% 50%";
}

/* Function to pad number */
function PadDigits(n, totalDigits) { 
	n = n.toString(); 
	var pd = ''; 
	if (totalDigits > n.length) { 
		for (i=0; i < (totalDigits-n.length); i++) { 
			pd += '0'; 
		} 
	} 
	return pd + n.toString(); 
}

/* Form validation */
function validate(thisform, error_message)
{
	var error = 0;
	var too_young = 0;
	form_inputs = thisform.getElementsByTagName("input");
	for (var i = 0; i < form_inputs.length; i++) 
	{ 
		if (form_inputs[i].id == "required" 
			|| form_inputs[i].id == "password" || form_inputs[i].id == "password2"
			|| form_inputs[i].id == "email" || form_inputs[i].id == "email2"
		)
		{
			if (form_inputs[i].value==null || form_inputs[i].value=="" || form_inputs[i].value==form_inputs[i].defaultValue) {
				error++;
				//form_inputs[i].style.background = "url(images/bg-input-error.jpg)";
			}
			else {
				//form_inputs[i].style.background = "url(images/bg-input-register.jpg)";
			}
		}
	}
	
	/* Minimum age test */
	var minAge = 18;
	var one_day=1000*60*60*24;
	
	/* get submitted dob */
	var formYear = document.getElementById("regDobYear").value;
	var formMonth = document.getElementById("regDobMonth").value-1;
	var formDay = document.getElementById("regDobDay").value;
	
	/* get current time adjusted for minimum age */
	var curTime = new Date();
	var curYear = curTime.getFullYear()-minAge;
	var curMonth = curTime.getMonth();
	var curDay = curTime.getDate();
	
	/* calculate date minus minage epoch  */
	var minAgeTime = new Date(curYear, curMonth, curDay);
	var minAgeEpoch = minAgeTime.getTime();

	/* calculate dob epoch */
	dobTime = new Date(formYear, formMonth, formDay);
	dobEpoch = dobTime.getTime();

	/* compare and return error */
	dobMinDiff = Math.ceil((minAgeEpoch-dobEpoch)/one_day);
	if (dobMinDiff < 0) {
		alert("You must be over 18 years of age to sign up with with Petscy.");
		return false;
	}
	/* check all required fields are populated */
	else if (error > 0) {
		alert(error_message);
		return false;
	}
	if (document.getElementById("username-warn").innerHTML.length > 0) {
		alert("The username you have chosen is already in use. Please choose another to continue.");
		return false;
	}
	
	if (document.getElementById("email-warn").innerHTML.length > 0) {
		alert("The email address you have chosen is already in use. Please choose another to continue.");
		return false;
	}
	/* check password length */
	else if (document.getElementById("password").value.length < 6) {
		alert("Your password must be at least 6 characters long.");
		return false;
	}	
	/* check passwords match */
	else if (document.getElementById("password").value !== document.getElementById("password2").value) {
		alert("The passwords you have supplied do not match.");
		return false;
	}	
	/* check email length */
	else if (document.getElementById("email").value.length < 7) {
		alert("You must enter a valid email address to sign up with Petscy.");
		return false;
	}	
	/* check emails match */
	else if (document.getElementById("email").value !== document.getElementById("email2").value) {
		alert("The email addresses you have supplied to not match.");
		return false;
	}	
	//else if (document.getElementById("lat").value == "") {
	//	alert("Please select your location on the map.");
	//	return false;
	//}
	else if (!document.getElementById("disclaimer").checked) {
		alert("You must agree to the Petscy terms and conditions to sign up.");
		return false;
	}
	else if (!document.getElementById("overage").checked) {
		alert("You must confirm you are over 18 years of age to use Petscy.");
		return false;
	}
	else {
		return true;
	}
}

/* Switch from input background back after error */
function clearErrorBg(input) {
	input.style.background = "url(images/bg-input-register.jpg)";
}

/* Show popup overlay and dialogue */
function loadPopup(div) {
	document.getElementById("overlay").style.display 	= "block";
	document.getElementById(div).style.display 				= "block";
}

function loadAutobid() {
	document.getElementById("overlay").style.display = "block";
	document.getElementById("autobid").style.display = "block";
}

/* Close overlay and dialogue */
function closeDialogue(div) {
	document.getElementById("overlay").style.display 	= "none";
	document.getElementById(div).style.display 				= "none";
}

/*function checkoutRedirect(ref,id,type,total,quantity) {
	// call database update handler
	if (type != "auction") {
		$.ajax ({
			type: "GET",
			cache: false,
			url: "handlers/new-order.php?ref=" + ref + "&item=" + id + "&type=" + type + "&total=" + total +"&quantity=" + quantity
		});
	}
	window.location="confirm.php?ref=" + ref;
}*/

/*function tellFriend() {
	friendsEmail = document.getElementById('friendsEmail').value;
	$("#friendsEmail").text("");
	$.ajax({
		url: "handlers/tell-friend.php?email=" + friendsEmail,
		cache: false,
		success: function(response){
			$("#tell-friend-text").html(response);
		}
	})
}*/

/* Check user is authenticated */
/*$(this).everyTime(1000, "meh", function() {
	$.ajax ({
		url: "handlers/get-uas.php",
		cache: false,
		success: function(uas){
			document.getElementById("check-uas").value = uas;
		}
	});
	// get server ping
	$.ajax({
		url: "handlers/ping.php",
		cache: false,
		success: function(ping){
			if (ping >= 1000) barwidth = 0;
			if (ping < 1000 && ping >= 900) barwidth = 10;
			if (ping < 900  && ping >= 800) barwidth = 20;
			if (ping < 800  && ping >= 700) barwidth = 30;
			if (ping < 700  && ping >= 600) barwidth = 40;
			if (ping < 600  && ping >= 500) barwidth = 50;
			if (ping < 500  && ping >= 400) barwidth = 60;
			if (ping < 400  && ping >= 300) barwidth = 70;
			if (ping < 300  && ping >= 200) barwidth = 80;
			if (ping < 200  && ping >= 100) barwidth = 90;
			if (ping < 100) barwidth = 100;
			$("#ping-bar").html(ping);
			$("#ping-bar").css("width", barwidth);
		}
	});
});*/

/*$(document).ready(function(){
	
	$.ajax ({
		url: "handlers/check-uas.php",
		cache: false,
		success: function(uas){
			document.getElementById("check-uas").value = uas;
		}
	});
	
});*/
	// subscribe popup
	
	$("#subscribe-button").live('click', function() {	
		$("#overlay").css("display", "block");
		centerThis("#subscribe-popup");
		$("#subscribe-popup").css("display", "block");
		return false;
	});

	
	// subscribe form contents
	$.post("get-subscribe-form.php", {
			cache 		: false
		},
		function(data) {
			$("#subform").html(data);
		}
	);
	
	// subscribe form action
	function subSubmit() {
		submitted = $("#submitted").value;
		subname 	= $("#subname").innerHTML;
		
		submitted = document.getElementById("submitted").value;
		subname = document.getElementById("subname").value;
		subemail = document.getElementById("subemail").value;
		subanswer = document.getElementById("subanswer").value;
		
		$.post("get-subscribe-form.php", {
			cache 		: false,
			"submitted" : submitted,
			"subname" 	: subname,
			"subemail" 	: subemail,
			"subanswer" : subanswer
		},
		function(data) {
			$("#subform").html(data);
		}
		);
		
	}
	
	// reset the subscription form
	function subReset() {
		// subscribe form contents
	$.post("get-subscribe-form.php", {
			cache 		: false
		},
		function(data) {
			$("#subform").html(data);
		}
	);
	}
	
	
	
	function closeSub() {
		$("#subscribe").css("display", "none");
		$("#overlay").css("display", "none");
		//$("body").css("background", "#000 url(images/bg-body.jpg) no-repeat center top");
		$.post("get-subscribe-form.php", {
			cache 		: false
		},
		function(data) {
			$("#subform").html(data);
		}
	);
	}
	
	function popout(image) {
		$("#popout").css("display", "block");
		$("#popout").centerInClient({forceAbsolute: true});
		$("#popout").css("background", "#fff url(uploads/images/medium/" + image + ") no-repeat 50% 50%");
		$("#overlay").css("display", "block");
		$("#overlay").css("opacity", "0.8");
		$("body").css("background", "#000");
	}
	
	function closePopout() {
		$("#popout").css("display", "none");
		$("#overlay").css("display", "none");
		$("body").css("background", "#000 url(images/bg-body.jpg) no-repeat center top");
	}
	
	
	/*if ($('#variants').length) {  
    console.log(product);
		console.log($('#variants').length);
		$.post("handlers/get-variants.php", {
			cache 		: false,
			"product" : product
			//"subname" 	: subname,
			//"subemail" 	: subemail,
			//"subanswer" : subanswer
		},
		function(data) {
			$("#variants").html(data);
		}
		);
	} */
	
	function reload(first, second, third) {
		//$(".price-block").hide();
		//$(".price-block-loading").show();
		//if (first.length > 0) first = "first=" + first;
		//if (second.length > 0) second = "&second=" + second;
		//if (third.length > 0) third = "&third=" + third;
		$.post("handlers/get-variants.php", {
			cache 		: false,
			"product" : product,
			"first"		: first,
			"second" 	: second,
			"third" 	: third
		},
		function(data) {
			$("#variants").html(data);
			//setTimeout(
			//	'$(".price-block").show(); $(".price-block-loading").hide();'
			//,500);
		}
		);
		//window.location.href = "variants.php?" + first + second + third;
	}
	
	
	



