// Radio Button Validation
function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
}

function isEmail(argvalue) {
  	if (argvalue.indexOf(" ") != -1)
    	return false;
  	else if (argvalue.indexOf("@") == -1)
    	return false;
  	else if (argvalue.indexOf("@") == 0)
    	return false;
  	else if (argvalue.indexOf("@") == (argvalue.length-1))
    	return false;

	var strArray = argvalue.split("@");

	if (strArray[1].indexOf(".") == 0) 
		return false;		
	else if (strArray[1].indexOf(".") == -1) 
		return false;
	else if (strArray[1].indexOf(".") == (strArray[1].length-1)) 
		return false;

	var laniArray = strArray[1].split(".");

	if (laniArray[(laniArray.length)-1]=="")
		return false;

  return true;
}

function validateshipping(theform){
	var validate = theform.validateShipping;
	var country = theform.Country;
	var postal = theform.PostalCode;

	if (validate.value=="true"){
		if (country.selectedIndex==0){
			alert("Please select your preferred shipping country from the list.");
			country.focus();
			return false;
		}

		if (postal.value==""){
			alert("Recipient's postal code cannot be blank.");
			postal.focus();
			return false;
		}

		var optship = valButton(theform.optShippingOptionID);
		if (optship==null){
			alert("Please select your preferred shipping method.");
			return false;
		}
	}
}

function validateshipping_ORIG(theform){
	var validate = theform.validateShipping;
	var country = theform.Country;
	var postal = theform.PostalCode;

	if (validate.value=="true"){
		var optship = valButton(theform.optShippingOptionID);
		if (optship==null){
			alert("Please select your preferred shipping method.");
			return false;
		}
	}
	else
	{
		if (country.selectedIndex==0){
			alert("Please select your preferred shipping country from the list.");
			country.focus();
			return false;
		}
	
		if (postal.value==""){
			alert("Recipient's postal code cannot be blank.");
			postal.focus();
			return false;
		}
	}
}

function noshipping(){
	//alert("Cannot proceed to check out.\n\nPls select your preferred shipping method from the list and enter the recepient's postal code then click [Update Totals] button.");
	alert("Cannot proceed to check out.\n\nPls enter the recepient's postal code then click [Get Shipping Rates] button.");
	return false;
}

function validatezipcode(theform){
	var country = theform.Country;
	var zip = theform.PostalCode;

	if (country.selectedIndex==0){
		alert("Please select your preferred shipping country from the list.");
		country.focus();
		return false;
	}

	if (zip.value==""){
		alert("Recipient's zip code cannot be blank.");
		zip.focus();
		return false;
	}
}

function Show_CountryShipping(thectrl, theform){
	if(thectrl.value == "US"){
		document.getElementById("US").style.display = "block";
		document.getElementById("CA").style.display = "none";
		theform.optShippingOptionID[0].checked = true;
		theform.optShippingOptionID[1].checked = false;
	}
	else if (thectrl.value == "CA"){
		document.getElementById("CA").style.display = "block";
		document.getElementById("US").style.display = "none";
		theform.optShippingOptionID[0].checked = false;
		theform.optShippingOptionID[1].checked = true;
	}
	else{
		document.getElementById("US").style.display = "none";
		document.getElementById("CA").style.display = "none";
	}
}

function highlightShipping(theform){
	var opt1 = theform.optShippingOptionID[0]
	var opt2 = theform.optShippingOptionID[1]
	if (opt1.checked){
		document.getElementById("label1").style.fontWeight="bold"
		document.getElementById("label2").style.fontWeight="normal"
	}
	if (opt2.checked){
		document.getElementById("label2").style.fontWeight="bold"
		document.getElementById("label1").style.fontWeight="normal"
	}
}

function switchImage(thisName){
	document.getElementById("imgPic").src="images/img/" + thisName.replace("tiny", "img");
	//document.getElementById("zoom").href="images/large/" + thisName.replace("img", "large");
	//document.getElementById("imgzoom").href="images/large/" + thisName.replace("img", "large");

	//document.getElementById("zoom").href="images/large/" + thisName;
	//document.getElementById("imgzoom").href="images/large/" + thisName;
}

function switchGallery(thisImg, thisDesc){
	document.getElementById("imgPic").src="images/gallery/" + thisImg.replace("tiny", "img");
	//document.getElementById("strName").innerHTML=thisName;
	document.getElementById("strDesc").innerHTML=thisDesc;
	//document.getElementById("zoom").href="images/large/" + thisName.replace("img", "large");
	//document.getElementById("imgzoom").href="images/large/" + thisName.replace("img", "large");

	//document.getElementById("zoom").href="images/large/" + thisName;
	//document.getElementById("imgzoom").href="images/large/" + thisName;
}

function validatepromotion(theform){
	var promocode = theform.PromotionCode;

	if (promocode.value==""){
		alert("Coupon code cannot be blank.");
		promocode.focus();
		return false;
	}
}

function validategallery(theform){
	var useremail;
	var fname = theform.Name;
	var cname = theform.ChildrensName;
	var age = theform.Age;
	var hometown = theform.HomeTown;
	var email = theform.Email;
	var filename = theform.URL_Document;
	var captcha = theform.captchacode;
	var chkbox = theform.chkTerms;

	useremail=isEmail(email.value);

	if (fname.value==""){
		alert("Name cannot be blank.");
		fname.focus();
		return false;
	}

	if (cname.value==""){
		alert("Child(ren)'s Name cannot be blank.");
		cname.focus();
		return false;
	}

	if (age.value==""){
		alert("Age cannot be blank.");
		age.focus();
		return false;
	}

	if (hometown.value==""){
		alert("Home Town cannot be blank.");
		hometown.focus();
		return false;
	}

	if (email.value!=""){
		if (useremail==false) {
			alert("Please enter your proper username/email address.");
			email.focus();
			return false;
		}
	}

	if (filename.value==""){
		alert("Pls click the [Browse] button and select the photo you would like to attach.");
		return false;
	}

	if (captcha.value == ""){
		alert("Please enter the security code.");
		captcha.focus();
		return false;
	}

	if (!chkbox.checked){
		alert("Please check the [Image Use Terms] checkbox.");
		return false;
	}
}

function clearFile(theform){
	var filename = theform.URL_Document;
	var filesize = theform.DocumentSize;

	filename.value = "";
	filesize.value = "";
}