// JavaScript Document
function validateBillNumber(vendr, field){
	var vendor = vendr;
	var value = field.value;
//	alert("TEST " + value + " for " + vendor)
	if(vendor == "DHL"){
		//DHL rules number must be between 9 and 12 digits 
		//last digit is mod 7 ck digit all numeric
		var DHLmsg = "DHL Number is invalid, tracking number must be all digits.\nAnd contains 9 to 12 digits";
		if(testNumericOnly(value)){
			field.style.backgroundColor = '#ff8585';
			return highlightInput(field, DHLmsg);
		//	showAlert(DHLmsg);
		//	setTimeout(function(){field.focus()}, 10);
		//	return false;	
		}else if(!testNumericLength(value, 9,12)){
			field.style.backgroundColor = '#ff8585';
			return highlightInput(field, "The DHL tracking numer fails the length requirements.\n" + DHLmsg);
			//showAlert("The DHL tracking numer fails the length requirements.\n" + DHLmsg);
			//setTimeout(function(){field.focus()}, 10);
			//return false;
		}else if(!ckdigitpass(value, 7)){
			field.style.backgroundColor = '#ff8585';
			return highlightInput(field, "The MOD check digit caculation failed for DHL Tracking Number.\n" + DHLmsg);
		//	showAlert("The MOD check digit caculation failed for DHL Tracking Number\n" + DHLmsg);
		//	setTimeout(function(){field.focus()}, 10);
		//	return false;
		}else{//DHL TEST PASSED...
		//	alert("DHL TEST PASSED.");
			field.style.backgroundColor = 'white';
			return true;	
		}
	}else if(vendor == "FDX"){
		//FEDEX TESTS
		//WE ACCEPT 12 digits 22 digits or anything else....(apparently)
		var FDXmsg = "Fedex input may be invalid please verify your FEDEX tracking number.";
		if(testNumericOnly(value)){
			field.style.backgroundColor = '#ff8585';
			return highlightInput(field, FDXmsg);
			//showAlert(FDXmsg);
			//setTimeout(function(){field.focus()}, 10);
			//return false;
	/*	}else if(value.length == 12){ //MUST BE 12 22 or longer....
			alert("12 digit FDX track OK");
			field.style.backgroundColor = 'white';
			return true;
	*/	}else if(value.length == 22){
		//	alert("22 digit - test ck digit");
			//remove the first 6 digits
			var fmod = value.substring(value.length -1);
			var val1 = value.substring(7, value.length -1);
		//	alert("FMOD: " + fmod + " : "+ val1);
			//sum even and multiply by 3
			var evens = sumup(val1,1) * 3;
			var odds = sumup(val1, 0);
			var total1 = evens + odds;
		//	alert("E:" + evens + " O:" + odds + " t:"+total1);
			//substract total1 from the next multiple of 10 
			var ckdig = calc10(total1) - total1;
		//	alert("calccd:" + ckdig);
			if(fmod == ckdig){
			//	alert("FDX TEST PASSED 22 digits");
				field.style.backgroundColor = 'white';
				return true;	
			}else{
				field.style.backgroundColor = '#ff8585';
				return highlightInput(field, "Check Digit calculation error.\n" + FDXmsg);
			//	showAlert("Check Digit calculation error.\n" + FDXmsg);
			//	setTimeout(function(){field.focus()}, 10);
			//	return false;	
			}
			
		}else{//accept any other FDX tracking type < than 12 or more than 22
			field.style.backgroundColor = 'white';
			return true;	
		}
		
	}else{
		//any other combination for other vendors true;;;;;
		field.style.backgroundColor = 'white';
		return true;	
	}
}
function sumup(number, indx){
	//of the given string take the odd digits and sum them up
	//if index is 0 SUM ODDS if indx is 1 sum of evens
	var incrementor=2;
	var index = indx;
	var digit=0;
	while(index < number.length){
		digit=digit+parseInt(number[index]);
		index = index + incrementor;
	}
	//alert("SUM up IS " + digit);
	return digit;
}
function calc10(numb){
	var next10 = numb - numb%10 + 10;
	//alert("the next 10 is " + next10);
	return next10;
}
function ckdigitpass(value, moddigit){
	var fvalue = value.substring(0,value.length -1);
	var fmod = value.substring(value.length -1);
	//alert(fvalue + " : " + fmod);
	ansmod = fvalue % moddigit;
	if(ansmod == fmod){
		return true;	
	}else{
		return false;	
	}
	
}
function testNumericLength(value, nummin, nummax){
	if(value.length > nummin && value.length < nummax){
		return true;	
	}else{
		return false;	
	}
}
function checkchars(form, maxchar) {
	var max=maxchar;
	if (form.value.length > max){
		showAlert("This field supports up to " +  max + " characters.\nYour input will be truncated.");
		form.value = form.value.substring(0,max);
		return false;
   }
	else return true;
}
function isNumeric(field) {
	var value = field.value;          // get input value
// Change the backgroundColor of the input field based on whether or not
// the value of the input field is a number.
	if(testNumericOnly(value)) {
		field.style.backgroundColor = '#ff8585';
		return highlightInput(field, "Please Enter numeric values only. (0-9 .)");
		//showAlert("Please Enter numeric values only. (0-9 .)");
		//field.value = "";
		//setTimeout(function(){field.focus()}, 10);
		//return false;
	} else {
		field.style.backgroundColor = 'white';
		return true;
	}
}
function showAlert(message){
	alert(message);
}
function validate(formName){
	//CHECK THE CARRIER HAS BEEN SELECTED
	if(formName.zipx_carrier.selectedIndex == 0){
		//showAlert("Please select your Carrier.");
		var errEle = "zipx_carrier_err";
		document.getElementById(errEle).innerHTML="Please select your Carrier."
		document.getElementById(errEle).style.visibility="visible";		
		formName.zipx_carrier.focus();
		return false;
	}
	//CHECK FOR EMPTY FIELDS...
		//CHECK THAT THE CARRIER TRACKING IS VALID IF AVAILABLE
//alert("vl "+formName.zipx_carrier.options[formName.zipx_carrier.selectedIndex].value + " : " + formName.carrier_bill_number_req.value);
	if(!validateBillNumber(formName.zipx_carrier.options[formName.zipx_carrier.selectedIndex].value, formName.carrier_bill_number_req)){
		return false;		
	}
	for(var i=0; i<formName.elements.length; i++){
		var inputName = formName.elements[i];
        	if(isRequired(inputName) && isBlank(inputName)){
				return highlightInput(inputName); 
			}
    }

}
function isBlank(inputName){ //return true if field has no value
	return (inputName.value == "");
}
// This function returns true ONLY if the required field is set
function isRequired(inputName){
	//return ( (typeof(inputName.required) != 'undefined') && (inputName.required.toLowerCase() != 'false') ); 
	return (inputName.name.indexOf("_req")>0);
}
// This function highlights the input and returns false
function highlightInput(inputName, message){
	if (message == null){
		message = "Please enter a value for " + cleanUpName(inputName.name,"_req").toUpperCase(); 
	}
	//showAlert(message);
	inputName.style.borderColor = 'red';
	var errEle = inputName.name.concat("_err");
	document.getElementById(errEle).innerHTML=message
	document.getElementById(errEle).style.visibility="visible";
	setTimeout(function(){inputName.focus()}, 10);
	return false;
}
function hideError(inputName){
	document.getElementById(inputName).style.borderColor = '';
	var errEle = inputName.concat("_err");
	document.getElementById(errEle).style.visibility="hidden";
}
function cleanUpName(name,lookup){
	var nameIndex = name.indexOf(lookup);
	if(nameIndex != -1){
		return name.substring(0,nameIndex);
	}else return name;
}
//TEST if zipx account is formatted like this ##-########## (2digits followed by - and then up to 10 chars no symbols
function isZipxAccountValid(inputField){
	//TEST one does it contain a dash at position 3
	var value = inputField.value;
	if(value == ""){
		inputField.style.backgroundColor = 'white';
		return true;
	}else{
		var msg="Input for the "+ inputField.name + " field must be in form ##-AAAAAAAAAA\n"+
		"## is a two digit code and A can be number or letter up to 10 chars.";
		var dash = value.indexOf("-");
		//test 1
		if(dash != 2){
			inputField.style.backgroundColor = '#ff8585';
			showAlert(msg);
			//inputField.value = "";
			setTimeout(function(){inputField.focus()}, 10);
			return false;
		}else{
			//test2
			var numbersonly = value.substring(0,2);
			var alphaonly = value.substring(3);
			//alert("Numbers only " + numbersonly + "\nAlpha only " + alphaonly);
			if(testNumericOnly(numbersonly)){
				inputField.style.backgroundColor = '#ff8585';
				showAlert("The first two digits can only be numeric. { " + numbersonly + " }\n" + msg);
				//inputField.value = "";
				setTimeout(function(){inputField.focus()}, 10);
				return false;
			}else{
				//test 3
				if(alphaonly.length > 9 || alphaonly == ""){
					inputField.style.backgroundColor = '#ff8585';
					showAlert("The alpha chars cannot be empty or exceed 9 maximum characters.\n" + msg);
				//	inputField.value = "";
					setTimeout(function(){inputField.focus()}, 10);
					return false;
				}else if(!testAlphaNumericOnly(alphaonly)){
					inputField.style.backgroundColor = '#ff8585';
					showAlert("You entered non alpha numeric characters\n" + msg);
				//	inputField.value = "";
					setTimeout(function(){inputField.focus()}, 10);
					return false;
				}
			}
		}
	}
	inputField.style.backgroundColor = 'white';
	
}
function buildZipxAccount(field_a, field_b){
	var acct = document.getElementById(field_a).value + "-" + document.getElementById(field_b).value;
	document.getElementById("zipx_number").value = acct;
}
function testNumericOnly(value){
	if(isNaN(value)){
		return true;	
	}else return false;
}
function testAlphaNumericOnly(value){
	var numaric = value;
	for(var j=0; j<numaric.length; j++){
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123)){
		  }else{
			// alert("Your Alpha Numeric Test Failed");
			 return false;
		  }
	}
 	//alert("Your Alpha Numeric Test Passed");
 	return true;
}
function capitalize(eleId){
	var x=document.getElementById(eleId).value;
	document.getElementById(eleId).value=x.toUpperCase();
}


