// Validation of form on client 
// For form: LogForm.asp
// Burntsand, L. Dion, Feb 28, 2000 
// Modified by SynchroSERV Inc., Simona C., Jan 2002


FieldErrMsg = new Array ();				// Save error messsage for each field if an error is found
FieldLabel = new Array ();				// Display label for field where error occured	
StateIncorrectFields = false;			// Set to true if one or more of the input fields is/are incorrect

// Global variables to fill out 
TotFieldToValidate = 7;					// Total field to be validated
strFormName = "frmLog";

// Fill in references for input field that are required to be validate BEFORE POSTING
// This information is used before the text of the error to indicate in which field 
// and error occured
FieldLabel[1] = "Building Name or Address";
FieldLabel[2] = "City";
FieldLabel[3] = "Contact Method";
FieldLabel[4] = "Contact";
FieldLabel[5] = "Phone";
FieldLabel[6] = "Email";
FieldLabel[7] = "Fax";

// end of global variables


function ValidateInput(){	
	StateIncorrectFields = false;	
	for (x = 1; x <= TotFieldToValidate; x++) {
		FieldErrMsg[x] = "OK";
	} 	

// Call functions to do the form validation on required input
// Check Biliding Name
  TxtInput = TestHasInput (1, "building_name"); 
  if (TxtInput != false) {
	  TxtLen = TestMinLenght(1, "building_name", 2); 
	   // if (TxtLen != false) {
	  //		TestAllLetters(1, "question"); 
		//}
  } 
TxtInput = TestHasInput (2, "city"); 
  if (TxtInput != false) {
	  TxtLen = TestMinLenght(2, "city", 2); 
	    if (TxtLen != false) {
	  		TestAllLetters(2, "city"); 
		}
  } 
Value_contact = TestButtonSelected(3, "contact", 2);

   // IF email or fax button is checked, validate input
  // How to Contact Method	
  // If a radio button from the group contact_method is selected, check corresponding input field
  
 if (Value_contact ==  "Yes") {
  Value_contact_method = TestButtonSelected (4, "contact_method", 3);    
  switch (Value_contact_method) { 
   
  case "Phone":		
	 	TxtContactMethod = 	TestHasInput(5, "contact_phone_area");
		if (TxtContactMethod != false) {
			LenContactMethod = TestMinLenght(5, "contact_phone_area", 3);				
			if (LenContactMethod != false) {
				ChkFax = TestIsPhoneFax (5, "contact_phone_area");
				if (ChkFax != false) {	 	
					TestNumberCount(5, "contact_phone_area", 3)
				}	
			}
		}
		TxtContactMethod = 	TestHasInput(5, "contact_phone_no");
		if (TxtContactMethod != false) {
			LenContactMethod = TestMinLenght(5, "contact_phone_no", 7);				
			if (LenContactMethod != false) {
				ChkFax = TestIsPhoneFax (5, "contact_phone_no");
				if (ChkFax != false) {	 	
					TestNumberCount(5, "contact_phone_no", 7)
				}	

			}
		}		
	break;
   
	case "Email":		
	 	TxtContactMethod = TestHasInput(6, "contact_email");	 		
  	 	if (TxtContactMethod != false) {			
			LenContactMethod = TestMinLenght(6, "contact_email", 8);
	 		if (LenContactMethod != false) {
	 			ChkEmail = TestIsEmail (6, "contact_email");
	 		}
	 	}
   break;

  	case "Fax":
		TxtContactMethod = 	TestHasInput(7, "contact_fax_area");
		if (TxtContactMethod != false) {
			LenContactMethod = TestMinLenght(7, "contact_fax_area", 3);				
			if (LenContactMethod != false) {
				ChkFax = TestIsPhoneFax (7, "contact_fax_area");
				if (ChkFax != false) {	 	
					TestNumberCount(7, "contact_fax_area", 3)
				}	
			}
		}
		TxtContactMethod = 	TestHasInput(7, "contact_fax_no");
		if (TxtContactMethod != false) {
			LenContactMethod = TestMinLenght(7, "contact_fax_no", 7);				
			if (LenContactMethod != false) {
				ChkFax = TestIsPhoneFax (7, "contact_fax_no");
				if (ChkFax != false) {	 	
					TestNumberCount(7, "contact_fax_no", 7)
				}	

			}
		}		
	break;
  } // end switch 
 }
 // if found any errors - build error message and display it in alert message for client
	if (StateIncorrectFields == false) { 		
		document.forms[strFormName].submit("POST", "LogForm.asp");
	} else {	
		strErrorMsg = "Please supply the correct information so we can help you with your request." + "\n" + "Fill in the following field(s): \n";
		//strErrorMsg = "Please check the following fields:" + "\n";
		for (x=1; x <= TotFieldToValidate; x++) {
			if (FieldErrMsg[x] != "OK") {
				strErrorMsg = strErrorMsg + FieldErrMsg[x];
			}		
		}		
		alert (strErrorMsg);
	} 
	return false;
} // end function

// Parameters: index=index of field to validate, grpName = name of radio buttons group, NoItemsInGrp=Total radio buttons in group
function TestButtonSelected (index, grpName, NoItemsInGrp) {
	RadioElementChecked = new Array ();
	RadioElementsValue = new Array ();
	RadioElementToCheckValue = "";
	FlagFoundSelected = false;
	
	// Test to see if any radio button was selected
	for (x=0; x < NoItemsInGrp; x++) {	
		RadioElementsValue[x] = document.forms[strFormName].elements[grpName][x].checked;		
		if (RadioElementsValue[x] == true) {
			RadioElementToCheckValue = document.forms[strFormName].elements[grpName][x].value;
			FlagFoundSelected = true;			
		} // end if
	} // end for
	
	if (FlagFoundSelected == true) {	
		   return RadioElementToCheckValue;		   
	} else {
			StateIncorrectFields = true;
			FieldErrMsg[index] = FieldLabel[index] + ": select a contact method and fill in the corresponding contact information." + "\n";		
			return "none_selected";
	} // end for
} // end function

function TestOptionalInput (index, fldname) {
strFieldValue = document.forms[strFormName].elements[fldname].value;		
	if (strFieldValue == "") {		
		return false;
	} else {
		return true;
	}
}

// Parameters fldname=name property of field to validate
// Test to see if field is empty
function TestHasInput (index, fldname) {	
	strFieldValue = document.forms[strFormName].elements[fldname].value;		
	if (strFieldValue == "") {		
		StateIncorrectFields = true;
		FieldErrMsg[index] = FieldLabel[index] + "\n";
		return false;
	} else {
		//return true;
	}
} // end function

// Parameters fldname=name property of field to validate
// Test to see if field is empty
function TestHasInput2 (index, fldname) {	
	strFieldValue = document.forms[strFormName].elements[fldname].value;		
	//return (strFieldValue != "");
	if (strFieldValue == "") {		
		return false;
	} else {
		return true;
	}
} // end function

// Parameters fldname=name property of field to validate, minlenght=minimum characters required for input
// Test minimum Lenght
function TestMinLenght (index, fldname, minlenght) {
	strFieldValue = document.forms[strFormName].elements[fldname].value;						
	intItemLen = strFieldValue.length;	
	if (intItemLen < minlenght)  {
		StateIncorrectFields = true;		
		FieldErrMsg[index] = FieldLabel[index] + "\n";
		return false;
	} else {
		return true;	
	} // end if	
} // end function

// Parameters fldname=name property of field to validate
// Test for repeat sequence ex: "aa"

function TestLetterSequence (index, fldname) {	
	strFieldValue = document.forms[strFormName].elements[fldname].value;
	intItemLen = strFieldValue.length;	
	if (intItemLen == 2) {
		tmpFirstChac = strFieldValue.charAt(0);
		tmpSecondChac = strFieldValue.charAt(1);
		if (tmpFirstChac == tmpSecondChac) {
			StateIncorrectFields = true;
			FieldErrMsg[index] = FieldLabel[index] + "\n";
		} 	
	}
	
	if (intItemLen > 2) {	
		tmpFirstChac = strFieldValue.charAt(0);
		tmpSecondChac = strFieldValue.charAt(1);
		tmpThirdChac = strFieldValue.charAt(2);
		if ((tmpFirstChac == tmpSecondChac) && (tmpThirdChac == tmpFirstChac)) {				
			StateIncorrectFields = true;				
			FieldErrMsg[index] = FieldLabel[index] + "\n";
		}		
	}	
} // end function

// test fields for all letters
function TestAllLetters(index, fldname) {	
	strFieldValue = document.forms[strFormName].elements[fldname].value;
	var StateIncorrectFields = false;
	var iChars = "*|,\":<>[]{}\;()@&$#%1234567890";
	for (var i = 0; i < strFieldValue.length; i++) {
     	if (iChars.indexOf(strFieldValue.charAt(i)) != -1) {     
		 	StateIncorrectFields = true;
	  	} // end if
    } // end for
	if (StateIncorrectFields == true) {				
		FieldErrMsg[index] = FieldLabel[index] + "\n";
	}			
} // end function


function TestIsEmail(index, fldname){
		// Look for @ sign in email
	var FlagChacDot = false;
	var FlagChacAt = false;	
	var strEmail = document.forms[strFormName].elements[fldname].value;		
		for (var i = 0; i < strEmail.length; i++ ){
			var c = strEmail.charAt(i);
			if (c == "@") FlagChacAt = true;
			if (c == ".") FlagChacDot = true;
		} 
		if ((FlagChacAt == true) && (FlagChacDot == true)){
			return true;
		} else {					
			StateIncorrectFields = true;
			FieldErrMsg[index] = FieldLabel[index] + + "\n";
			return false;
		} // end if
} // end function

function TestIsPhoneFax(index, namefield) {
	var FlagIsPhoneFax = true;
	var strInvalidChar = "";
	var strMsg = "";
	var strValueCheck = document.forms[strFormName].elements[namefield].value;					
	var Str2 = strValueCheck;
	var iChars = "abcdefghijklmnopqrstuvwxyz*|,\":<>[]{}`\';@&$#%()";
	for (var i = 0; i < Str2.length; i++) {
      	if (iChars.indexOf(Str2.charAt(i)) != -1) {  		   
			//strInvalidChar = strInvalidChar + Str2.substr(i, 1);
			FlagIsPhoneFax = false;			
		} 
	}	
	if (FlagIsPhoneFax == true) {
		return true;
	} else {	
		StateIncorrectFields = true;
		//strMsg = FieldLabel[index] + "  Invalid character(s): " + strInvalidChar + "\n";
		strMsg = FieldLabel[index] + "\n";
		FieldErrMsg[index] = strMsg;
		return false;		
	} 
} // end function               

function isAllLetters(IndexRefInputValue) {
	var FlagFoundError = false;
	var Str3 = RefInputValue[IndexRefInputValue]
	var iChars = "*|,\":<>[]{}\;()@&$#%1234567890";
	for (var i = 0; i < Str3.length; i++) {
     	if (iChars.indexOf(Str3.charAt(i)) != -1) {     
		 	FlagFoundError = true;
	  	} // end if
    } // end for
	
	if (FlagFoundError == true){
		return true;
	} else {
		StateIncorrectFields = true;
		return false;
	} // end if
} // end function                     

function isAllNumbers(ValueOfInputField) {
	var FlagIsNumber = false;
	var Str2 = ValueOfInputField;
	var iChars = "abcdefghijklmnopqrstuvwxyz*|,\":<>[]{}`\';@&$#%()-";
	for (var i = 0; i < Str2.length; i++) {
      	if (iChars.indexOf(Str2.charAt(i)) != -1) {     
		 	FlagIsNumber = true;  		// means string contains only numbers	 	
		} // end if
	} // end for
		
	if (FlagIsNumber == true){
		return true;
	} else {
		StateIncorrectFields = true;
		return false;
	} // end if
} // end function  

function TestNumberCount(index, namefield, iTotNumberSerie) {
	var FlagIsPhoneFax = true;
	var iFoundcount = 0;
	var strMsg = "";
	var strValueCheck = document.forms[strFormName].elements[namefield].value;					
	var Str2 = strValueCheck;
	var iChars = "0123456789";
	for (var i = 0; i < Str2.length; i++) {		
      	if (iChars.indexOf(Str2.charAt(i)) != -1) {  		   
			iFoundcount = iFoundcount + 1;			
		} 
	}	
	if (iFoundcount == iTotNumberSerie) {
		return true;
	} else {	
		StateIncorrectFields = true;
		strMsg = FieldLabel[index] +  "\n ";
		//strMsg = strMsg + "Invalid characters: " + strInvalidChar + "\n";
		FieldErrMsg[index] = strMsg;
		return false;		
	} 
} // end function 


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

