//Contains validation routines and basic navigation and functionality
// All validation functions place the cursor in the 
// ** hasSelection(strFieldName,strMsg) tests if ddb selected option >0
// ** isEmpty(strFieldName,strMsg) checks if textbox not null (removes spaces to check)
// ** isEmptyOptional(strFieldName,strMsg) Reminds user that although optional it may be of benefit to complete
// ** isNumeric(strFieldName,strMsg) Checks that string is all numeric
// ** isNumericOptional(strFieldName,strMsg) Checks that string, if not NUL, is all numeric
// ** isEmail(strFieldName,strMsg) Checks that string has , no spaces, >6 long, 
//                    '.' exists & not >4 from end, '@' exists & not >5 from end.
// ** 
// ** 
//Open new window
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
//============================
//Load new window
function MM_moveBrWindow(theURL) { //v2.0
  window.location=theURL;
}
//============================
//Remaining test counter
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}
//============================================================
// ************* Validation section starts *******************
//============================================================
//===============Validate Car Leasing form=============
function validateForm(){
    if(!isEmpty('Your_Name','Full Name')){return false;}
    if(!isEmpty('EmailAddress','Email Address')){return false;}  
    if(!isEmail('EmailAddress','Email Address')){return false;}  
    if(!isEmpty('Town','Town / City')){return false;}  
    if(!isEmptyOneofThree('TelDay','Telephone Day','TelEve','Telephone Eve','Mobile','Mobile Number')){return false;}
    if(!isNumericEntered('TelDay','Telephone Day')){return false;}	
    if(!isNumericEntered('TelEve','Telephone Eve')){return false;}	
    if(!isNumericEntered('Mobile','Mobile Number')){return false;}	
    if(!isNumericEntered('Fax','Fax Number')){return false;}	
    if(!isEmpty('NoToCall','Best No To Call')){return false;}
    if(!isEmpty('Vehicles','Vehicles or Vehicles Required')){return false;}
}          

function validateForm2(){
    if(!isEmpty('Your_Name','Name')){return false;}
    if(!isEmpty('EmailAddress','Email Address')){return false;}  
    if(!isEmail('EmailAddress','Email Address')){return false;}  
    if(!isEmpty('Message','Message')){return false;}  
}          

//Validate the Select
function hasSelection(strFieldName,strMsg){
    var objFormField = document.forms[0].elements[strFieldName];
    if(objFormField.selectedIndex ==0)     {
         errors = "Please select a option for "+ strMsg ; 
         alert(errors); 
          objFormField.focus(); 
           return false; 
           }
    return true; 
}
//============================
//Validate Text Box
function isEmpty(strFieldName,strMsg){
    var objFormField = document.forms[0].elements[strFieldName];
    var strValue = objFormField.value;
    strValue = strValue.split(" ").join("")
    if(strValue.length < 1){
		  errors =  "Sorry, " + strMsg + " is required information.";
         alert(errors);
         objFormField.focus();
          return false;
         }
    return true;
}
// Validate isEmpty one of three values
function isEmptyOneofThree(strFieldName1,strMsg1,strFieldName2,strMsg2,strFieldName3,strMsg3){
    var numbersEntered = 0;
	var objFormField1 = document.forms[0].elements[strFieldName1];
    var strValue1 = objFormField1.value;
    strValue1 = strValue1.split(" ").join("")
    var objFormField2 = document.forms[0].elements[strFieldName2];
    var strValue2 = objFormField2.value;
    strValue2 = strValue2.split(" ").join("")
    var objFormField3 = document.forms[0].elements[strFieldName3];
    var strValue3 = objFormField3.value;
    strValue3 = strValue3.split(" ").join("")
    if(strValue1.length < 1 && strValue2.length < 1 && strValue3.length < 1 ){
		  errors =  "Sorry, at least one of " + strMsg1 + ", " + strMsg2 + ", " + strMsg3 + " is required information.";
         alert(errors);
         objFormField1.focus();
          return false;
         }
    return true;
}
//Validate Text Box Optional
function isEmptyOptional(strFieldName,strMsg){
    var objFormField = document.forms[0].elements[strFieldName];
    var strValue = objFormField.value;
    strValue = strValue.split(" ").join("")
    if(strValue.length<1){
		  errors =  strMsg + " isn't a required field\n\n "+ 
		  			"but completing it can help you.\n\n" +
					"________________________________\n\n" +
		  			"Are you sure you want to leave it empty?\n\n" +
					"________________________________\n\n" +
		  			"Select 'OK' to submit the form or\n\n" +
					"'Cancel' to complete " + strMsg;
         if (confirm(errors))
		 return true;
		 else{
         objFormField.focus();
          return false;
		  }
         }
    return true;
}
//===========================
//Validate Numerical field
function isNumeric(strFieldName,strMsg){
    var objFormField = document.forms[0].elements[strFieldName];
    var strValue = objFormField.value;
    strValue = strValue.split(" ").join("")
    strValue = strValue.split("-").join("")
    strValue = strValue.split(",").join("")
    if (isNaN(strValue)){
		  errors =  strMsg + " must contain only numbers.";
         alert(errors);
         objFormField.focus();
          return false;
         }
    return true;
}
//Validate Numerical field
function isNumericEntered(strFieldName,strMsg){
    var objFormField = document.forms[0].elements[strFieldName];
    var strValue = objFormField.value;
    strValue = strValue.split(" ").join("")
    strValue = strValue.split("-").join("")
    strValue = strValue.split(",").join("")
	if (strValue.length>0){
      if (isNaN(strValue)){
		  errors =  strMsg + " must contain only numbers.";
         alert(errors);
         objFormField.focus();
          return false;
         }
	}
    return true;
}
//Validate Numerical field
function isNumericOptional(strFieldName,strMsg){
    var objFormField = document.forms[0].elements[strFieldName];
    var strValue = objFormField.value;
    strValue = strValue.split(" ").join("")
    strValue = strValue.split("-").join("")
    if(strValue.length<1){ return true; }
	else{
    if (isNaN(strValue)){
		  errors = "Sorry, where entered " + strMsg + " must contain only numbers.";
         alert(errors);
         objFormField.focus();
          return false; } 
		}
    return true;
}
//==========================
//Validate Email  
function isEmail(strFieldName,strMsg){
    var objFormField = document.forms[0].elements[strFieldName]
    var strEmail = objFormField.value;
    var bolValid = true;
         if(strEmail.length < 7){ bolValid = false; }
         if(strEmail.lastIndexOf(" ") >0){ bolValid = false; }
    var intLastDot = strEmail.lastIndexOf(".")
         if(intLastDot == -1 ||  strEmail.length - intLastDot >4){ bolValid = false; }
    var intAt = strEmail.lastIndexOf("@")
         if(intAt == -1 || strEmail.length - intAt < 5){ bolValid = false; }
         if(!bolValid){ errors = "Please enter a valid email address."; 
         alert(errors);
          objFormField.focus();
         }
    return bolValid;
}
//==========================
