var defaultEmptyOK = false;
var invalid = "";

var mPrefix = "Please enter a value into the "
var mSuffix = " field."

var iLengthPrefix = "This field value should be less than "
var iLengthSuffix = " characters."
var iEmail = "Please enter valid email address."

var digitsInZIPCode1 = 5
var digitsInZIPCode2 = 9

var reWhitespace = /^\s+$/
var reAlphabetic = /^[a-zA-Z]+$/
var reInteger = /^\d+$/
//var reEmail = /^.+\@.+\..+$/
var reEmail = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/

function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

function isWhitespace (s) {
	return (isEmpty(s) || reWhitespace.test(s));
}

function isValidLength(theField,no) {
	if (theField.value.length > no) {
	   	theField.focus()
		theField.select()
	    	alert(iLengthPrefix + no + iLengthSuffix)
	    	return false
	}
	else return true;
}

function isValidChars (theField, s) {
	if (!reAlphabetic.test(theField.value)) {
		theField.focus()
	    	alert(s + " field contains invalid characters. Numbers, spaces and special characters are not allowed.");
	    	return false
	}
	return true;
}

function isInteger (s)
{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);
    return reInteger.test(s)
}

function warnEmpty (theField, s) {
	theField.focus()
    	alert(mPrefix + s + mSuffix)
    	return false
}

function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}

function checkString (theField, s, emptyOK,maxChars) {  
		if (emptyOK == 'N') emptyOK = defaultEmptyOK;
    	if ((emptyOK == 'Y') && (isEmpty(theField.value))) return true;
    	if (isWhitespace(theField.value)) {
    		if (warnEmpty (theField, s)) return true;
    		else return false;
    	}
    	else {
    		if (maxChars > 0){
    			if (isValidLength(theField,maxChars)) return true
    			else return false;
    			return false;
    		}
    		else return true;
    	}
}

function checkAlphaString (theField, s, emptyOK,maxChars) {  
    	if (emptyOK == 'N') emptyOK = defaultEmptyOK;
    	if ((emptyOK == 'Y') && (isEmpty(theField.value))) return true;
    	if (isWhitespace(theField.value)) {
    		if (warnEmpty (theField, s)) return true;
    		else return false;
    	}
    	else {
    		if (maxChars > 0){
    			if (!isValidLength(theField,maxChars)) return true
    			else {
    				if (isValidChars(theField,s)) return true
    				else 
    					return false;
    			}
    		}
    		else return true;
    	}
}

function checkZipCode (theField, s, emptyOK,minChars,maxChars) {  
	if (emptyOK == 'N') emptyOK = defaultEmptyOK;
    	if ((emptyOK == 'Y') && (isEmpty(theField.value))) return true;
    	if (isWhitespace(theField.value)) {
    		if (warnEmpty (theField, s)) return true;
    		else return false;
    	}
		else alert('NOT Empty')
}

function isZIPCode (theField,s,emptyOK,maxChars) {  
	if (emptyOK == 'N') emptyOK = defaultEmptyOK;
    	if ((emptyOK == 'Y') && (isEmpty(theField.value))) return true;
    	if (isWhitespace(theField.value)) {
    		if (warnEmpty (theField, s)) return true;
    		else return false;
    	}
	if (isInteger(theField.value) && ((theField.value.length >= digitsInZIPCode1) && (theField.value.length <= digitsInZIPCode2))) {
		return true;
	}
	else {
		theField.focus();
		alert(s + " field contains invalid characters or " + s + " field length must be between 5 to 9 digits");
		return false;
	}
}

function checkPhone(theField,s,emptyOK,maxChars){
	if (emptyOK == 'N') emptyOK = defaultEmptyOK;
    	if ((emptyOK == 'Y') && (isEmpty(theField.value))) return true;
    	if (isWhitespace(theField.value)) {
    		if (warnEmpty (theField, s)) return true;
    		else return false;
    	}
	if(isInteger(theField.value)) {
		if (theField.value==0) {
			alert('Please enter valid '+s+' Number.');
			theField.focus();
		}
		else
			return true;
	}
	else {
		theField.focus();
		alert('Please enter valid '+s+' Number.');
	}
}

function checkNumeric(theField,s,emptyOK){
	if (emptyOK == 'N') emptyOK = defaultEmptyOK;
    	if ((emptyOK == 'Y') && (isEmpty(theField.value))) return true;
    	if (isWhitespace(theField.value)) {
    		if (warnEmpty (theField, s)) return true;
    		else return false;
    	}
	if(isInteger(theField.value)) {
		return true;
	}
	else {
		theField.focus();
		alert('Please enter valid numbers.');
		return false;
	}
}

function isPhone(Fareacode,Fprefix,Fbody){

  if (Fareacode.value.length == 0 || Fprefix.value.length == 0 || Fbody.value.length == 0)
   {
	alert('Please enter the Phone Number');
	Fareacode.focus();
        return false;
   }
  else  
  {
  	if (checkNumeric(Fareacode,Fareacode.name,'N')) 
  		if (checkNumeric(Fprefix,Fprefix.name,'N'))
  			if (checkNumeric(Fbody,Fprefix.name,'N'))
  	       return true;	
  	else
               return false;	 
          
  }
   
}

function matchPasswords(passwordField,confirmField) 
{
	if (checkString(passwordField,'Password','N',15))
	{
	  if (checkString(confirmField,'Confirm Password','N',15))
	   {	
	  	if (passwordField.value==confirmField.value)
			return true
		else {
			alert('Password and Confirm Password doesnot match. Please enter same Password.');
			confirmField.focus();
		     }
	   }
	 }
}

function isEmail (s)

{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
    
    else {
       return reEmail.test(s)
    }
}

function checkEmail (theField, emptyOK)
{   if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isEmail(theField.value, false)) 
       return warnInvalid (theField, iEmail);
    else return true;
}

function populate(strFrmField1,strFrmField2) {	
	var  iLoop;
	for (iLoop=0; iLoop<strFrmField1.length;iLoop++) {
		if (strFrmField1.options[iLoop].value==strFrmField2) {
			strFrmField1.options[iLoop].selected=true;
		}
	}
}

function isDate(dateStr) {

    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        alert("Please select valid date.");
        return false;
    }

    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        alert("Please select valid date.");
        return false;
    }

    if (day < 1 || day > 31) {
        alert("Please select valid date.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        alert("Please select valid date.")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            alert("Please select valid date.");
            return false;
        }
    }
    return true; // date is valid
}

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

//Function to add thousand separator for a number
//Created by Sachin Joshi on May 28, 2005
function ThouSeparator(nStr, inD, outD, sep)
{
	nStr += '';
	var dpos = nStr.indexOf(inD);
	var nStrEnd = '';
	if (dpos != -1) {
		nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
		nStr = nStr.substring(0, dpos);
	}
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) {
		nStr = nStr.replace(rgx, '$1' + sep + '$2');
	}
	return nStr + nStrEnd;
}

//Function to round the number to two decimal places.
//Created by Sachin Joshi on May 28, 2005
function Round(num, precision) 
{ 
	var strZeros;

	//convert num to string 
	num = "" + num 
	precision = parseInt(precision); 

	var fullNum = "" + Math.round(num * Math.pow(10, precision)); 

	//Looking for the decimal point 
	var decPoint = fullNum.length - precision; 

	if(decPoint > 0) 
	{ 
		//Merging the left and right part to the decimal together 
		result = fullNum.substring(0, decPoint); 
		result += "."; 
		result += fullNum.substring(decPoint, fullNum.length); 
	} 
	else 
	{ 
		result = fullNum;
		if ((num.charAt(0) == "0") && (num.indexOf(".") > 0))
		{
			strZeros= "0.";
			for(i=2;i<num.length-1;i++)
			{
				if ((num.charAt(i) == "0") && (i <= precision))
				strZeros += "0";
			}
			result = strZeros + result;
		}
	} 
	return result; 
}