function setupLink()
{
	//alert(document.getElementById("linkLWSubmit").onclick)
	if (document.getElementById("linkLWSubmit").onclick.toString().indexOf("validateChangePassword") == -1)
	{
		document.getElementById("linkLWSubmit").onclick = "return validateChangePassword(document.frmChangePassword);" + 
		document.getElementById("linkLWSubmit").onclick;
	}
}


function updatePasswordSubmit()
{
	var pword = jQuery("#pword").val()
	var user_id = jQuery("#user_id").val()
	if (validateChangePassword(document.frmChangePassword))
	{
		jQuery.ajax({
			type: "POST",
			url: "change_password_process.asp",
			data: "pword="+pword+"&changePassword=y&user_id="+user_id,
			success: function(msg){
				showPasswordUpdated();
			}
 		});
	}
	
}

function showPasswordUpdated()
{
	jQuery("#divPwordUpdateForm").hide();
	jQuery("#divSuccess").fadeIn("slow");
}


function validateChangePassword(form)
{
	var bolValid = true;
	var errorMessage = 'The following password requirements are not yet met:\n\n'
	
	var check={
	  '.{8,}':'eight characters',
	  '[0-9]':'one number (0-9)',
	  '[a-zA-Z]':'one letter (a-z)',
	}
	//alert("form.pword.value: " + form.pword.value)
	var message='';
	for (var exp in check)
	  if (!form.pword.value.match(new RegExp(exp)))
	  {
		errorMessage +='Password must contain at least '+check[exp]+'.\n'
		 bolValid = false;
	  }
	
	
	if (form.pword.value != form.pword_confirm.value) { 
            bolValid = false; 
            errorMessage += "Password and confirmation must match.\r\n"; 
	}
	/*
	if (form.address.value=="") { 
            bolValid = false; 
            errorMessage += "Please enter your address.\r\n"; 
	}
	*/
				
	if (bolValid) {
		return true;
	}

	else	{
		alert(errorMessage);
		return false;
	}		
}

function validateCatalogueRequest(form) {
	var re = /^[^.]+(\.[^.]+)*@([^.]+[.])+[a-zA-Z]{2,3}$/;
	var bolValid = true;
	var errorMessage = ''
	
	if (form.name.value=="") { 
            bolValid = false; 
            errorMessage += "Please enter your name.\r\n"; 
	}
	if (form.address.value=="") { 
            bolValid = false; 
            errorMessage += "Please enter your address.\r\n"; 
	}
	
	if (form.email.value=="") { 
            bolValid = false; 
            errorMessage += "Please enter your email address.\r\n"; 
	} else {
		
		if (form.email.value.search(re) != 0) {
			bolValid = false; 
			errorMessage += "Please enter your valid email address.\r\n"; 
		}
	}
				
	if (bolValid) {
		return true;
	}

	else	{
		alert(errorMessage);
		return false;
	}	
	
}

function proceedToPaymentClick()
{
	if(checkCookieConsent()) //Check if cookie consent given
	{
		jQuery("#frmBasket").attr("action", "account.asp");
		return true;
	}	
	else
	{
		return false;
	}
}

function checkCookieConsent()
{
	if(jQuery("#consentCookies:checked").val() !== undefined)
	{
		return true;
	}
	else
	{
		alert("The cookies consent box below must be checked in order to proceed.");
		return false;
	}
}


function savePostageSendEmail()
{
	var lvstrURL = "order_international.asp?oid=" + getQueryStringValueByName("oid") + "&order_postage=" + jQuery("#postage").val();
	window.location = lvstrURL;
}

function orderCountry()
{
	//If the selected shipping country isn't local, show/hide the relevant controls.
	if(jQuery("#country_ship").val() != countryName(jQuery("#local_country").val()) && jQuery("#orderRecover").val() != "y")
	{
		jQuery("#divCCDetails").hide();
		jQuery(".localPmtOptions").hide();
		jQuery("#trQuote").fadeIn();
		jQuery("#quote").attr("checked", true);
		jQuery("#divTax").hide();
		jQuery("#divNoTax").show();
		jQuery("#divTotalInc").hide();
		jQuery("#divTotalExc").show();
		jQuery("#divShipping").hide();
		jQuery("#divShippingTBD").show();
		jQuery("#is_international").val("1");				
	}
	else
	{
		if(!jQuery(".localPmtOptions").is(":visible"))
		{
			jQuery("#trQuote").hide();
			jQuery(".localPmtOptions").fadeIn();
			jQuery("#divCCDetails").fadeIn();					
			jQuery("#quote").attr("checked", false);
			jQuery("#radCC").attr("checked", true);	
			jQuery("#divTax").show();
			jQuery("#divNoTax").hide();	
			jQuery("#divTotalInc").show();
			jQuery("#divTotalExc").hide();
			jQuery("#divShipping").show();
			jQuery("#divShippingTBD").hide();	
			jQuery("#is_international").val("0");
		}
	}
}


function countryName(paramCountryCode)
{
	var country = "";
	switch(paramCountryCode)
	{
		case "NZ":
			country = "New Zealand";
			break;
			
		case "A":
			country = "Australia";
			break;
			
		case "US":
			country = "United States";
			break;
			
		case "UK":
			country = "UK";
			break;
			
		case "C":
			country = "New Zealand";
			break;
	}
	return country;
}

jQuery(document).ready(function() {
	
	jQuery(".leftNavButton")
	.mouseover(function() { 
		//muteOtherButtons(this);
		if(jQuery(this).attr("src").indexOf("_over") == -1) //If the image doesn't include "_over" then it needs to be set to over state.
		{
			var imgSrc = jQuery(this).attr("src").replace(".png", "_over.png");
			jQuery(this).attr("src", imgSrc);
		}
		
     });
	 
	jQuery("div ul li a:not([class^=p7PMM_img])")
	.mouseover(function() { 
		//muteOtherButtons(this);
		var parentMenu = jQuery(this).parents("[class*=root]")
		var parentMenuImage = jQuery(parentMenu).contents().find("img");
		//alert(jQuery(parentMenuImage).attr("src"))
		setButtonOn(parentMenuImage)
		
		//alert(jQuery(parentMenu).attr("class"));
		/*if(jQuery(this).attr("src").indexOf("_over") == -1) //If the image doesn't include "_over" then it needs to be set to over state.
		{
			var imgSrc = jQuery(this).attr("src").replace(".png", "_over.png");
			jQuery(this).attr("src", imgSrc);
		}
		*/
     });
	 
	 jQuery("#LHnav")
	.mouseleave(function(event) {
		//Using the "mouseleave" function because it doesn't bubble to child elements like mouseout does.
		resetButtons();
     });
	 
});

function getMenuImageOfSubMenu(paramSubMenuLink)
{
	var parentMenu = jQuery(paramSubMenuLink).parents("[class*=root]")
	var parentMenuImage = jQuery(parentMenu).contents().find("img");
	return parentMenuImage;
}

function setButtonOn(paramImage)
{
	if(jQuery(paramImage).attr("src"))
	{
		if(jQuery(paramImage).attr("src").indexOf("_over") == -1) 
		//If the image doesn't include "_over" then it needs to be set to over state.
		{
			var imgSrc = jQuery(paramImage).attr("src").replace(".png", "_over.png");
			jQuery(paramImage).attr("src", imgSrc);
		}
	}
}

function muteOtherButtons(paramCurrentButton)
{
	jQuery(".leftNavButton").each(
		function(){
			if (jQuery(this).attr("src") != jQuery(paramCurrentButton).attr("src"))
			{
				var imgSrc = jQuery(this).attr("src").replace("_over", "");
				jQuery(this).attr("src", imgSrc);
			}
		}
	)
}

function resetButtons()
{
	jQuery(".leftNavButton").each(
		function(){
			if(jQuery(this).attr("src").indexOf("_over") == -1) //If the image doesn't include "_over" then it needs to be reset.
			{
				var imgSrc = jQuery(this).attr("src").replace(".png", "_over.png");
				jQuery(this).attr("src", imgSrc);
			}
		}
	)
}

function deleteSubCat(paramProdID)
{
	var catID = jQuery("#catsProdID"+paramProdID.toString()).val();
	var SubCatID = jQuery("#subcatProdID"+paramProdID.toString()+"CatID"+catID).val()
	var selectedText = jQuery("#subcatProdID"+paramProdID.toString()+"CatID"+catID+" option:selected").text();
	if(confirm("Delete sub-category " + selectedText + "?"))
	{
		window.location = "admin.asp?doaction=deleteSubCat&product_id=" + paramProdID +
		"&category_id=" + catID + "&subcat_id=" + SubCatID + addAdminQueryString();
	}
}

function setParentCatOfUpdatedProduct()
{
	if(getQueryStringValueByName("product_id").length > 0 && getQueryStringValueByName("category_id").length > 0)
	{
		jQuery("#catsProdID"+getQueryStringValueByName("product_id")).val(getQueryStringValueByName("category_id"));
		showHideSubCats(+getQueryStringValueByName("product_id"),getQueryStringValueByName("category_id"));
	}
}

function addSubCategoryToProduct(paramProductID, paramCategoryID, paramSubCatID)
{
	var lvstrQueryString = "";
	if(getQueryStringValueByName("keyword").length > 0)
	{
		lvstrQueryString = "&keyword=" + getQueryStringValueByName("keyword");
	}
	if(getQueryStringValueByName("isAdv_search").length > 0)
	{
		lvstrQueryString += "&isAdv_search=" + getQueryStringValueByName("isAdv_search");
	}
	window.location = "admin.asp?doaction=addSubCat&product_id=" + paramProductID.toString() + "&category_id=" + paramCategoryID.toString()
	+ "&subcat_id=" + paramSubCatID + addAdminQueryString();
}

function addAdminQueryString()
{
	//Used for adding existing QS values to a redirect.
	var lvstrQueryString = "";
	if(getQueryStringValueByName("keyword").length > 0)
	{
		lvstrQueryString = "&keyword=" + getQueryStringValueByName("keyword");
	}
	if(getQueryStringValueByName("isAdv_search").length > 0)
	{
		lvstrQueryString += "&isAdv_search=" + getQueryStringValueByName("isAdv_search");
	}
	if(getQueryStringValueByName("pg").length > 0)
	{
		lvstrQueryString += "&pg=" + getQueryStringValueByName("pg");
	}
	return lvstrQueryString;
}

function addCategoryToProduct(paramProductID, paramCategoryID)
{
	window.location = "admin.asp?doaction=addCat&product_id=" + paramProductID.toString() + "&category_id=" + 
	paramCategoryID.toString() + addAdminQueryString();
}

function showHideSubCats(paramProdID,paramCatID)
{
	jQuery(".subcatsProdID"+paramProdID.toString()).hide();
	jQuery("#subcatProdID"+paramProdID.toString()+"CatID"+jQuery("#catsProdID"+paramProdID.toString()).val()).show();
	jQuery("[name='addSubCategoryProdID"+paramProdID.toString()+"CatID"+jQuery("#catsProdID"+paramProdID.toString()).val()+"']").show();
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

var newwin;
function launchwin(winurl,winname,winfeatures)
{
	//This launches a new window and then
	//focuses it if window.focus() is supported.
	newwin = window.open(winurl,winname,winfeatures); 
	window.newwin.focus();    
}

function CloseWindow(){
	self.close()
}

function validateForgotPasword(form) {
	var re = /^[^.]+(\.[^.]+)*@([^.]+[.])+[a-zA-Z]{2,3}$/;
	var bolValid = true;
	var errorMessage = ''
	
	if (form.user_email.value=="") { 
            bolValid = false; 
            errorMessage += "Please enter your email address.\r\n"; 
	} else {
		
		if (form.user_email.value.search(re) != 0) {
			bolValid = false; 
			errorMessage += "Please enter your valid email address.\r\n"; 
		}
	}
				
	if (bolValid) {
		return true;
	}

	else	{
		alert(errorMessage);
		return false;
	}	
	
}

function setSearchTextFocus(paramSearchField)
{
	if(paramSearchField.value=="Enter keyword or code")
	{
		paramSearchField.value = "";
	}
}

function setSearchTextBlur(paramSearchField)
{
	if(paramSearchField.value=="")
	{
		paramSearchField.value = "Enter keyword or code";
	}
}

function shipSameAsBilling()
{
	if (jQuery('#chkShipSameAsBilling:checked').val() !== undefined) //Box is checked
	{
		var inputs = jQuery('#frmCheckout :input'); //Get form input elements
		inputs.each(function() //iterate through elements
		{
			if(jQuery("#"+this.name+"_ship").length) //If element has an "_ship" equivalent then copy over the value.
			{
				jQuery("#"+this.name+"_ship").val(jQuery("#"+this.name).val())
			}
		});
	}
}


function return2br(str)
{ 
	var strSingleLineText = str.replace(
	// Replace out the new line character.
	new RegExp( "\\n", "g" ),"<br />");	 
	return strSingleLineText;
} 

function appendFormValues()
{
	var lvstrReview = jQuery("#review").val();
	var lvstrName = jQuery("#name").val();
	lvstrName = jQuery.trim(lvstrName); //Remove leading and trailing spaces and line breaks.
	lvstrReview = jQuery.trim(lvstrReview); //Remove leading and trailing spaces and line breaks.
	lvstrReview = return2br(lvstrReview) //Convert line breaks to <br />
	jQuery("#linkSubmit").attr("href", "reviews_add.asp?name=" + encodeURI(lvstrName) + "&review=" +   encodeURI(lvstrReview) + 
	"&product_id=" +  encodeURI(jQuery("#product_id").val()));
}

function appendCommentValues()
{
	var lvstrReview = jQuery("#comment").val();
	var lvstrName = jQuery("#name").val();
	lvstrName = jQuery.trim(lvstrName); //Remove leading and trailing spaces and line breaks.
	lvstrReview = jQuery.trim(lvstrReview); //Remove leading and trailing spaces and line breaks.
	lvstrReview = return2br(lvstrReview) //Convert line breaks to <br />
	jQuery("#linkSubmit").attr("href", "cust_comment_add.asp?name=" +  encodeURI(lvstrName) + "&comment=" +   encodeURI(lvstrReview));
}

function appendLoginValues()
{
	var lvstrEmail = jQuery("#user_email").val();
	var lvstrPassword = jQuery("#user_password").val();
	var lvintCartID = jQuery("#cartid").val();
	lvstrEmail = jQuery.trim(lvstrEmail); //Remove leading and trailing spaces and line breaks.
	lvstrPassword = jQuery.trim(lvstrPassword); //Remove leading and trailing spaces and line breaks.
	lvintCartID = jQuery.trim(lvintCartID); //Remove leading and trailing spaces and line breaks.
	jQuery("#linkSubmit").attr("href", "login_user.asp?user_email=" + encodeURI(lvstrEmail) + "&user_password=" +  encodeURI(lvstrPassword) + "&cartid=" + encodeURI(lvintCartID));
}


function showHideCCDetails()
{
	if (jQuery("#radCC").is(':checked'))
	{
		jQuery("#divCCDetails").fadeIn();
	}
	else 
	{
		jQuery("#divCCDetails").fadeOut();
	}	
}

function showSwitch(id,bolhide){
	if (document.getElementById){
		var tr=document.getElementById(id);
		if (tr){
			if (bolhide==1){
				if(navigator.appName.indexOf("Microsoft") > -1){
					tr.style.display = 'block';
				} 
				else {
					tr.style.display='table-row';
				}						
			}
			else {
				tr.style.display = 'none';
			}
		}     
	}
}

function getQueryStringValueByName(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}
