/*  Program name		: form_check.js
 	Program function	: general form checking client side js functions 	
	Author				: Arnold Bandwring
	Developed by	    : Daffodils Interactive
	
sample usage of some of the functions declared below are as follows:
<SCRIPT language=javascript src="./js/form_check.js"></SCRIPT>
<script language="javascript">
<!---  
function Validate(theform)
{
	if (isEmpty(theform.City.value)){    //this is text box validation example
  		alert("Please enter your City.\nNo spaces or blanks allowed.");
		theform.City.focus();
		return false; 
	}
		
	if(theform.int_country_id.selectedIndex == 0){  //this is dropdown validation example
	  alert("Please Select your Counrty");
	  theform.int_country_id.focus();
	  return false;	
	}	
	
	var retval = usernameCheck(theform.dealer_username.value) //sample username validation example
	if (retval == false){
		theform.dealer_username.focus();
  		return false;
	}	
	
	var retval = emailCheck(theform.email.value)  // email validation example
	if (retval == false){
		theform.email.focus();
  		return false;
	}	 
}	
--->
</script>	
--------------------------------------------------------------------*/
// function to confirm database setup
function confirm_setup(){
	if(confirm("Tables already exists.\nThis action will drop all tables and data and it CANNOT BE UNDONE. \nAre you sure that you want to run Setup again?")){
		return true;
	}
	return false;
}
// function to open a new window
function openWin(windowURL, windowName, windowFeatures){ 
	window.open(windowURL, windowName, windowFeatures);
	return false; 
}

// function to confirm record deletion
function confirmCleanUp(Link) {
   if (confirm("Are you sure you want to delete ?")) {
      location.href=Link;
   }
}

// to simulate the clicking of the browser back button
function fn_back(){
	window.history.back();
}	

// function to check YEAR with 4 digit
function isYear(snum)
{
	var reInteger = /^[1-9]\d{3}$/
	return reInteger.test(snum)
}
// function to check indiger field
function IsInteger(snum)
{
	var reInteger = /^\d+$/
    return reInteger.test(snum)
}

// function to check SSN US
function IsSSN(snum)
{
	var reSSN = /^(\d{3})(-)(\d{2})(-)(\d{4})$/	
    return reSSN.test(snum)
}

// function to check US phone - fomat 999-999-9999
function IsPhone(snum)
{
	var rePhone = /^(\d{3})(-)(\d{3})(-)(\d{4})$/	
    return rePhone.test(snum)
}

// Check whether string s is empty.
function isEmpty(s)
{   
	return ((s == null) || (s.length == 0) || (s.substr(0,1) == " "))
}

// function to check Price value ( with two decimal places )
function isPrice(s)
{  
	var reFloat = /^[\d\.]((\d+(\.\d{0,2})?)|((\d*\.)?\d{0,2}))$/	
	return reFloat.test(s)	   	
}
// function to check float/decimal fileds
function isFloat(s)
{  
	var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/	
	return reFloat.test(s)	   	
}

// function to check username fields
function usernameCheck (s)
{
  // this is done to avoid any special characters
  var reUsername = /^[a-zA-Z0-9_][a-zA-Z0-9_]*$/
  
  if (isEmpty(s))
  {
  	 alert("Please enter a desired Username. \nNo spaces, blanks or special characters , except '_'.");
	 return false; 
  } 
  else
     {
   	       var matchArray = s.match(reUsername); // is the format ok? 
			 if (matchArray == null) 
			 { 
				alert("Username should contain only \nalphabets, digits or underscore '_'.\n '_' should not be the first character.");
				return false; 
			 }	 	
			 else
			 { 
				/* if(s.length<8){		
					alert("Username should be minimum of 8 characters");	
					return false; 
			                 }	*/
             }			
     }      	 
  return true;
}
//validate the name


function usernameCheck1 (s)
{
  // this is done to avoid any special characters
  var reUsername = /^[a-zA-Z0-9][a-zA-Z0-9_]*$/
  
  if (isEmpty(s))
  {
  	 //alert("Please enter a desired Username. Minimum 8 characters.\nNo spaces, blanks or special characters , except '_'.");
	 return false; 
  } 
  else
     {
   	 var matchArray = s.match(reUsername); // is the format ok? 
	 if (matchArray == null) 
	 { 
		//alert("Username should contain only \nalphabets, digits or underscore '_'.\n '_' should not be the first character.");
		return false; 
	 }	 	
	 else
	 { 
	  	 if(s.length<8){		
			//alert("Username should be minimum of 8 characters");	
			return false; 
	 }	
    }			
}      	 
  return true;
}

// function to check username fields
function passwordCheck (s){
  // this is done to avoid any special characters
  var rePassword = /^[a-zA-Z0-9][a-zA-Z0-9~!@#$%^&*()]*$/
  
  if (isEmpty(s)){
  	 alert("Please enter a desired Password. Minimum 6 characters.\nNo spaces and blanks allowed.");
	 return false; 
  } 
  else{
   	 var matchArray = s.match(rePassword); // is the format ok? 
	 if (matchArray == null) { 
		alert("Password should contain only alphabets, digits\nor only these special characters ~!@#$%^&*()$\nbut not the first character.");
		return false; 
	 }	 	
	 else{ 
	  	 if(s.length<6){		
			alert("Password should be minimum of 6 characters");	
			return false; 
		 }	
	 }			
  }      	 
  return true;
}
function passwordCheck1 (s){
  // this is done to avoid any special characters
  var rePassword = /^[a-zA-Z0-9][a-zA-Z0-9~!@#$%^&*()]*$/
  
  if (isEmpty(s)){
  	 //alert("Please enter a desired Password. Minimum 8 characters.\nNo spaces and blanks allowed.");
	 return false; 
  } 
  else{
   	 var matchArray = s.match(rePassword); // is the format ok? 
	 if (matchArray == null) { 
		//alert("Password should contain only alphabets, digits\nor only these special characters ~!@#$%^&*()$\nbut not the first character.");
		return false; 
	 }	 	
	 else{ 
	  	 if(s.length<6){		
			//alert("Password should be minimum of 8 characters");	
			return false; 
		 }	
	 }			
  }      	 
  return true;
}
function postcode(s)
 {
			if(isEmpty(s))
					{
						return false;
					}
					else
					{ 
						if(IsInteger(s))
						 {
							var lnumval=s;
							var lnumlength=lnumval.length;
								if (lnumlength!=4)
								 {
									return false;
								 }
						 }
						 else
						 {
								return false;		
						 }
					}	 		
   return true;
}	
function confirmpassword(a,b)
 {
   if(a == b)
    {
	   return true;
	 }
	 else
	 {
	    return false;
	  }
	return true;
 }	  
	
// function to validate date field
function isValidDate(dateStr) { 
// Checks for the following valid date formats: 
// MM/DD/YY MM/DD/YYYY MM-DD-YY MM-DD-YYYY 
// Also separates date into month, day, and year variables 

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/; 

// To require a 4 digit year entry, use this line instead: 
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; 

var matchArray = dateStr.match(datePat); // is the format ok? 
if (matchArray == null) { 
	alert("Date is not in a valid format.") 
	return false; 
} 
month = matchArray[1]; // parse date into variables 
day = matchArray[3]; 
year = matchArray[4]; 
if (month < 1 || month > 12) { // check month range 
alert("Month must be between 1 and 12."); 
return false; 
} 
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31."); 
return false; 
} 
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!") 
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("February " + year + " doesn't have " + day + " days!"); 
return false; 
} 
} 
return true; // date is valid 
} 
// End date validation -------------------------------------> 
function AusPhone(snum)
{
	var totphonenum="";
	var totphonelen =snum.length;
	for ( var i=0;i<totphonelen;i++)
	{
		if (snum.substr(i,1) != " " )
		{
			//alert(snum.substr(i,1));
			totphonenum=totphonenum+snum.substr(i,1);
		}
		
	}
	totphonenum=Trim(totphonenum);
	//alert(totphonenum);
	var totphonelen =totphonenum.length;
	//alert(totphonelen);
	if(IsInteger(totphonenum)){
	if (totphonelen!=10){
		return false;
	}
	}else
	{
		return false;
	}
			
   return (totphonenum);
}
function LTrim(str)
{
  var whitespace = new String(" \t\n\r");

  var s = new String(str);

  if (whitespace.indexOf(s.charAt(0)) != -1) {
    // We have a string with leading blank(s)...

    var j=0, i = s.length;

    // Iterate from the far left of string until we
    // don't have any more whitespace...
    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
    j++;


    // Get the substring from the first non-whitespace
    // character to the end of the string...
    s = s.substring(j, i);
  }

  return s;
}

// --------------------------------------------------------------------------
// Remove trailing blanks from our string.

// I               str - the string we want to RTrim
// Return          the input string without any trailing whitespace

// Date            03 avr. 2003 23:13:50
// Author          Aurélien Tisné	(CS)
// --------------------------------------------------------------------------
function RTrim(str)
{
  // We don't want to trip JUST spaces, but also tabs,
  // line feeds, etc.  Add anything else you want to
  // "trim" here in Whitespace
  var whitespace = new String(" \t\n\r");

  var s = new String(str);

  if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
    // We have a string with trailing blank(s)...

    var i = s.length - 1;       // Get length of string

    // Iterate from the far right of string until we
    // don't have any more whitespace...
    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
      i--;


    // Get the substring from the front of the string to
    // where the last non-whitespace character is...
    s = s.substring(0, i+1);
  }

  return s;
}


// --------------------------------------------------------------------------
// Remove trailing and leading blanks from our string.

// I               str - the string we want to Trim
// Return          the trimmed input string

// Date            03 avr. 2003 23:15:09
// Author          Aurélien Tisné	(CS)
// --------------------------------------------------------------------------
function Trim(str)
{
  return RTrim(LTrim(str));
}
function emailCheck(emailStr) {
	/* 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)")
		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.")
	    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!")
			return false
		    }
	    }
	    return true
	}
	
	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		alert("The Domain Name doesn't seem to be valid.")
	    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 address must end in a three-letter domain, or two letter country.")
	   return false
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errStr="This E-mail address is missing a hostname!"
	   alert(errStr)
	   return false
	   }
	   // If we've gotten this far, everything's valid!
	return true	   
}
function emailCheck1(emailStr) {
	/* 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)")
		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.")
	    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!")
			return false
		    }
	    }
	    return true
	}
	
	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		//alert("The Domain Name doesn't seem to be valid.")
	    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 address must end in a three-letter domain, or two letter country.")
	   return false
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errStr="This E-mail address is missing a hostname!"
	  // alert(errStr)
	   return false
	   }
	   // If we've gotten this far, everything's valid!
	return true	   
}
/////////////
   function FormatNumber(num, decimalNum, bolLeadingZero, bolParens)
   /* IN - num:            the number to be formatted
           decimalNum:     the number of decimals after the digit
           bolLeadingZero: true / false to use leading zero
           bolParens:      true / false to use parenthesis for - num

      RETVAL - formatted number
   */
   {
       var tmpNum = num;

       // Return the right number of decimal places
       tmpNum *= Math.pow(10,decimalNum);
       tmpNum = Math.floor(tmpNum);
       tmpNum /= Math.pow(10,decimalNum);

       var tmpStr = new String(tmpNum);

       // See if we need to hack off a leading zero or not
       if (!bolLeadingZero && num < 1 && num > -1 && num !=0)
           if (num > 0)
               tmpStr = tmpStr.substring(1,tmpStr.length);
           else
               // Take out the minus sign out (start at 2)
               tmpStr = "-" + tmpStr.substring(2,tmpStr.length);                        


       // See if we need to put parenthesis around the number
       if (bolParens && num < 0)
           tmpStr = "(" + tmpStr.substring(1,tmpStr.length) + ")";


       return tmpStr;
   }


// end o
