//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);
}
//============================
// Extra for OfficeFile open company form
function MM_openBrWindow1(theURL) { //v2.0
   if(document.cgform.alphselect[0].selected){
      }
   else{
       for(i=0; i<document.cgform.alphselect.options.length; i++){
         if(document.cgform.alphselect[i].selected){
		 	theURL = theURL + "#" + document.cgform.alphselect[i].value;
            top.location.href = theURL;
			cgform.reset(); 
            }
         }
	}
}
//============================
// Extra for OfficeFile open catagory form
function MM_openBrWindow2(theURL, Pre) { //v2.0
   if(document.cgform.cgselect[0].selected){
      }
   else{
       for(i=0; i<document.cgform.cgselect.options.length; i++){
         if(document.cgform.cgselect[i].selected){
		 	
		 	if(document.cgform.cgselect[i].value != "1" ){
			theURL = theURL + document.cgform.cgselect[i].value + ".htm";
              top.location.href = theURL;
		  	cgform.reset();  
				}
				else{
			theURL2 = Pre + "Guides.htm";
              top.location.href = theURL2;
		  	cgform.reset();  
			var theURL3 = theURL + "noCompany.htm"
			MM_openBrWindow(theURL3,'','status=no,menubar=yes,scrollbars=no,width=550,height=300')
				}
            }
         }
	}
}
//============================
//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 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 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("")
    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;
}
//==========================

