
// Javascrips for Order Form Validation //

var isNN = (navigator.appName.indexOf("Netscape")!=-1);


function autoTab(input,len, e)
{

var keyCode = (isNN) ? e.which : e.keyCode; 
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
if(input.value.length >= len && !containsElement(filter,keyCode)) {
input.value = input.value.slice(0, len);
input.form[(getIndex(input)+1) % input.form.length].focus();
}
function containsElement(arr, ele)
{
var found = false, index = 0;
while(!found && index < arr.length)
if(arr[index] == ele)
found = true;
else
index++;
return found;
}

function getIndex(input)
{
var index = -1, i = 0, found = false;
while (i < input.form.length && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}


return true;
}

function popupPage(l, t, w, h) {
	var windowprops = "location=no,scrollbars=no,menubars=no,toolbars=no,resizable=yes" +
	",left=" + l + ",top=" + t + ",width=" + w + ",height=" + h;

	var URL = "aboutprimus.htm";
	popup = window.open(URL,"MenuPopup",windowprops);
	}

function stripComma(s)

{   var i;
    var returnString = "";
    var bag = ",";
    var strfiller = "  ";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) { returnString += c ;	}
        else { returnString += strfiller; }
    }

    return returnString;
}

function IsAlphaNumeric(theValue) {
    for(var inx = 0; inx < theValue.length; inx++) {
        if(!IsNumeric(theValue.charAt(inx)) && !IsAlpha(theValue.charAt(inx))) {
            return false;
        }
    }

    return true;
}

function IsNumeric(theChar) {
    if(theChar >= '0' && theChar <= '9') {
        return true;
    }
    else { return false; }
}

function IsAlpha(theChar) {
    if(theChar >= 'A' && theChar <= 'z') {
        return true;
    }
    else { return false; }
}

function CheckPhoneArea(theField) {
    var theVal = theField.value;
    var theLength = theVal.length;
    var inx = 0;
    var theChar = '';
    if(theLength < 3) {
	        alert('Please enter 3 digit Area code for conatct and Main phone number fields!.');
	        theField.focus();
	        theField.select();
	        return false;
    }
    else {

		for(inx = 0; inx < theLength; inx++) {
			theChar = theVal.charAt(inx);
			if(IsAlpha(theChar)) {
				alert("Area Code should be entered with numbers ONLY!");
				theField.focus();
				theField.select();
				return false;
			}

		}

		return true;
   }
}

function CheckPhone1(theField) {
    var theVal = theField.value;
    var theLength = theVal.length;
    var inx = 0;
    var theChar = '';
    if(theLength < 7) {
	        alert('Please enter 7 digit phone number for conatct and Main phone number fields!.');
	        theField.focus();
	        theField.select();
	        return false;
    }
    else {

		for(inx = 0; inx < theLength; inx++) {
			theChar = theVal.charAt(inx);
			if(IsAlpha(theChar)) {
				alert("Phone numbers should be entered with numbers ONLY!");
				theField.focus();
				theField.select();
				return false;
			}

		}

		return true;
   }
}

function CheckPhoneNxx(theField) {
    var theVal = theField.value;
    var theLength = theVal.length;
    var inx = 0;
    var theChar = '';
    if(theLength < 3) {
	        alert('Please enter 3 digit NXX  for phone number fields!.');
	        theField.focus();
	        theField.select();
	        return false;
    }
    else {

		for(inx = 0; inx < theLength; inx++) {
			theChar = theVal.charAt(inx);
			if(IsAlpha(theChar)) {
				alert("Phone numbers should be entered with numbers ONLY!");
				theField.focus();
				theField.select();
				return false;
			}

		}

		return true;
   }
}

function CheckPhoneNumber(theField) {
    var theVal = theField.value;
    var theLength = theVal.length;
    var inx = 0;
    var theChar = '';
    if(theLength < 4) {
	        alert('Please enter last 4 digits for phone number fields!.');
	        theField.focus();
	        theField.select();
	        return false;
    }
    else {

		for(inx = 0; inx < theLength; inx++) {
			theChar = theVal.charAt(inx);
			if(IsAlpha(theChar)) {
				alert("Phone numbers should be entered with numbers ONLY!");
				theField.focus();
				theField.select();
				return false;
			}

		}

		return true;
   }
}

function CheckPhone2(theField) {
    var theVal = theField.value;
    var theLength = theVal.length;
    var inx = 0;
    var theChar = '';
    if(theLength < 10) {
	        alert('Please enter 10 digit phone numbers for additional Phone Lines!.');
	        theField.focus();
	        theField.select();
	        return false;
    }
    else {

		for(inx = 0; inx < theLength; inx++) {
			theChar = theVal.charAt(inx);
			if(IsAlpha(theChar)) {
				alert("Phone numbers should be entered with numbers ONLY!");
				theField.focus();
				theField.select();
				return false;
			}

		}

		return true;
   }
}

function CheckZip(theField) {
    var theVal = theField.value;
    var theLength = theVal.length;
    var inx = 0;
    var theChar = '';
    if(theLength < 5) {
	        alert('Please enter 5 digit Zip code!.');
	        theField.focus();
	        theField.select();
	        return false;
    }
    else {

		for(inx = 0; inx < theLength; inx++) {
			theChar = theVal.charAt(inx);
			if(IsAlpha(theChar)) {
				alert("Zip code cannot contain alpha characters");
				theField.focus();
				theField.select();
				return false;
			}

		}

		return true;
   }
}

function CheckSSN(theField) {
    var theVal = theField.value;
    var theLength = theVal.length;
    var inx = 0;
    var theChar = '';
    if(theLength < 9) {
	        alert('Please enter 9 digit Social Security number!.');
	        theField.focus();
	        theField.select();
	        return false;
    }
    else {

		for(inx = 0; inx < theLength; inx++) {
			theChar = theVal.charAt(inx);
			if(IsAlpha(theChar)) {
				alert("SSN cannot contain alpha characters");
				theField.focus();
				theField.select();
				return false;
			}

		}

		return true;
   }
}

function filterDigits()
{
    if (window.event.keyCode<48 || window.event.keyCode >57) {
	alert("Please use only digits")
	window.event.keyCode = 8
    }
}

function CheckLength(theField) {
    if(theField.value.length < 1) {
        alert('This is a mandatory field.  You must\nenter some information here.');
        theField.focus();
        theField.select();
        return false;
    }
    else { return true; }
}


function Trim(theField) {
    var theVal = theField.value;
    var theLength = theVal.length;
    var newVal = '';
    var foundChar = false;
    var inx = 0;
    
    for(inx = 0; inx < theLength; inx++) {
        if(!foundChar && theVal.charAt(inx) != ' ') {
            foundChar = true;
        }
        
        if(foundChar) {
            newVal += theVal.charAt(inx);
        }
    }
    
    foundChar = false;
    theVal = newVal;
    theLength = theVal.length;
    newVal = '';
    for(inx = (theLength - 1); inx >= 0; inx--) {
        if(!foundChar && theVal.charAt(inx) != ' ') {
            foundChar = true;
        }
        
        if(foundChar) {
            newVal = theVal.charAt(inx) + newVal;
        }
    }    
    
    theField.value = newVal;
}

// This function checks for validity of E-Mail address
function validEMail(theField)
{
	if (theField.value.length != 0){
		if ((theField.value.indexOf('@') < 0) || (theField.value.indexOf('.') < 0)){
			alert('You must enter a valid E-Mail address');
			theField.focus();
			theField.select();
			return false;
		}
	}
	return true;
}



//////////////////////////////////////////////////////////////////////////

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
  result = true;
  }

  return result;
}

function validRequired(formField,fieldLabel)
{
  var result = true;
  
  if (formField.value == "")
  {
    alert('Please enter a value for the "' + fieldLabel +'" field.');
    formField.focus();
    result = false;
  }
  
  return result;
}

function validRequired_drop(formField,fieldLabel)
{
  var result = true;
 
  if (formField.selectedIndex == 0)
  {
    alert('Please select a value for the "' + fieldLabel +'" field.');
    formField.focus();
    result = false;
  }
  
  return result;
}


function validEmail(formField,fieldLabel,required)
{
  var result = true;
  
  if (required && !validRequired(formField,fieldLabel))
    result = false;

  if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
  {
    alert("Please enter a complete email address in the form: yourname@yourdomain.com");
    formField.focus();
    result = false;
  }
   
  return result;

}


function validNum(formField,fieldLabel,required)
{
  var result = true;

  if (required && !validRequired(formField,fieldLabel))
    result = false;
  
   if (result)
   {
     var num = parseInt(formField.value,10);
     if (isNaN(num))
     {
       alert('Please enter a number for the "' + fieldLabel +'" field.');
      formField.focus();    
      result = false;
    }
  } 
  
  return result;
}

function validDate(formField,fieldLabel,required)
{
  var result = true;

  if (required && !validRequired(formField,fieldLabel))
    result = false;
  
   if (result)
   {
     var elems = formField.value.split("/");
     
     result = (elems.length == 3); // should be three components
     
     if (result)
     {
       var month = parseInt(elems[0],10);
        var day = parseInt(elems[1],10);
       var year = parseInt(elems[2],10);
      result = !isNaN(month) && (month > 0) && (month < 13) &&
            !isNaN(day) && (day > 0) && (day < 32) &&
            !isNaN(year) && (elems[2].length == 4);
     }
     
      if (!result)

     {
       alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
      formField.focus();    
    }
  } 
  
  return result;
}

// Function assumes field already validated for date
function validOver13(formField)
{
  var result = true;
  
  // Create date object using birth date str
  var birthDate = new Date(formField.value);
  
  // Create todays date
  var today = new Date();
  
  // getTime returns date in milliseconds, so need
  // to divide by number of milliseconds in year
  var age = (today.getTime() - birthDate.getTime())
        /(365*24*60*60*1000)


  if (age < 13)
  {
     alert('You must be over age 13 to register.');
     result = false;
  }
  
  return result;
}



function validCurrentDate(indate)
{
	x= new Date(indate.value);
	today = new Date();
	tdmon=today.getMonth()+1;
	stripped=tdmon+'-'+today.getDate()+'-'+today.getYear();
	//alert('value of stripped"' + stripped +'" ');
	today= new Date(stripped);
	//alert('value of today"' + today.valueOf() +'" ');
	//alert('value of input"' + x.valueOf() +'" ');
	if (x.valueOf()<today.valueOf())
	{  
	   alert('You entered a Past Date! Please enter Todays Date');
	   return false;
	}
	
	if (x.valueOf()>today.valueOf())
	{  
            alert('You entered a Future Date!  Please enter Todays Date');
	    return false;
	}
	//alert('Passed all tests.');
	return true;
}



function checkTerm(formField,fieldLabel)
{
  var result = true;
 
  if (formField.checked == false)
  {
    alert('Please check terms and conditions.');
   
    result = false;
  }
  
  return result;
}

function CheckSSN1(theField) {
    var theVal = theField.value;
    var theLength = theVal.length;
    var inx = 0;
    var theChar = '';
    if(theLength < 3) {
	        alert('Please enter 3 digit NXX  for Social Security Number field!.');
	        theField.focus();
	        theField.select();
	        return false;
    }
    else {

		for(inx = 0; inx < theLength; inx++) {
			theChar = theVal.charAt(inx);
			if(IsAlpha(theChar)) {
				alert("SSN numbers should be entered with numbers ONLY!");
				theField.focus();
				theField.select();
				return false;
			}

		}

		return true;
   }
}

function CheckSSN2(theField) {
    var theVal = theField.value;
    var theLength = theVal.length;
    var inx = 0;
    var theChar = '';
    if(theLength < 2) {
	        alert('Please enter 2 digit NXX  for Social Security Number field!.');
	        theField.focus();
	        theField.select();
	        return false;
    }
    else {

		for(inx = 0; inx < theLength; inx++) {
			theChar = theVal.charAt(inx);
			if(IsAlpha(theChar)) {
				alert("SSN numbers should be entered with numbers ONLY!");
				theField.focus();
				theField.select();
				return false;
			}

		}

		return true;
   }
}
function CheckSSN3(theField) {
    var theVal = theField.value;
    var theLength = theVal.length;
    var inx = 0;
    var theChar = '';
    if(theLength < 4) {
	        alert('Please enter 4 digit NXX  for Social Security Number field!.');
	        theField.focus();
	        theField.select();
	        return false;
    }
    else {

		for(inx = 0; inx < theLength; inx++) {
			theChar = theVal.charAt(inx);
			if(IsAlpha(theChar)) {
				alert("SSN numbers should be entered with numbers ONLY!");
				theField.focus();
				theField.select();
				return false;
			}

		}

		return true;
   }
}
//----------------------------------------------------------------------------
function validateEMAILForm(theForm)
{
  // Customize these calls for your form

  // Start ------->
        
	
		if (!validEmail(theForm.txtemail,"Email Address",true))
           return false;
   
   
	  // <--------- End
		//document.SubEmail.submit();
	  return true;
	  
}

//----------------------------------------------------------------------------
function validateAgentForm(theForm)
{
  // Customize these calls for your form

  // Start ------->
        
	if (!validRequired(theForm.txtcompany_name,"Company Name"))
	    return false;
	if (!validRequired(theForm.txtfirstname,"First Name"))
	    return false;
	if (!validRequired(theForm.txtlastname,"Last Name"))
		return false;
	if (!validRequired(theForm.txtaddress,"Street Address"))
	    return false;
	if (!validRequired(theForm.txtcity,"City Name"))
	    return false;
	if (!validRequired(theForm.state,"State Name"))
	    return false;
	if (!validRequired(theForm.zip,"Zip Code"))
	    return false;
	if (!validRequired(theForm.txtdayphone_area,"Phone Area Code"))
	    return false;
	if (!CheckPhoneNxx(theForm.txtdayphone_area," Phone Area Code"))
	    return false;
	if (!validRequired(theForm.txtdayphone_nxx," Phone NXX"))
	    return false;
	if (!CheckPhoneNxx(theForm.txtdayphone_nxx," Phone Area Code"))
	    return false;
		if (!validRequired(theForm.txtdayphone_number," Phone Number"))
	    return false;  
	 	if (!CheckPhoneNumber(theForm.txtdayphone_number,"Phone Number"))
	    return false; 
		if (!validEmail(theForm.txtemail,"Email Address",true))
           return false;
   
   
	  // <--------- End
	
	  return true;
}

//----------------------------------------------------------------------------
function validateForm(theForm)
{
  // Customize these calls for your form

  // Start ------->
        

	if (!validRequired(theForm.txtfirstname,"First Name"))
	    return false;
	if (!validRequired(theForm.txtlastname,"Last Name"))
		return false;
		
	if (!validRequired(theForm.SSN1,"SSN Number"))
	    return false;
	
	if (!CheckSSN1(theForm.SSN1,"SSN Number"))
	    return false;
	    
	  if (!validRequired(theForm.SSN2,"SSN Number"))
	    return false;
	
	if (!CheckSSN2(theForm.SSN2,"SSN Number"))
	    return false;
	  
	  if (!validRequired(theForm.SSN3,"SSN Number"))
	    return false;
	
	if (!CheckSSN3(theForm.SSN3,"SSN Number"))
	    return false;
	  
	  
	if (!validRequired_drop(theForm.date_of_birth_month,"Date of Birth - Month"))
	    return false;
	if (!validRequired_drop(theForm.date_of_birth_day,"Date of Birth - Day"))
	    return false;
	if (!validRequired_drop(theForm.date_of_birth_year,"Date of Birth - Year"))
	    return false;
	if (!validRequired(theForm.txtaddress,"Street Address"))
	    return false;
	if (!validRequired(theForm.txtcity,"City Name"))
	    return false;
	if (!validRequired(theForm.state,"State Name"))
	    return false;
	if (!validRequired(theForm.zip,"Zip Code"))
	    return false;
	if (!validRequired(theForm.txtdayphone_area,"Billing Phone Area Code"))
	    return false;
	if (!CheckPhoneNxx(theForm.txtdayphone_area,"Billing Phone Area Code"))
	    return false;
	if (!validRequired(theForm.txtdayphone_nxx,"Billing Phone NXX"))
	    return false;
	if (!CheckPhoneNxx(theForm.txtdayphone_nxx,"Billing Phone Area Code"))
	    return false;
	    
	if (!validRequired(theForm.txtdayphone_number,"Billing Phone Number"))
	    return false;  
	    
	if (!CheckPhoneNumber(theForm.txtdayphone_number,"Billing Phone Number"))
	    return false; 
	      
	if (!validEmail(theForm.txtemail,"Email Address",true))
           return false;
           
    if (!validRequired(theForm.Reference,"Reference"))
	    return false;
	/* 
    if(!checkTerm(theForm.Terms_Service,""))
		return false;
         
           
	if (!validRequired(theForm.mainphone_area,"Main Phone Area Code"))
	    return false;
	if (!validRequired(theForm.mainphone_nxx,"Main Phone NXX"))
	    return false;
	if (!validRequired(theForm.mainphone_number,"Main Phone Number"))
	    return false;
	if (!validRequired(theForm.todaysdate,"Todays Date"))
	    return false;
        if (!validDate(theForm.todaysdate,"Todays Date"))
	    return false;
	
	if (!validCurrentDate(theForm.todaysdate))
	    return false;
	  */
	  // <--------- End
	
	  return true;
}
//-----------------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////////////

function MM_findObj(n, d) { //v3.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); return x;
}

function MM_validateForm() { //v3.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      }
else if (test!='R') { num = parseFloat(val);
        if (val!=''+num) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  }

if (errors){
alert('The following fields are required:\n'+errors);
document.MM_returnValue = (errors == '');
}
else{
document.RegisterUser.submit();
document.SubEmail.submit();

}
}

//
