//*****************************************************************************
//client side validation
//*****************************************************************************

function validate( form, fieldsArray)
{

	var j;
	var berror
	var ie = false
	var serrormessage;
	
	ie=(document.all!=null)	
	serrormessage='';	
		
	for (j=0; j < fieldsArray.length ;j++) 
	 { 

			if (!fieldsArray[j])
			{
				break;
			}
			var tempobj=fieldsArray[j][0]
			var stext =fieldsArray[j][1]
							
				
				if (form.elements[tempobj].options)
				{ //combobox special case 
					var selectedIndex;
					
					selectedIndex = form.elements[tempobj].selectedIndex;
									
					if (form.elements[tempobj].options[selectedIndex].value == 0 )
					{	
						serrormessage = serrormessage +   stext + ' ' + 'cannot  be empty!' + '\n';	
						berror = true;		
					}	
				}
				else
				{

					if (form.elements[tempobj].value.length==0||form.elements[tempobj].value ==0)
					{	
						
						serrormessage = serrormessage +   stext + ' ' + 'cannot  be empty!' + '\n' ;	
			
						berror = true;		
					}	
				}
	}	
	
	
	printerror(serrormessage)
	
	return serrormessage;

}

function isInteger(arg)
{
	//parseInt returns NaN if its argument does not start with an integer
	//otherwise it returns the integer the argument starts with
	//for example parseInt("4ever") will return 4
	//If parseInts return value exactly matches its argument
	// then it contains ONLY an integer
	if ( parseInt(arg) == arg )
		return true
	else
		return false
}

function isFloat(arg)
{
	//See description of isInteger function above	
	if ( parseFloat(arg) == arg )
		return true
	else
		return false
}

function isPhoneNumber(PhoneNo)
{	
	if ( isInteger(PhoneNo) && (String(PhoneNo).length == 10) )	
		return true;
	else
		return false;	
}

//*****************************************************************************
//model year validation
//*****************************************************************************

function isModelYearValid( nyear )
{
	
	if( nyear > 1980 ) 
		return true;
	else
		return false;	
		
	
}


//*****************************************************************************
//list of existing client side errors
//*****************************************************************************
/***GETERROR BEGIN***/
//DO NOT REMOVE THIS TAG USED IN SETUP PROGRAM
function getError(nerrorNumber)
{

	var errors = new Array(87);

	errors[0] = 'Vehicle model year should be > 1980';
	errors[1] = 'Fleet information will be deleted';
	errors[2] = 'Parking flag should be set for parking locations';
	errors[3] = 'Parking location cannot be Fueling location, fueling flag will be unchecked';
	errors[4] = 'Password should be  8 characters';
	errors[5] = 'Password cannot contain spaces';
	errors[6] = 'Password must be alphanumeric - it must contain at least one number and one character';
	errors[7] = 'Username, must be at least 3 characters';
	errors[8] = 'Username cannot not allow any spaces.';
	errors[9] = 'Invalid password, please try again.';
	errors[10] = 'Cannot delete this vehicle model year because all vehicle options are attached to it.';
	errors[11] = 'field should be numeric';
	errors[12] = 'Cannot delete the last vehicle model option. The current model year will be removed from the vehicle model.';
	errors[13] = 'Cannot delete this chassis spec year because all chassis options are attached to it.';
	errors[14] = 'Cannot delete this chassis option because all chassis spec are attached to it.';
	errors[15] = 'Cannot delete this vehicle model because all chassis spec are attached to it.';
	errors[16] = 'Cannot delete this equipment spec year because all equipment options are attached to it.';
	errors[17] = 'Cannot delete this Equipment model year because all equipment options are attached to it.';
	errors[18] = 'All changes made will be lost.';
	errors[19] = 'Cannot delete this equipment model because all equipment spec are attached to it.';
	errors[20] = 'Rate should be < 100 and > 0 for the percent rate type';
	errors[21] = 'Rate should be  > 0 for the dollar rate type';
	errors[22] = 'should be numeric';
	errors[23] = 'should be before';
	errors[24] = 'Hours should be  > 1 and <= 12.';
	errors[25] = 'Minutes should be > 0 and < = 59.';
	errors[26] = 'Service offering effective date should be > then contract effective date';
	errors[27] = 'Service offering expiry date should be < =  then contract expiry date';
	errors[28] = 'Number of vehicles in the batch is invalid.';
	errors[29] = 'Phone number must be digits in the form XXX-XXX-XXXX';
	errors[30] = 'Fax number must be digits in the form XXX-XXX-XXXX';
	errors[31] = 'Pager number must be digits in the form XXX-XXX-XXXX';
	errors[32] = 'Water Depth Read must be less than Fluid Depth Read';
	errors[33] = 'cannot be less than 0';
	errors[34] = 'ISO_NO or PST_NO should be set.';
	errors[35] = 'If the province of registration changes the lecense plate must be change as well.';
	errors[36] = 'Asset transfer will be approved.';
	errors[37] = 'Asset transfer will be unapproved.';
	errors[38] = 'Transfer will be removed.';
	errors[39] = 'Cannot set  service date before billing or last transfer date.';
	errors[40] = 'Cannot set  retirement date  before billing date or last asset transfer date';
	errors[41] = 'Retirement date  cannot be not be future dated.';
	errors[42] = 'Expiry Date or Valid Months fields should be specified.';
	errors[43]=	 'Cost center that does not have parent, should be billable.';
	errors[44]=	 'Advancement should be > =   0 or < = 100.';  
	errors[45] = 'Cannot set expiry date less then today';
	errors[46] = 'Cannot set effective date less then today';
	errors[47] = 'Assigned to vehicle does not match the vehicle owner.';
	errors[48] = 'From Date cannot be greater than To Date.';
	errors[49] = 'Delivery Date  should not be bigger then';
	errors[50] = 'At least one line item should exist for PO  before it can be saved.\n System will redirect you to Parts/Services screen.';
	errors[51] = 'Regular Hours available exceeded.';
	errors[52] = 'Is mandatory.';
	errors[53] = 'This Time Code (TC) requires VMRS entries.';
	errors[54]=  'field should be numeric and greater than zero';
	errors[55]=  'should be greater than zero';
	errors[56]=  'Vacation hours can only be 3.75 or 7.5 hours per day.';
	errors[57]=  'Vacation hours not allowed if AST hours exist.';
	errors[58]=  'AST hours not allowed if vacation hours exist.';
	errors[59]=  'Non REG entry(s) need to be deleted before REG entry can be deleted.';
	errors[59]=  'Non REG entry(s) need to be deleted before REG entry can be deleted.';
	errors[60]=  'AST entry cannot be deleted if OT hours exist.';
	errors[61]=  'AST entry not allowed if REG hours entered less than';
	errors[62]=  'OT entry not allowed if REG plus AST hours entered less than';
	errors[63]=  'Username and password cannot be empty';
	errors[64] = 'Are you sure you want to close Autovision?';
	errors[65] = 'Coming soon?';
	errors[66] = 'From Date cannot be greater than To Date'
	errors[67]=  'Absence hours cannot be greater than';
	errors[68]=  'Admin TS hours must be greater than zero.';
	errors[69]=  'TS hours must be in units of 0.25 (quarter) hours.';
	errors[70]=  'TS hours cannot be greater than 18.';
	errors[71]=  "Vehicle model year and all vehicle options will be deleted.";
	errors[72]=  "Model Year will be removed.";
	errors[73]=  "Equipment spec year, options and equipment models will be deleted.";
	errors[74]=  "Equipment Spec Year will be removed.";
	errors[75]=  "You have exceeded the allowable regular time for the day. \nPlease re-adjust your entry and try again.";
	errors[76]=  "Timesheet hours cannot exceed 24.";
	errors[77]=  "An External Asset Transfer exists.  Do you want to continue?";
	errors[78]=  "Are you sure that you want to delete customer asset transfer.?";
	errors[79]=  "Parking location or Postal code should be selected.";
	errors[80]=  'AST hours not allowed if TGP hours exist.';
	errors[81]=  'You must enter a customer reference on the work order screen first if you select any one of the following reason codes: Abuse, Accident, or Special Request.';
	errors[82]=  'Do you want to change expiry date for all contract service offerings.';
	errors[83]=  'Are you sure that you want to delete this task?';
	errors[84]=  'Service offering effective date should be greater then last billing date';
	errors[85]=  'Total TGP hours must be >= 1 hour.';
	errors[86]=  'Selection is unavailable. Please save your group first.';
	errors[87]=  'Are you sure that you want to delte this user.';
		

	return errors[nerrorNumber];
}
/***GETERROR END***/

//*****************************************************************************
//password validation
//will return error code
//*****************************************************************************
function validatePassword( s )
{
	var error
	
	error = 0;
	
	if (s.length < 8 )
	{
		error =   4;
	};
	
	if (s.indexOf(" ") > -1 )
	{
		error =  5;
	};
	
	if (numCheck(s))
	{
		error =  6;
	};
	
	return error;
}

//*****************************************************************************
//userid validation
//will return error code
//*****************************************************************************
function loginValidation(s)
{
	var error
	
	error = 0;
	
	if (s.length < 3 )
	{
		error =   7;
	};
	

	
	if (s.indexOf(" ") > -1 )
	{
		error =  8;
	};
	
	return error;	
}



//*****************************************************************************
//numcheck function
//will return true if arguments contains only 0-9
//*****************************************************************************

function numCheck(argvalue) 
{
  if (argvalue.length == 0)
    return false;

  for (var n = 0; n < argvalue.length; n++)
    if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
      return false;

  return true;

}

function isNumeric(strString)
   //  check for valid numeric strings	
  {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

	if (isEmpty(strString))
		blnResult = false;
	else	
	//  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;
   }

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function printerror( serror)
{
	
	
	if(!isEmpty(serror))
		alert(serror);
	
	
}	

function  isEmailAddress( emailIn )
{
	var  numAtChars;					//Number of '@' chars 
	var  userNameIn;					//part before the '@' 
	var  domainNameIn;					//part after the '@' 
	var  addressFields = new Array();	//parts of address split at '@'s	
	var  alertString = "";	

	addressFields = emailIn.split( '@' );
	numAtChars = addressFields.length - 1;

	if ( emailIn == "" )
		alertString += "is EMPTY.";

	else if ( numAtChars  ==  0 )
		alertString += "contains no \'@\' character.";

	else if ( numAtChars  >  1 )
		alertString += "contains " + numAtChars + " '@' characters.";

	else if ( addressFields[0] == "" )
		alertString += "has no Username before the \'@\' character.";

	else if ( addressFields[1] == "" )
		alertString += "has no Domain Name after the \'@\' character.";

	else
		{
		userNameIn   = addressFields[0];
		domainNameIn = addressFields[1];

		if ( userNameIn.indexOf( " " ) != -1 )
			alertString += "has one or more Spaces in the Username before the \'@\' character.";
		else if ( domainNameIn.indexOf( " " ) != -1 )
			alertString += "has one or more Spaces in the Domain Name after the \'@\' character.";

		else if ( isStandardDomain( domainNameIn ) == false )
			alertString += "does not end with a \'.com\' style domain or two letter country code domain.";

		} // Ends else from outer string of if-then-else's.

	return alertString;
}

/// This checks the string passed in as a domain name and requires that it
///   either end in one of the '.com' style endings or ends with '.cc' where the
///   'cc' represents two alphabetic characters of a country code domain.
/// cr#7378 4 character domain such as .coop
function  isStandardDomain( domainIn )
{

	
	var  isStandardReturn = false;													
	var  last5chars  =  domainIn.substring( domainIn.length-5, domainIn.length );   
	var  last4chars  =  domainIn.substring( domainIn.length-4, domainIn.length );   
	var  last3chars  =  domainIn.substring( domainIn.length-3, domainIn.length );  

	last4chars = last4chars.toUpperCase();	

	/// A regular expression pattern to match country code domains.
	///  In otherwords a Dot character followed by two alphabetic characters.
	var  countryCodePattern = /\.[a-zA-Z][a-zA-Z]/;
	var	companyCodePattern= /\.[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z]/;
	if      ( last4chars == ".COM" ) isStandardReturn = true;
	else if ( last4chars == ".EDU" ) isStandardReturn = true;
	else if ( last4chars == ".GOV" ) isStandardReturn = true;
	else if ( last4chars == ".NET" ) isStandardReturn = true;
	else if ( last4chars == ".MIL" ) isStandardReturn = true;
	else if ( last4chars == ".ORG" ) isStandardReturn = true;
	else if ( last3chars.search( countryCodePattern )   !=  -1 )
		isStandardReturn = true;
	else if ( last5chars.search( companyCodePattern )   !=  -1 )
		isStandardReturn = true;

	return  isStandardReturn;
}

function validateTime( ihour, iminutes)
{
	
	var ireturn = '';
	
	if (isEmpty(ihour)) 
	{
		ireturn = getError(24);
		
	}
	else
		if (isEmpty(iminutes)) 
		{
			
			ireturn = getError(25);
		}
		else
		
			if ( ihour < 1 || ihour >12)
				
				ireturn = getError(24);
			else
				if 	(iminutes < 0 || iminutes > 59 )
					ireturn = getError(25);
			
	
	return ireturn;	
}


function validateDates(ddate1, ddate2)
{
	
	if (Date.parse(ddate1) <= Date.parse(ddate2))
		return true;		
	else
		return false;

}

function ReplaceAll(str,strFind,strReplace)
{
  var returnStr = str;
  var start = returnStr.indexOf(strFind);
  while (start>=0)
  {
    returnStr = returnStr.substring(0,start) + strReplace + returnStr.substring(start+strFind.length,returnStr.length);
    start = returnStr.indexOf(strFind,start+strReplace.length);
  }
  return returnStr;
} 

