﻿// JScript File

function IsPresent(o){
 if( Trim(document.getElementById(o).value) == "" ){
  return false;
  }
 else{
  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 IsValidTime(o)
 {
     var s= Trim(document.getElementById(o).value);
     if(s.length==0){
     return false;
     }
       var regNo= new RegExp(/^([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$/);	
        if (!regNo.test(s)){
              return false;
         }
    return true;
 }
 
function IsValidPhone(o)
{ 
 var str = Trim(document.getElementById(o).value);
 if(Trim(str).length==0){
   return false;
  }
      var re = new RegExp(/^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/);
      if(!re.test(str)){
      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;
}



function ValidateContactUs(){
IsValid = true;
// Validating First Name for presenceclass="contact-text"
    if ( IsPresent("ctl00_cph_center_txtContactName") == true ){ // rolling back css changes if any
    document.getElementById("fname").className = "contact-text";
     }
    else{ // changing css to reflect validation failed
    document.getElementById("fname").className = "contact-text-invalid";
    IsValid = false;
    }
    
// Validating query for presence
    if ( IsPresent("ctl00_cph_center_txtContactQuery") == true ){ // rolling back css changes if any
    document.getElementById("contactquery").className = "contact-text";
       }
    else{ // changing css to reflect validation failed
    document.getElementById("contactquery").className = "contact-text-invalid";
    IsValid = false;
    }
    
// Validating email id for presence and email pattern + phone number and phone number pattern
    if ( (IsValidEmail("ctl00_cph_center_txtContactEmail") == true) && (IsValidPhone("ctl00_cph_center_txtContactPhoneNo") == true) ){
    document.getElementById("contactemail").className = "contact-text";
    document.getElementById("contactphone").className = "contact-text";    
    }
    else {
            if ( IsValidEmail("ctl00_cph_center_txtContactEmail") == true ){
                document.getElementById("contactemail").className = "contact-text";
                document.getElementById("contactphone").className = "contact-text";    
            }            
            else if ( IsValidPhone("ctl00_cph_center_txtContactPhoneNo") == true ){
                        document.getElementById("contactemail").className = "contact-text";
                        document.getElementById("contactphone").className = "contact-text";    
                        // Validating time
                     if ( IsValidTime("ctl00_cph_center_txtContactBestTime") == true ){
                        document.getElementById("ctm").className = "contact-text";
                        }
                     else{
                        document.getElementById("ctm").className = "contact-text-invalid";
                    IsValid = false;
                        }
                     }
                    else{
                            document.getElementById("contactemail").className = "contact-text-invalid";
                            document.getElementById("contactphone").className = "contact-text-invalid";    
                            IsValid = false;                                
                         }
            
     }
    
    

    
// over all validation 

    if( IsValid == true ){
    return true;
    }
    else{
    return false;
    }    
}
