// JavaScript Functions
function validateContactUs(){
	var	ERROR = "";
	if (document.getElementById("email").value == "") ERROR = "A Email is required.\n";
	else if (!IsEmail(document.getElementById("email").value)) ERROR = "Please enter a valid Email.\n";
	if (document.getElementById("message").value == "") ERROR += "Please enter your message.\n";
	
	if (ERROR != ""){
		alert(ERROR);
		return false;
	}else	return true;
}

function updateSplitQty(resubmitType){
	document.getElementById("updateQty").value = resubmitType;
	document.basket_split.submit();
}

function validateQty(){
	ERROR = false;
	for (var i = 0; i < document.getElementById("prod_count").value; i++){
		qty = 0;
		totalQty = eval("document.getElementById('totalQty_"+ i + "').value");
		if (document.getElementById("total_rec_"+i).value == 0) ERROR = true;
		else if (document.getElementById("total_rec_show_"+i).value != document.getElementById("total_rec_"+i).value) ERROR = true;
		else {
			for( var x = 0; x < document.getElementById("total_rec_"+i).value; x++){
				pid = eval("document.getElementById('pid_" + i + "').value");
				rec = eval("document.getElementById('rec_" + i + "_" + x + "').value");
				name = "qty_" + pid + "[" + rec + "]";
				recQty = eval ("document.getElementById('" + name + "').value")
				qty = Number(qty) + Number(recQty);
			}
			if (qty != totalQty) ERROR = true;
		}
	}
	if (ERROR){
		alert("Verify your destinations and the amount distributed")
	}else	updateSplitQty(2); //last command (will submit form)
}
function changeShippers(prodId, count){
	var totalRecShow = eval("document.getElementById('total_rec_show_" + count + "').value"); 
	var shipNum = eval("document.getElementById('shipNum_" + prodId + "').value");
	if (totalRecShow <= shipNum){
		document.getElementById("addShipper").value = shipNum;
		document.getElementById("productId").value = prodId;
		document.basket_split.submit();
	}else
		alert("You must remove a shipping destination\n to decrease the number of destinations");
}
function splitOrder(splitting){
	if (splitting)
		document.getElementById("doAction").value = "multi";
	else
		document.getElementById("doAction").value = "single";
	document.basket_split.submit();		
}
function addRec(key, pid, count){
	if (key == "single"){
		var selectBox = document.basket_split.recipientSelect_single;
		var recipient = selectBox.options[selectBox.selectedIndex].value;
		if (recipient != ""){
			document.getElementById("productId").value = "single";
			document.getElementById("addRecipient").value = recipient;
			document.basket_split.submit();
		}
	}else{
		var selectBox = eval("document.basket_split.recipientSelect_" + key);
		var recipient = selectBox.options[selectBox.selectedIndex].value;
		var productQty = eval("document.basket_split.totalQty_" + count + ".value");
		if (recipient != ""){
			document.getElementById("productId").value = pid;
			document.getElementById("addRecipient").value = recipient;
			document.getElementById("productQty").value = productQty;
			document.basket_split.submit();
		}
	}
}

function removeRecipient(rid, pid){
	document.getElementById("delRecipient").value = rid;
	document.getElementById("productId").value = pid;
	document.basket_split.submit();
	
}

function IsEmail(sEmail){
	var at="@"
	var dot="."
	var lat=sEmail.indexOf(at)
	var lstr=sEmail.length
	var ldot=sEmail.indexOf(dot)
	if (sEmail.indexOf(at)==-1)		return false
	if (sEmail.indexOf(at)==-1 || sEmail.indexOf(at)==0 || sEmail.indexOf(at)==lstr)	return false
	if (sEmail.indexOf(dot)==-1 || sEmail.indexOf(dot)==0 || sEmail.indexOf(dot)==lstr)	return false
	if (sEmail.indexOf(at,(lat+1))!=-1)		return false
	if (sEmail.substring(lat-1,lat)==dot || sEmail.substring(lat+1,lat+2)==dot)		return false
	if (sEmail.indexOf(dot,(lat+2))==-1)	return false
	if (sEmail.indexOf(" ")!=-1)	return false
	return true
}

function IsNumeric(sText){
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) { 
     Char = sText.charAt(i); 
     if (ValidChars.indexOf(Char) == -1) 	IsNumber = false;
    }
   return IsNumber;
 }