// JavaScript Document

jQuery(document).ready(function(){
	
	
	var emailFilter    = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	var zipFilter      = /^\d{5}([\-]\d{4})?$/;
	var webFilter      = /(http|https):\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
	var phoneFilter    = /^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$/;
	var firstnameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	var companynameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
	var fname         = jQuery("#Fname");
	
	var form          = jQuery("#frmRegistration");
	var email         = jQuery("#txtEmail");
	
 	var pass          = jQuery("#txtPassword");
	var cPass         = jQuery("#txtConfirmPassword");
 	var license       = jQuery("#txtLicense");
	var specialty     = jQuery("#specialty");
	var country       = jQuery("#country");
	var state         = jQuery("#state");
	var city          = jQuery("#txtCity");
	var zip           = jQuery("#txtZip");
	var webUrl        = jQuery("#txtUrl");
	var certificate   = jQuery("#certification");
	var lang          = jQuery("#languages");
	var termsAccepted = jQuery("#termsAccepted");
	var stateInfo     = jQuery("#stateInfo");
	var txtCaptcha    = jQuery("#txtCaptcha");
	var phone         = jQuery("#txtPhone");
    var	licensureInfo = jQuery("#licensureInfo");
	var interest      = jQuery("#areaofinterest");
	var mess          = jQuery("#message");
	var comp          = jQuery("#company");
	var pos           = jQuery("#position");
	
	
	country.change(showState);
	
	form.submit(function(){
		
		if(jQuery.trim(fname.val())==""){
			inlineMsg('Fname','Please enter  name.',3);
			return false;	
		}
		
		if(jQuery.trim(fname.val()).length<2){
			inlineMsg('Fname','Name must be between 2 to 30 characters.',3);
			return false;	
		}
		if(!fname.val().match(firstnameRegex)){
			inlineMsg('Fname','Please enter valid Name.',3);
			return false;
		}  
		 
		
		
		
		if(!validateEmail()){
			return false;
		}
		
				
		
		if(jQuery.trim(interest.val())==""){
			inlineMsg('areaofinterest','Please Select Practice / Area of Interest.',3);
			return false;	
		}
		
	
		
		if(jQuery.trim(mess.val())==""){
			inlineMsg('message','Please Enter Message.',3);
			return false;	
		}
		
		if(jQuery.trim(mess.val()).length<5){
			inlineMsg('message','Message must be more than 5 characters.',3);
			return false;	
		}
		if(jQuery.trim(comp.val())==""){
			inlineMsg('company','Please Enter Company Name.',3);
			return false;	
		}
		
		/*if(jQuery.trim(comp.val()).length<3){
			inlineMsg('company','Company name must be between 3 to 50 characters.',3);
			return false;	
		}
		if(!comp.val().match(companynameRegex)){
			inlineMsg('company','Please enter valid Message.',3);
			return false;
		}  */
		
		if(jQuery.trim(pos.val())==""){
			inlineMsg('position','Please Enter Position.',3);
			return false;	
		}
		
		
		if(!pos.val().match(companynameRegex)){
			inlineMsg('position','Please enter valid Position.',3);
			return false;
		}  
		
		
		
		if(!validateConfirmEmail()){
			return false;
		}
		
		if(!validatePassword()){
			return false;
		}
		
		if(!validateConfirmPass()){
			return false;
		}
		
		if(specialty.val()=="select"){
			inlineMsg('specInfo','Please select specialty.',3);
			return false;
		}
		if(country.val()=="select"){
			inlineMsg('country','Please select country.',3);
			return false;
		}	
		
		if((document.getElementById('state').value=="") && (country.val()!="US")){
			inlineMsg('state','Please enter state.',3);
			return false;
		}
		if(!document.getElementById('state').value.match(firstnameRegex)){
			inlineMsg('state','Please enter valid state.',3);
			return false;
		} 
		if(city.val()!=""){
			if(!city.val().match(firstnameRegex)){
				inlineMsg('txtCity','Please enter valid city.',3);
				return false;
			}  
		}
		if(jQuery.trim(zip.val())==""){
			inlineMsg('txtZip','Please enter zip.',3);
			return false;
		}
		if(country.val()=="US"){
			if(!zipFilter.test(zip.val())){
				inlineMsg('txtZip','Please enter valid zip code.',3);
				return false;
			}
		}
		
		if(phone.val()!=""){
			if(!phoneFilter.test(phone.val())){
				inlineMsg('txtPhone','Please enter valid phone.',3);
				return false;
			}
		}
		

		if(webUrl.val()!=""){
			if(!webFilter.test(webUrl.val())){
				inlineMsg('txtUrl','Please enter valid web URL.',3);
				return false;
			}
		}
		
		if(document.getElementById('pageType')){
			if(document.getElementById('pageType').value=="2"){
				if((document.getElementById('certification').value=="") && (country.val()!="US")){
					inlineMsg('certification','Please enter state board licensure.',3);
					return false;
				}
			}
		}else{
			if(certificate.val()=="select"){
				inlineMsg('certification','Please select state board certification.',3);
				return false;
			}
		}
		
		if(jQuery.trim(license.val())==""){
			inlineMsg('txtLicense','Please enter license number.',3);
			return false;
		}
		if(lang.val()=="select"){
			inlineMsg('languages','Please select languages spoken.',3);
			return false;
		}
		
		if(txtCaptcha.val()==""){
			inlineMsg('txtCaptcha','Please enter verification code.',3);
			return false;
		}
		
		if(!termsAccepted.attr('checked')){
			inlineMsg('termInfo','Please accept  terms of use.',3);
			return false;			
		}
		return true;
		
	});
	
	//Function to Validate Email-id for Registration
	function validateEmail(){
		
		
	
		
		var a = jQuery.trim(email.val());
		if(a==""){
			jQuery("#txtEmail").focus();
			inlineMsg('txtEmail','Please enter Email-id.',3);
			return false;
			
		}else if(emailRegex.test(a)){
		
			
			return true;
		}else{
			jQuery("#txtEmail").focus();
			inlineMsg('txtEmail','Please enter valid email-id.',3);
			return false;
		}
			
	}
	
	//Function to Validate Confirm Email 
	/*function validateConfirmEmail(){
		var a = email.val();
		var b = cEmail.val();
		if(a!=b){
			inlineMsg('txtConfirmEmail','Email and confirm email should be same.',3);
			return false;
		}
		return true;
	}
	*/
	//Function to Validate Password
	function validatePassword(){
		var a = pass.val();
		if(a == ""){
			inlineMsg('txtPassword','Password must be between 6 to 15 characters.',3);
			return false;
		}else if((a.length < 6) | (a.length>15)){
			inlineMsg('txtPassword','Password must be between 6 to 15 characters.',3);
			return false;
		}else{
			return true;
		}
	}
	
	//Function to Validate Confirm Password 
	function validateConfirmPass(){
		var a = pass.val();
		var b = cPass.val();
		if(a!=b){
			inlineMsg('txtConfirmPassword','Password and confirm password should be same.',3);
			return false;
		}
		return true;
	}
	
	//Function to show state
	function showState()
	{
		var a= country.val();

		if(a=="US"){
			var url    = 'request.php?showState';
			var result = ajaxRequest(url);						
			stateInfo.html(result);
			if(document.getElementById('pageType')){
				if(document.getElementById('pageType').value=="2"){
					result = result.replace('state','certification');
					result = result.replace('state','certification');
					licensureInfo.html(result);	
				}
			}
			zip.attr('maxlength','5');
			
		}else{
			var result = '<input maxlength="25" name="state" type="text" id="state" class="textbox_reqd" />';
			stateInfo.html(result);	
			if(document.getElementById('pageType')){
				if(document.getElementById('pageType').value=="2"){
					result = result.replace('state','certification');
					result = result.replace('state','certification');
					licensureInfo.html(result);	
				}
			}
			zip.attr('maxlength','8');
		}
		
	}
	
	
});

function ajaxRequest(urlController)
{
	jQuery.ajax({
	type: "POST",
	url: urlController,
	async: false,
	data: jQuery("form"),
	success:
		function(result) 
		{
			returnValue=result;
			return
		},
	error:
		function(errorThrown) {
			returnValue="error";
			return;
		}
	});	
	return returnValue;
}

function RefreshCaptcha()
{
	var captchaImage = document.getElementById('captcha');
	captchaImage.src = '../securimage/securimage_show.php?sid=' + Math.random(); 
	return false
}
