// JavaScript Document


 
function isEmpty(theString) {
  if(theString.length == 0) return(true);
  else return(false);
  } 

function isNumber(theString){
  var numString = "1234567890";
  for(i=0; i<theString.length; i++){
     if(numString.indexOf(theString.substring(i, i+1), 0)==-1) return(false);
     }
  return(true);
  }
 
 
 
  function validateLoginfrm()  {
	  
		 var error_message ='';
		 var returnVar = true;
		 
		 
		 //User Name
		if(isEmpty(document.ovation_login_frm.username.value)){
			error_message = error_message + 'Please Enter Your Username \n';
			returnVar = false
			document.ovation_login_frm.username.style.backgroundColor= '#FF0000' ;
		  }else{
		   document.ovation_login_frm.username.style.backgroundColor= '#FFFFFF';
		}
		
		 
		 //Password
		if(isEmpty(document.ovation_login_frm.password.value)){
			error_message = error_message + 'Please Enter Your Password \n';
			returnVar = false
			document.ovation_login_frm.password.style.backgroundColor= '#FF0000' ;
		  }else{
		   document.ovation_login_frm.password.style.backgroundColor= '#FFFFFF';
		}
		 
		 
		 
		if(returnVar == false){
			alert(error_message);
			return false;
		}else{
			return true;
		} 
 
  }
	  
	  
	  
 
 
 
 
 
 function validate_ovation_frm()  {



 var error_message ='';
 var returnVar = true;
 
 
 //-_______ SET UP FORM FILED VARS ___________
 
 
 if(isEmpty(document.ovation_frm.Access1.value) || isEmpty(document.ovation_frm.Access2.value) || isEmpty(document.ovation_frm.Access3.value)){
	error_message = error_message + 'Please Enter Your Access Code, All Three Fields Are Required \n';
    returnVar = false
	document.ovation_frm.Access1.style.backgroundColor= '#FF0000';
	document.ovation_frm.Access2.style.backgroundColor= '#FF0000';
	document.ovation_frm.Access3.style.backgroundColor= '#FF0000';
	
  }else{
   document.ovation_frm.Access1.style.backgroundColor= '#FFFFFF';
   document.ovation_frm.Access2.style.backgroundColor= '#FFFFFF';
   document.ovation_frm.Access3.style.backgroundColor= '#FFFFFF';
   
}
 
 
 

//First Name
if(isEmpty(document.ovation_frm.fName.value)){
	error_message = error_message + 'Please Enter Your First Name \n';
    returnVar = false
	document.ovation_frm.fName.style.backgroundColor= '#FF0000' ;
  }else{
   document.ovation_frm.fName.style.backgroundColor= '#FFFFFF';
}


//Last Name
if(isEmpty(document.ovation_frm.lName.value)){
	error_message = error_message + 'Please Enter Your Last Name \n';
    returnVar = false
	document.ovation_frm.lName.style.backgroundColor= '#FF0000';
  }else{
   document.ovation_frm.lName.style.backgroundColor= '#FFFFFF';
}


//Email
if(isEmpty(document.ovation_frm.email.value)){
	error_message = error_message + 'Please Enter Your Email Address \n';
    returnVar = false
	document.ovation_frm.email.style.backgroundColor= '#FF0000';
  }else{
	    
var validEmail = Validate_Email_Address(document.ovation_frm.email.value);

//Email
if(!validEmail){
	error_message = error_message + 'Error: Email Address Is Not Valid \n';
    returnVar = false
	document.ovation_frm.email.style.backgroundColor= '#FF0000';
  }else{
   document.ovation_frm.email.style.backgroundColor= '#FFFFFF';
}
  
}






//Email1 = Email2

if(validEmail){
	if(document.ovation_frm.email.value != document.ovation_frm.email2.value){
		error_message = error_message + 'Please Make Sure That Both Email Entries Match \n';
		returnVar = false
		document.ovation_frm.email.style.backgroundColor= '#FF0000';
		document.ovation_frm.email2.style.backgroundColor= '#FF0000';
	  }else{
		document.ovation_frm.email.style.backgroundColor= '#FFFFFF';
		document.ovation_frm.email2.style.backgroundColor= '#FFFFF';
	}
}



	
	// USER NAME	
	if(isEmpty(document.ovation_frm.uName.value)){
		error_message = error_message + 'Please Enter Your User Name. \n';
		returnVar = false
		document.ovation_frm.uName.style.backgroundColor= '#FF0000';
	  }else{
       document.ovation_frm.uName.style.backgroundColor= '#FFFFFF';
}	
		
		
   	 re = /^\w+$/;   	 
		
		
 if(document.ovation_frm.email.value != document.ovation_frm.uName.value){
	   if(!re.test(document.ovation_frm.uName.value)) {
     	error_message = error_message + 'If your Username is different from your Email Address - it must contain letters and numbers only!';
      	returnVar = false;
		document.ovation_frm.uName.style.backgroundColor= '#FF0000';
      	}else{
       document.ovation_frm.uName.style.backgroundColor= '#FFFFFF';
		}
			
			
 }
		
		
	// PASS WORD	
	if(isEmpty(document.ovation_frm.password1.value)){
		error_message = error_message + 'Please Enter Your Password. \n';
		returnVar = false
		document.ovation_frm.password1.style.backgroundColor= '#FF0000';
	}	
	
	
	//Password 1 = Password 2
	if(document.ovation_frm.password1.value != document.ovation_frm.password2.value){
		error_message = error_message + 'Please Make Sure That Both Password Entries Match. \n';
		returnVar = false
		document.ovation_frm.password1.style.backgroundColor= '#FF0000';
		document.ovation_frm.password2.style.backgroundColor= '#FF0000';
		  }else{
		document.ovation_frm.password1.style.backgroundColor= '#FFFFFF';
		document.ovation_frm.password2.style.backgroundColor= '#FFFFFF';
}	
	
	

   //Password Letters and numbers only
   re = /^\w+$/;  
   
   if(!re.test(document.ovation_frm.password1.value)) {
		error_message = error_message + 'Error: Password must contain letters and numbers only!. \n';
		returnVar = false
		document.ovation_frm.password1.style.backgroundColor= '#FF0000';
		document.ovation_frm.password1.focus();
		  }else{
		document.ovation_frm.password1.style.backgroundColor= '#FFFFFF';
}	

if(returnVar == false){
	alert(error_message);
	return false;
}else{
	return true;
}



}



function Validate_Email_Address(email_address) {
  //Assumes that valid email addresses consist of user_name@domain.tld
  at = email_address.indexOf('@');
  dot = email_address.indexOf('.');

  if(at == -1 || 
    dot == -1 || 
    dot <= at + 1 ||
    dot == 0 || 
    dot == email_address.length - 1)
    return(false);
     
  user_name = email_address.substr(0, at);
  domain_name = email_address.substr(at + 1, email_address.length);                  

  if(Validate_String(user_name) === false || 
    Validate_String(domain_name) === false)
    return(false);                     

  return(true);
}





function Validate_String(string, return_invalid_chars) {
  valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  invalid_chars = '';
  if(string == null || string == '')
     return(true);

  //For every character on the string.   
  for(index = 0; index < string.length; index++) {
    char = string.substr(index, 1);                        
     
    //Is it a valid character?
    if(valid_chars.indexOf(char) == -1) {
      //If not, is it already on the list of invalid characters?
      if(invalid_chars.indexOf(char) == -1) {
        //If it's not, add it.
        if(invalid_chars == '')
          invalid_chars += char;
        else
          invalid_chars += ', ' + char;
      }
    }
  }
            
  //If the string does not contain invalid characters, the function will return true.
  //If it does, it will either return false or a list of the invalid characters used
  //in the string, depending on the value of the second parameter.
  if(return_invalid_chars == true && invalid_chars != '') {
    last_comma = invalid_chars.lastIndexOf(',');
    if(last_comma != -1)
      invalid_chars = invalid_chars.substr(0, $last_comma) + 
      ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
    return(invalid_chars);
    }
  else
    return(invalid_chars == ''); 
}
