//------------------------------------------------------------------------------------------------------
// 											Author: Sohan Kabra
//------------------------------------------------------------------------------------------------------

function validateValues(validValues, actualValue)
{
	var allValid = true;
	for (i = 0;  i < actualValue.length;  i++)
	{
		ch = actualValue.charAt(i);

		for (j = 0;  j < validValues.length;  j++)
		{

		if (ch == validValues.charAt(j))
		break;
			if (j == (validValues.length-1))
			{
				allValid = false;
				break;
			}
		}
	}

	if (!allValid)
	{
		return false;
	}

	return true;
}

function compareDates()

{

   var str1  = document.getElementById("fromdate").value;
   var str2  = document.getElementById("todate").value;
   var dt1   = parseInt(str1.substring(0,2),10);
   var mon1  = parseInt(str1.substring(3,5),10);
   var yr1   = parseInt(str1.substring(6,10),10);
   var dt2   = parseInt(str2.substring(0,2),10);
   var mon2  = parseInt(str2.substring(3,5),10);
   var yr2   = parseInt(str2.substring(6,10),10);
   var fromDate = new Date(yr1, mon1, dt1);
   var toDate = new Date(yr2, mon2, dt2);
   if(toDate < fromDate)
   {
      alert("To date cannot be less than from date");
      return false;
   }else {
      return true;
   }

}

function validateDate(fld) {
    var RegExPattern = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
    var errorMessage = 'Please enter valid Date.\nFormat should be dd/mm/yyyy.';
    if ((fld.value.match(RegExPattern)) && (fld.value!='')) {
		return checkDateLength(fld);
    } else {
        alert(errorMessage);
        fld.focus();
        return false;
    } 
}

function checkDateLength(fld){

	if(fld.value.length < 10 ){
		alert('Please enter Date with valid length (10 characters), dd/mm/yyyy.');
		fld.focus();
		return false;
	}
	return true;
}

function doDateCheck(from, to) {
if (Date.parse(from.value) <= Date.parse(to.value)) {
alert("The dates are valid.");
}
else {
if (from.value == "" || to.value == "") 
alert("Both dates must be entered.");
else 
alert("To date must occur after the from date.");
   }
}



//used in copping physical address to postal address/shipping adress ect..
///start code
var address1 = "";
var address2 = "";
var address3 = "";
var address4 = "";
var postalCode = "";

function InitSaveVariables(form) {
	address1 = form.address1.value;
	address2 = form.address2.value;
	address3 = form.address3.value;
	address4 = form.address4.value;
	postalCode = form.postalCode.value;
}

function postalToPhysical(form) {
	if (form.sameAddress.checked) {
		InitSaveVariables(form);
		form.address1.value = form.flatOfficeNumber.value + " " + form.flatZone.value;
		form.address2.value =  form.streetNumber.value + " " + form.streetName.value;
		form.address3.value = form.suburb.value;
		form.address4.value = form.city.value;
		form.postalCode.value = form.physicalPostalCode.value;
	}
	else {
		form.address1.value = address1;
		form.address2.value = address2;
		form.address3.value = address3;
		form.address4.value = address4;
		form.postalCode.value = postalCode;
	}
}
//end code

function validateIDNumber(elem,idType, min, max){

	var type = idType.value;

	if(type == "IDNumber"){
		if(!lengthRestriction(elem, min, max,'invalid length of ID number')){
			return false;
		}else if(!isNumeric(elem, 'ID number is not numeric')){
			return false;
		}
	}
	return true;
}

// If the element's string matches the regular expression it is all numbers
function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}


function lengthRestriction(elem, min, max,errorMsg){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert(errorMsg);
		elem.focus();
		return false;
	}
}


function validateContactNumber(elem, min, max){

	if(!lengthRestriction(elem, min, max,'invalid length of contact number')){
		return false;
	}else if(!isNumeric(elem, 'contact number is not numeric')){
		return false;
	}
	return true;
}

function validateFaxNumber(elem, min, max){
	
	if(!lengthRestriction(elem, min, max,'invalid length of fax number')){
		return false;
	}else if(!isNumeric(elem, 'fax number is not numeric')){
		return false;
	}
	return true;
}


function validateCardVerifyCode(elem, min, max){
	if(!lengthRestriction(elem, min, max,'Invalid Credit Card Verify Code!\n Please enter numeric number with 3 digits.')){
		return false;
	}else if(!isNumeric(elem, 'Card verify code is not numeric')){
		return false;
	}
	return true;
}

function validateBankBranchCode(elem, min, max){
	if(!lengthRestriction(elem, min, max,'Invalid Branch Code!\n Please enter number with not more than 6 digits.')){
		return false;
	}else if(!isNumeric(elem, 'Branch Code is not numeric')){
		return false;
	}
	return true;
}




function validatePostalCode(elem, min, max){
	if(!lengthRestriction(elem, min, max,'Invalid Postal Code!\n Please enter numeric number with 4 digits.')){
		return false;
	}else if(!isNumeric(elem, 'Postal code is not numeric')){
		return false;
	}
	return true;
}



//Start of email check method

function emailCheck (form) {

//var emailStr = registerBizUserForm.emailAddress.value;
var emailStr = form.txtEmailAddress.value;

/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Email address seems incorrect (check @ and .'s)")
	form.txtEmailAddress.focus()
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("The username doesn't seem to be valid.")
    form.txtEmailAddress.focus()	
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address is invalid!")
		  form.txtEmailAddress.focus()	
		return false
	    }
    }
    return true
}

// Domain is symbolic name
//var domainArray=domain.match(domainPat)
//if (domainArray==null) {
	//("The domain name doesn't seem to be valid.")
	//registerBizUserForm.emailAddress.focus()
   // return false
//}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   alert("The email address must end in a three-letter domain, or two letter country.")
   form.txtEmailAddress.focus()
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   alert(errStr)
   form.txtEmailAddress.focus()
   return false
}

// If we've gotten this far, everything's valid!
return true;
}

//End of meail check method 



function validateRadioButton()
{
	// set var radio_choice to false
	var radio_choice = false;
	
	// Loop from zero to the one minus the number of radio button selections
	for (counter = 0; counter <registerBizUserForm.businessSubType.length; counter++)
	{
	// If a radio button has been selected it will return true
	// (If not it will return false)
	if (registerBizUserForm.businessSubType[counter].checked)
		radio_choice = true; 
	}
	
	if (!radio_choice)
	{
		// If there were no selections made display an alert box 
		alert("Please select a BusinessType.")
		return (false);
	}
	return (true);
}


function textLimit(field, maxlen) {
	if (field.value.length > maxlen + 1)
		alert('your input has been truncated!');
	if (field.value.length > maxlen)
		field.value = field.value.substring(0, maxlen);
} 

















