// JavaScript Document

// FUNCTION THAT will check if a given field(as fieldname) is empty 
// if it is they print * required 
// it takes 2 argument 
// 1- input is the value that has been entered 
// 2- name of the place that will show the error msg... 
// i have used the msg box(a span)with name "fieldname_msg", to make it easy for the function to work out the place to show msg
function check_for_empty(input, fieldname){
	
	
	if(input.length==0)
		{
		
			/*alert('length is 0');*/
			document.getElementById(fieldname+'_msg').innerHTML=' * Required';
			document.getElementById(fieldname+'_msg').style.color="red";
			//document.getElementById(fieldname).style.background="red";
	        document.getElementById(fieldname).style.border="1px solid red";
			//shakem(1);
			//shakef(1);
			return false;
			
		}
		else
		{
			document.getElementById(fieldname+'_msg').innerHTML='Thank you';
			document.getElementById(fieldname+'_msg').style.color="green";
			document.getElementById(fieldname).style.background="white";
	        document.getElementById(fieldname).style.border="1px solid #5B0D08";

	        return true;
		
		}
	
}


// this function checks to if only only alphabets has been entered
// 2 arguemt input -- value that user has entered and msg box to show the msg if there is problem
// first it check if user has entered anything if not then call the function check_for_empty-- to print out the msg 
// other wise check if user has entered only alpha -- then funtion ends
// otherwise show error msg asking to the use to fix entery
function check_for_alphanum(input, fieldname)
{
// var   alphaRegExp = /[0123456789.\/\\.<>?:";',\[\]\{\}=\+\*\)\(&^%$#@!]/;
 
 // this exp will let the ' " to be entered 
 var   alphaRegExp = /[\/\\|.<>?:;,\[\]\{\}=\+\*\-\)\(&^%$#@!]/;
// var   alphaRegExp =/^[a-zA-Z_0-9]$/;
//var   alphaRegExp =   /\W/;
		

 	 var res1 = check_for_empty(input, fieldname);
		 //alert('in alpha jst after chek empty res is ');
		 //alert(res1);
		
		if(res1 == false){
			
			return false;
		}
		else
		{
				  
			if(alphaRegExp.test(input))
			{
				document.getElementById(fieldname+'_msg').innerHTML='* Alphanumeric only';
				document.getElementById(fieldname+'_msg').style.color="red";
				document.getElementById(fieldname).style.background="red";
				document.getElementById(fieldname).style.border="1px solid #c9960c";
				return false;
	
	
			}
			else
			{
				 
				document.getElementById(fieldname+'_msg').innerHTML='Thank You';
				document.getElementById(fieldname+'_msg').style.color="green";
				document.getElementById(fieldname).style.background="white";
				document.getElementById(fieldname).style.border="1px solid #5B0D08";
	
			return true;
			}
		}
}



function check_for_alpha(input, fieldname)
{
// var   alphaRegExp = /[0123456789.\/\\.<>?:";',\[\]\{\}=\+\*\)\(&^%$#@!]/;
// var   alphaRegExp = new RegExp (/^[A-Za-z]$/);

	var alphaRegExp = new RegExp (/[^a-zA-Z\$]/);
//var   alphaRegExp =   /\W/;
		//var dd = (return (alphaRegExp.test(input)));
 	 var res2 = check_for_empty(input, fieldname);
		 //alert('in alpha jst after chek empty res is ');
		 //alert(res1);
		
		if(res2 == false){
			
			return false;
		}
		else
		{
				  
			if(alphaRegExp.test(input))
			{
				document.getElementById(fieldname+'_msg').innerHTML=' * Alphabets only';
				document.getElementById(fieldname+'_msg').style.color="red";
				document.getElementById(fieldname).style.background="red";
				document.getElementById(fieldname).style.border="1px solid #c9960c";
				return false;
	
	
			}
			else
			{
				 
				document.getElementById(fieldname+'_msg').innerHTML=' Thank You';
				document.getElementById(fieldname+'_msg').style.color="green";
				document.getElementById(fieldname).style.background="white";
	
				document.getElementById(fieldname).style.border="1px solid #5B0D08";
	
			return true;
			}
		}
}





//function to check valid email address
function isValidEmail(input, fieldname){
  var emailRegExp =  /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;


		var res = check_for_empty(input, fieldname);
		
		if(res == false)
		{
			return false;
		}
		else
		{
				// search email text for regular exp matches
				if(emailRegExp.test(input))
				{
					document.getElementById(fieldname+'_msg').innerHTML=' Thank You';
					document.getElementById(fieldname+'_msg').style.color="green";
					document.getElementById(fieldname).style.background="white";

					document.getElementById(fieldname).style.border="1px solid #5B0D08";
					return true;
				} 
				else
				{
				   	document.getElementById(fieldname+'_msg').innerHTML=' * Invalid email ';
					document.getElementById(fieldname+'_msg').style.color="red";
				   	document.getElementById(fieldname).style.border="1px solid #c9960c"
				   	return false; 
				}
			
		}

}


//function to check valid phone number
function check_for_phone(input, fieldname){
  //var phoneRegExp =  /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
   //var phoneRegExp = /^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/; 
   var phoneRegExp = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/;



	var res = check_for_empty(input, fieldname);
	
	if(res == false)
		{
			return false;
		}
		else
		{
   			
    		if(phoneRegExp.test(input))
   			{
				
				document.getElementById(fieldname+'_msg').innerHTML=' Thank You';
				document.getElementById(fieldname+'_msg').style.color="green";
				document.getElementById(fieldname).style.border="1px solid #444444";
      			return true;
    		} 
			else
			{
				
			   	document.getElementById(fieldname+'_msg').innerHTML=' * Invalid phone';
				document.getElementById(fieldname+'_msg').style.color="red";
	        	document.getElementById(fieldname).style.border="1px solid red";
			   	return false; 
			}

		}

}



//thus function check if the indate and out for availablity has beed filed and filled correctly
// otherwise it gives an error and do not submit form
function check_contactus_form(){
    
var val_name =   document.getElementById('name').value;
var val_phone =   document.getElementById('phone').value;
var val_email =   document.getElementById('email').value;


var res3 = check_for_empty(val_name, 'name');
var res2 = check_for_phone(val_phone, 'phone');
var res1 = isValidEmail(val_email, 'email');





if(res1 == true && res2 == true && res3 == true)
   {	return true;  }
    else
    {	return false; }
    
}







