﻿// JScript File
function ValidateSignUp(){
var IsValid = true;

// validating First Name for presence

    if ( IsPresent("ctl00_cph_center_txtfname") == true ){ // rolling back css changes if any
    document.getElementById("fname").className = "lefttext-personal";    
    }
    else{ // changing css to reflect validation failed
    document.getElementById("fname").className = "lefttext-personal-invalid";
    IsValid = false;
    }
    
// validating Last Name for presence

    if ( IsPresent("ctl00_cph_center_txtlname") == true ){ // rolling back css changes if any
    document.getElementById("lname").className = "lefttext-personal";    
    }
    else{ // changing css to reflect validation failed
    document.getElementById("lname").className = "lefttext-personal-invalid";
    IsValid = false;
    }
    
// validating Company Name for Presence

    if ( IsPresent("ctl00_cph_center_txtCompanyName") == true ){ // rolling back css changes if any
    document.getElementById("compname").className = "lefttext-personal";    
    }
    else{ // changing css to reflect validation failed
    document.getElementById("compname").className = "lefttext-personal-invalid";
    IsValid = false;
    }
    
// validating zip code for presence and (zip code) pattern

    if( IsValidZipCode("ctl00_cph_center_txtZip") == true){ // rolling back css changes if any
    document.getElementById("zip").className = "lefttext-personal";    
    }
    else{ // changing css to reflect validation failed
    document.getElementById("zip").className = "lefttext-personal-invalid";
    IsValid = false;
    }
    
// validating email id for presence and (email) pattern

    if( IsValidEmail("ctl00_cph_center_txtEmailid") == true){ // rolling back css changes if any
    document.getElementById("emailspan").className = "lefttext-personal";    
    }
    else{ // changing css to reflect validation failed
    document.getElementById("emailspan").className = "lefttext-personal-invalid";
    IsValid = false;
    }

// Over All Validation
    if ( IsValid == true ){
    return true;
    }
    else{
    return false;
    }
}

function IsPresent(o){
 if( Trim(document.getElementById(o).value) == "" ){
  return false;
  }
 else{
  return true;
 }   
}

function IsValidZipCode(o){
  o = Trim(document.getElementById(o).value);
  reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/)     
      if(o.length==0){       
         return false;
      }
      if (!reZip.test(o)){         
         return false;
       }
   return true;
}

function IsValidEmail(o){
 o = Trim(document.getElementById(o).value);
 var pos1 = o.indexOf('@');
 var pos2 = o.indexOf('.');
 var str=o;
 var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i	;
  if(o.length==0) {  // zero length email
  return false;
  }
  if((o!="") && (pos1 == - 1)){	// @ symbol not present
	return false;
   }
	
  if((o!="") && pos2 == -1){ // . symbol not present	
	return false;
	}
	
  if (!filter.test(str)) { // not a valid email pattern
	return false;
  }	
  
  return true
}

function Trim(str){  
    while(str.charAt(0) == (" ") )
    {  str = str.substring(1);  }
    while(str.charAt(str.length-1) == " " )
    {  str = str.substring(0,str.length-1); }
  return str;
}

