//function DateCompare(): Checks two dates (a from date and a to date) to make sure the 
//to date is greater. The to date is variable t and the from date is variable f. Returns
//true if the dates are equal (Jay Silberkleit 7/28/00)
function DateCompare (f, t, message)
{
  var fromyear, frommonth, fromday;      //variables for the from stuff
  var toyear, tomonth, today;            //variables for the to stuff
  var mode, i, Flag;                           //utility variables
  
  fromyear = frommonth = fromday = "";   //reset from variables to nothing
  toyear   = tomonth   = today   = "";   //reset to variables to nothing
  mode = 1;                              //start mode at 1 (get the month)
  
  if (f.length == 0 && t.length == 0)    //make sure there is data in the fields
    return true;                         //there isn't, so just return true
  if ((f == " " && t == "") || (f=="" && t == " "))
    return true;
 
  for (i=0;i < f.length; i++)            //for loop to break down from date
    //check to see if current character is a number...
    if (f.charAt(i) == "-" || f.charAt(i) == "/" || f.charAt(i) == "\\")
      mode++;                            //...its not, increase mode and ignore
    else  
      switch (mode) {                    //...it is, so begin building strings
      case 1:                            //build a month string
        frommonth += f.charAt(i);
        break;
      case 2:                            //build a day string
        fromday   += f.charAt(i);
        break;
      case 3:                            //build a year string
        fromyear  += f.charAt(i);
        break;
      }
    
  for (i=0, mode=1;i < t.length; i++)    //for loop to break down to date
    //check current char to see if it is a number...
    if (t.charAt(i) == "-" || t.charAt(i) == "/" || t.charAt(i) == "\\")
      mode++;                            //...it's not so ignore and increase mode
    else                                 
      switch (mode) {                    //...it is so build strings
      case 1:                            //build month string
        tomonth += t.charAt(i);
        break;
      case 2:                            //build day string
        today   += t.charAt(i);
        break;
      case 3:                            //build year string
        toyear  += t.charAt(i);
        break;
      }
    
  //error checking on dates
  if (tomonth   > 12 || tomonth   < 1) Flag = 1;
  if (frommonth > 12 || frommonth < 1) Flag = 1;
  if (tomonth   != 2 && (today     > 31 || today     < 1)) Flag = 1;
  if (frommonth != 2 && (frommonth > 31 || frommonth < 1)) Flag = 1;
  if (tomonth   == 2 && (today     > 29 || today     < 1)) Flag = 1;
  if (frommonth == 2 && (frommonth > 29 || frommonth < 1)) Flag = 1;
  if (Flag == 1) {
    return false;
  }
  //compare dates to make sure the to date is greater than the from 
  if (toyear >=  fromyear                                           ) return true;
  if (toyear == fromyear && tomonth >= frommonth                    ) return true;
  if (toyear == fromyear && tomonth == frommonth && today >= fromday) return true;
  //errmessage += message + "\n";
  return false;
}
function  frmUser_onSubmit ()
{	
	if((document.Group_enroll.Password.value=="")||(document.Group_enroll.Repassword.value=="") || (document.Group_enroll.UserName.value==""))
	{
		alert("Bad login information entered !");
		return false;
		//self.location=this.back
	}
	else
	{	
	if(document.Group_enroll.Password.value!=document.Group_enroll.Repassword.value)
	{
		alert("Re-enter passwords !");
		return false;
		//self.location=this.back
	}
	else
	{	
		document.Group_enroll.submit();
		return true;
		//alert("hi");
		
	}
	}
}


function isZipCode (s,message){
	if (s.value.length >= 5){
			return true;
	}

	errmessage = errmessage + message + "\n" ;
	s.focus();
	return false;
}

function checkDate(s,m,message){
	// s and m are  strings  for  month and year
	// this function checks if the month  and year are  valid
	if( (isInteger(s.value,message)) && (isInteger(m.value,message)) && ((s.value >"0")  && (s.value < "13"))){
		return true;
	}
	else {
		errmessage = errmessage + message + "\n";	
		return false;
	}
}

//Function CheckExpDate works now: Added more advanced checking like
//# of digits in the month and year, makes both year and month numbers,
//automaticly adds a zero to one digit months, and checks for out of range
//values (JS 7/25/00)
function checkExpDate (s, m,message){
	// s and m are strings for  month and year respectively
	// this function finds out if the chosen date is past date
	// message is a string
	var now;
	now = new Date();
	curmonth = now.getMonth()+1;
	curyear = now.getFullYear();
	
	//**ADD ON- Formats the date fields to proper format before
	//** evaluating (JS 7/25/00)
	
	s.value = numericize(s.value);
	m.value = numericize(m.value);
	
	if (s.value.length == 1) {
	  Temp    = "0" + s.value;
	  s.value = Temp;
	}
	
	//Check for proper length of strings
	if (s.value.length != 2 || m.value.length != 4) {
	  errmessage = errmessage + "Invalid expiration date\n\n";	
	  return false;
	}
	if (s.value > 12 || s.value < 1) {
	  errmessage = errmessage + "Month out of range!\n\n";	
	  return false;
	}
	if (m.value < curyear) {
	 	 	errmessage = errmessage + "Expiration year should be in the future\n\n";	
			//alert("failing1");
			return false;
		}
	else if( (m.value <=curyear) && (s.value < curmonth) ){
		errmessage = errmessage + message + "\n";	
		//alert("failing2");
		return false;
		}
	//else if(m > curyear)
	//{
	return true;
	//}
}

function isInteger (s, message){
	// s is a string
	// message is a string
	var i;
	for (i = 0; i < s.length; i++){   
	    // Check that current character is number.
	    var c = s.charAt(i);
	    if (!isDigit(c)){
	 	 	errmessage = errmessage + message + "\n";	
			return false;
		}
	}
	return true;
}

function isDigit (c){
	// c is a character
	return ((c >= "0") && (c <= "9"))
}

function checkSS(s, message) {
  s.value=numericize(s.value)
  if (isInteger(s.value) && (s.value.length == 8 || s.value.length == 9)) {
    if (s.value.length == 8) {
      s.value  = "0" + s.value;
    }   
    return true;
  }
  errmessage = errmessage + message + "\n";
  return false;
}
   
function numericize(s){
	// s is a string
	// message is a string
	var i,j;
	j="";
	for (i=0;i<s.length;i++){
		if (isDigit(s.charAt(i)) && s.charAt(i) != " " && s.charAt(i) != "-"&& s.charAt(i) != "."){
			 j=j+s.charAt(i);
		}
	}
	return j;
}

function isBlank(s,message){
	// s is a form element
	// message is a string
	if ((s.value.length > 0) && (s.value != null) && (s.value != "")){
		return true;
	}
	errmessage = errmessage + message + "\n";
	s.focus();
	return false;
}

function isCCN(s,message){
	// s is a form element
	// message is a string
	//Uses loops instead of slice or substr because IE3 doesn't
	//support those methods.  -- Steve
	//var x,i;
	//alert("i am here");
	// Check blank
	//alert(s.value.length);
	

	s.value=numericize(s.value);
	//alert(s.value.length);
	if ((s.value.length >14)&&(s.value.length<22))
	{
		//alert("true1");
		
		//if (isInteger(s.value,message))
		//{
			//alert("true2");
			return true;
		//}
		//else{	//alert("false2");
		//	errmessage = errmessage + message + "\n";
		//	s.focus();
			//return false;
		    //}
	}
	
	else
	{
	//alert("false2");
	//alert(message);
	errmessage = errmessage + message + "\n";
	s.focus();
	return false;
	}
}

function isPhone (s,message){
	// s is a form element
	// message is a string
	// If non-numeric characters, strip them, then check for length
	
	s.value = numericize(s.value);
	
	
	//Check if first digit is 0 or 1. Invalid phone number.
	//if ((s.value.charAt(0) == '0') || (s.value.charAt(0) == '1')){
	//	errmessage = errmessage + message + "\n";
	//	s.focus();
	//	return false;
	//}
	
	//if (s.value.length == 7) {
	  //errmessage = errmessage + "Please specify an area code in phone/fax number\n\n";
	  //s.focus()
	  //return false;
	//}
	//Should be 3 digit area code + 7 digit phone number, as a 10 digit string
	//if (s.value.length == 10){
		//if (isInteger(s.value,message)) {
		  //return true;
		//} else {
		  //s.focus();
		  //return false;
	    //}
	//}
	
	//s.focus();
	//errmessage = errmessage + message + "\n";
	//return false;
		
}

function isEmail (s,message){
	// s is a form element
	// message is a string
	var i,ii;
	var j;
	var k,kk;
    var jj;
    var len;
    
    // Check blank
	//if (isBlank(s,message)){
	//	s.focus();
	//	return false;
	//}

    // Check valid email
    // Must have a "@" and a "." to be valid.
    // Must have at least 1 character before "@"
    // Must have at least 1 character after "@" and before "."
    // Must have at least 2 characters after "."
    if (s.value.length >0){
		i=s.value.indexOf("@");
		ii=s.value.indexOf("@",i+1);
		j=s.value.indexOf(".",i);
		k=s.value.indexOf(",");
		kk=s.value.indexOf(" ");
		jj=s.value.lastIndexOf(".")+1;
		len=s.value.length;
		if ((i>0) && (j>(1+1)) && (k==-1) && (ii==-1) && (kk==-1) &&
			(len-jj >=2) && (len-jj<=3)) {}
		else {
	 		 	errmessage = errmessage + message + "\n";
	 		 	s.focus();			
				return false;
		}
	}
    return true;
}

function isSelected (s,message){
	// s is a select form element
	// message is a string
	// Checks to see if a selectbox has been changed from its first value
	// Should be used on drop-boxes that have "Select an Item" or something
	// similar as their first OPTION
	
	//alert(s.selectedIndex +" *" + s.value + "*");
	if (s.selectedIndex==0 && !s.value){
		
		errmessage = errmessage + message + "\n";
		s.focus();
		return false;
	}
	return true;
}




//function isChecked (s,message){
	// s is a check/radio  form element
	// message is a string
	// Checks to see if a radio button has been chosen
//	if (!(s.checked == "true")) {
//		errmessage = errmessage + message + "\n";
//		return false;
//}
//	return true;
//}


function getBillingAddress(what){
	if(what.chkAddr.checked){
		what.groupbilladd1.value = what.groupadd1.value;
		what.groupbilladd2.value =what.groupadd2.value;
		what.groupbillcity.value = what.groupcity.value;
		
		what.groupbillzip.value = what.groupzip.value;
		what.groupbillstate.selectedIndex = what.groupstate.selectedIndex;
		
	}
	else{
		what.groupbilladd1.value ="";
		what.groupbilladd2.value ="";
		what.groupbillcity.value ="";
		what.groupbillstate.value = "";
		what.groupbillzip.value = "";
        }
}


function getBillingAddress2(what){
	if(what.chkAddr.checked){
		what.var2.value = what.var7.value;
		what.var3.value = what.var8.value;
		what.var4.value = what.var9.value;
		
		what.var6.value = what.var11.value;
		what.var5.selectedIndex = what.var10.selectedIndex;
		what.var5.value = what.var10.value;   //add by JS 8/1/00 to pass the value also
		
	}
	else{
		what.var2.value ="";
		what. var3.value ="";
		what.var4.value ="";
		what.var5.value = "";
		what.var6.value = "";
        }
}

//****************************************************************
//FORMAT FUNCTIONS FOR FORMATING PHONE NUMBERS AND SOCIAL SECURITY
//(Jay Silberkleit 7/18/00)
//****************************************************************

//function FormatPhone(): Formats a phone number and area code
//to (XXX)XXX-XXXX. Call with the form object. (JS 7/17/00)
//**(NOTE: RETURNS PHONE NUMBER AS A VALUE!)**
function FormatPhone(number)
{
  var i, temp = "(";
  
  //strip away everything but numbers
  number.value = numericize(number.value);
  //if length is still > 10 then return number unformatted
  if (number.value.length > 10) return number.value;
  //if there is no number, return a prototype
  if (number.value.length == 0) return "(   )   -";  
  
  for (i = 0; i < number.value.length; i++) {
    
    switch (i) {
    case 3:             //after 3 digits, insert ")"
      temp += ")";
      break;
    case 6:             //after 6 digits, insert "-"
      temp += "-";
      break;
    }
    temp += number.value.charAt(i);  //append next number
  } 
  return temp;                       //return formatted string
}

//function FormatSS(): Formats a social security number that is 
//nine (9) digits long (the database should store all social security
//numbers as 9 digits). Formats using XXX-XX-XXXX. Call with form 
//object.
//**(NOTE: RETURNS THE NEW SSN AS A VALUE!)**
//(JS 7/17/00)
function FormatSS(number)
{
  var i, temp;
  
  //strip all but numbers from number
  number.value = numericize(number.value);
  //if number is greater then 9, return unformatted
  if (number.value.length > 9) return number.value;
  
  temp = "";
  for (i=0; i < number.value.length; i++) {
    if (i == 3 || i == 5)              //after 3 and 5th digits...
      temp += "-";                     //...insert "-"
    temp += number.value.charAt(i);    //append next number
  }
  return temp;                         //return formated string
}  


function isTAT(tat,message)                        //By Nimesh 08/30/00
{
//alert(tat);
if ((tat.length>11) || (tat.length<11))
{	 errmessage = errmessage + message + "\n";
	return false;
}
else
{	return true;
}
}   

function isTaxID(s,message)       //Nimesh 08/30/00
{

if ((s.length>11) || (s.length<9))
{	 errmessage = errmessage + message + "\n";
	return false;
}
else
{	return true;
}

}

function isUPIN(s,message)       //Nimesh 08/30/00
{

if ((s.length<5) || (s.length>13))
{	 errmessage = errmessage + message + "\n";
	return false;
}
else
{	return true;
}

}

function LoginCheck(message)
{
	if((document.userFrm.password.value=="")||(document.userFrm.repassword.value=="") || (document.userFrm.login.value=="")||(document.userFrm.email.value==""))
	{
		errmessage = errmessage + message + "\n";
			return false;
	}
	else
	{	
	
		if(document.userFrm.password.value!=document.userFrm.repassword.value)
		{	
			errmessage = errmessage + message + "\n";
			return false;
		}
		else
		{
		return true;
		
		}
		
	}
	
}

function getBillingAddress2(what){
	if(what.chkAddr.checked){
		what.sfname.value=what.bfname.value;
	what.slname.value=what.blname.value;
	what.sadd1.value=what.badd1.value;
	what.sadd2.value=what.badd2.value;
	what.scity.value=what.bcity.value;
	//what.bstate.value=what.sstate.value;
	what.szip.value=what.bzip.value;
	what.state.selectedIndex = what.bstate.selectedIndex;
	what.state.value = what.bstate.value;
	}
	else{
	what.sfname.value="";
	what.slname.value="";
	what.sadd1.value="";
	what.sadd2.value="";
	what.scity.value="";
	what.state.value="";
	what.szip.value="";
	//what.bcountry.value="";
        }
}

function isSame(s,m,message)
{
	//alert(s.value);
	//alert(m.value);
	if (s.value!=m.value )
	{	
	errmessage = errmessage + message + "\n";
	return false;
	}
	else
	{
	return true;
	}
	
}


function isSelct_Payment(m,cno,mo,ye,pno,aupin,message)
{	
	var x;
	var cno,mo,ye,pno,aupin;
	x=parseInt(m);
	//alert(m+"--"+cno+"---"+mo+"--"+ye+"---"+pno+"---"+aupin);
	
	if (x<1)
	{
	 errmessage = errmessage + message + "\n";
	return false;
	}
	else
	{
		if(x==1)
		{
			//alert(ye.value, mo.value);
			var bool1,bool2
			if(isCCN(cno, "Invalid Credit card number.\n")&&checkExpDate(mo, ye,"Credit Card is expired.\n"))
			return true;
			else
			return false;
		}
		else 
		{
			if(x==2)
			{
		
			if (isBlank(pno,"Purchase order no is missing")&&isBlank(aupin,"Purchase order authorization pin is missing"))
			return true;
			else
			return false;
			}
			else
			{
			return true;
			}
		}
		
	}
}

function checknumber(s,message)
{
var x=s;
var anum=/(^\d+$)|(^\d+\.\d+$)/
	if (anum.test(x))
		return true
	else
	{
		errmessage = errmessage + message + "\n";
		return false
	}

}
