// JavaScript Document

function mainContent()
{
	  document.getElementById('txtResultBlock').style.display='none';
	  document.getElementById('content').style.display='block';
	  document.getElementById('txtResult').style.display='none';
	  document.getElementById("request_comm_ques").value='';
	  var frm = document.form1;
	  var len = frm.elements.length;
	  extraFeilds = "";
	  for(p=0;p<len;p++){
		if((frm.elements[p].type == 'text')){
			if((frm.elements[p].name=='request_name')||
			   (frm.elements[p].name=='request_phone')||
			   (frm.elements[p].name=='request_email')||
			   (frm.elements[p].name=='txtCaptcha') ||
			   ((frm.elements[p].name).substr(0,13) =='request_text_'))
				  
				frm.elements[p].value = '';
		}
		if( frm.elements[p].type == 'select-one'){
			frm.elements[p].selectedIndex=0;
		}
		if( frm.elements[p].type == 'checkbox'){
			frm.elements[p].checked = false;
		}
		if( frm.elements[p].type == 'radio'){
			frm.elements[p].checked = false;
		}
	  }
	getParam();
	document.getElementById('txtResult1').style.display='none';
}


var xmlHttp = false;
var xmlHttp123 = false;
var xmlHttpF = false;
var xmlHttpC = false;

function getAttorney()
{ 
xmlHttp123=GetXmlHttpObject()
if (xmlHttp123==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }

//document.getElementById("txtResult2").style.display='none';
document.getElementById('txtResult1').innerHTML = "Getting...";
var state=document.getElementById('state').value;
var county=document.getElementById('county').value;
if(county =='')
{ 
  county = 0;
}
var url="get_counties.php"
url=url+"?c="+county+"&s="+state
//alert(url);
url=url+"&sid="+Math.random()
xmlHttp123.onreadystatechange=stateChangedAttorney 
xmlHttp123.open("GET",url,true)
xmlHttp123.send(null)

}
function stateChangedAttorney() 
{ 
if (xmlHttp123.readyState==4 || xmlHttp123.readyState=="complete")
 { 
 document.getElementById("txtResult1").innerHTML=xmlHttp123.responseText; 
 } 

}


function getCounty()
	{ 
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		 {
		 alert ("Browser does not support HTTP Request")
		 return
		 }
		
		var str=document.getElementById("state").value;
		
		if(str=='0')
		{
		 //alert("select a state name"); 
		 document.getElementById('txtResult').innerHTML = "";
		 //return ;
		}
		else{
		var url="get_counties.php"
		url=url+"?state="+str
		url=url+"&sid="+Math.random()
		xmlHttp.onreadystatechange=stateChanged 
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
		}
		
	}
function stateChanged() 
{ 
	if (xmlHttp.readyState == 1) { 
	  document.getElementById('txtResult').innerHTML = '<img src="images/ajax-loader.gif" align="center" />'; 
	}else if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
	 document.getElementById("txtResult").innerHTML=xmlHttp.responseText;
	 getAttorney();
	 } 
}


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


  var http_request = false;
   function makePOSTRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      document.getElementById('txtResultBlock').style.display='block';
	  document.getElementById('content').style.display='none';
      document.getElementById('txtResultF').innerHTML = '<img src="images/ajax48.gif" align="center" />'; 
	  
	  document.form1.action ="request_submit.php?act=step1_profe";
	  document.form1.target = "upload_target_profe";
	  document.form1.submit();
	  /*http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);*/
   }

   function alertContents() {
	  if (http_request.readyState == 1) { 
	  document.getElementById('txtResultBlock').style.display='block';
	  document.getElementById('content').style.display='none';
      document.getElementById('txtResultF').innerHTML = '<img src="images/ajax48.gif" align="center" />'; 
	  
	  }else
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
           // alert(http_request.responseText);
            result = http_request.responseText;
			
			document.getElementById('txtResultBlock').style.display='block';
			document.getElementById('content').style.display='none';
            document.getElementById('txtResultF').innerHTML = result; 
			
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   
   function getParam()
   {
	  img = document.getElementById('imgCaptcha'); 
  	  //Change the image
   	  img.src = 'create_image.php?' + Math.random();
   }

	function requestForm() {
		  error_id =''; 
		  count = 0;
		  sucess_id ='';
		  scount = 0;
		  img_ids = '';
		  imgsu_ids = '';
		  custom_error = '';
		  custom_checking =0;
		  // validate to name
		  var rName = document.getElementById("request_name");
		  if(trim(rName.value)=='')
		  {
			  error_id += "err_id1"+"@@@@";
			  img_ids +="err_img_id1"+"@@@@";
			  count++;
		  }else{
			  sucess_id += "err_id1"+"@@@@";
			  imgsu_ids +="err_img_id1"+"@@@@";
			  scount++;	
		  }
		  var rPhone = document.getElementById("request_phone");
		  // validate to state name
		  var rState = document.getElementById("state");
		  if(rState.value=='0')
		  {
			  error_id += "err_id2"+"@@@@";
			  img_ids +="err_img_id2"+"@@@@";
			  count++;
		  }else{
		  	  sucess_id += "err_id2"+"@@@@";
			  imgsu_ids +="err_img_id2"+"@@@@";
			  scount++;	
		  }
		  if(rState.value!='0')
		  {
			  	
			  var rCounty = document.getElementById("county");
			  var status1 =  document.getElementById("status");
			  // validate to county name
			  if(status1.value == '0'){	
				if(rCounty.value=='no_county'){
				  error_id += "err_id3"+"@@@@";
				  img_ids +="err_img_id3"+"@@@@";
				  count++; 
				}else{
				  sucess_id += "err_id3"+"@@@@";
				  imgsu_ids +="err_img_id3"+"@@@@";
				  scount++;
				}
			  }
		  }
		  var rComm = document.getElementById("request_comm_ques");
		  var rEmail = document.getElementById("request_email");
		  // validate to email
		  var re =/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,3}$/;
		  if(!rEmail.value.match(re)){
			rEmail.value=""
			error_id += "err_id4"+"@@@@";
			img_ids +="err_img_id4"+"@@@@";
			count++;
		  }else{
			sucess_id += "err_id4"+"@@@@";
			imgsu_ids +="err_img_id4"+"@@@@";
			scount++;	
		  }
		
		 
		 var rrTotal = parseInt(document.getElementById('request_require_total').value);
		 var extraFeilds="";
		 var status =0;
		 var statRadio =0;
		 // Cutom fields validations
		 for ( i=0;i< rrTotal;i++){
			if(document.getElementById('request_require['+i+']').value == '1')
			{
				// validate to form type is "TextBox"
				if(document.getElementById('request_type['+i+']').value =='text'){
					if(document.getElementById('request_text_'+i+'').value == '')
					{
						var f = document.getElementById('request_formname['+i+']').value;
						document.getElementById('err_text_'+i+'').style.background='#FFCCCC';
						document.getElementById('img_text_'+i+'').innerHTML = '<img src="images/validationAlert.gif" />';
						custom_error += "1"+"@@@@";
					}else{
						document.getElementById('err_text_'+i+'').style.background='';
						document.getElementById('img_text_'+i+'').innerHTML ='';
						custom_error += "0"+"@@@@";
					}
				} 
				// validate to form type is "CheckBox"
				if(document.getElementById('request_type['+i+']').value =='check'){
					var total_checks = document.getElementById('request_check_total').value;
					var frm = document.form1;
					var len = frm.elements.length;
					for(p=0;p<len;p++)
					if( frm.elements[p].name == 'request_check_'+i+'[]')
					{
						if(frm.elements[p].checked == false )
						{
							status =0;
						}else{
							status =1; break;
						}
					}
					if(status== 0){
						var f = document.getElementById('request_formname['+i+']').value;
						document.getElementById('err_check_'+i+'').style.background='#FFCCCC';
						document.getElementById('img_check_'+i+'').innerHTML = '<img src="images/validationAlert.gif" />';
						custom_error += "1"+"@@@@";
					}else {
						document.getElementById('err_check_'+i+'').style.background='';
						document.getElementById('img_check_'+i+'').innerHTML ='';
						custom_error += "0"+"@@@@";
					}
				}
				// validate to form type is "Select Box"
				if(document.getElementById('request_type['+i+']').value =='select'){
					if(document.getElementById('request_select_'+i+'').value == '0')
					{
						var f = document.getElementById('request_formname['+i+']').value;
						document.getElementById('err_select_'+i+'').style.background='#FFCCCC';
						document.getElementById('img_select_'+i+'').innerHTML = '<img src="images/validationAlert.gif" />';
						custom_error += "1"+"@@@@";
					}else{
						document.getElementById('err_select_'+i+'').style.background='';
						document.getElementById('img_select_'+i+'').innerHTML ='';
						custom_error += "0"+"@@@@";
					}
				}
				// validate to form type is "Radio buttons"
				if(document.getElementById('request_type['+i+']').value =='radio'){
					
					var frm = document.form1;
					var len = frm.elements.length;
					for(p=0;p<len;p++)
					if( frm.elements[p].name == 'request_radio_'+i+'')
					{
						if(frm.elements[p].checked == false ){
							statRadio =0;
						}else{
							statRadio =1; break;
						}
					}
					if(statRadio== 0){
						var f = document.getElementById('request_formname['+i+']').value;
						document.getElementById('err_radio_'+i+'').style.background='#FFCCCC';
						document.getElementById('img_radio_'+i+'').innerHTML = '<img src="images/validationAlert.gif" />';
						custom_error += "1"+"@@@@";
					 }else{
						document.getElementById('err_radio_'+i+'').style.background='';
						document.getElementById('img_radio_'+i+'').innerHTML ='';
						custom_error += "0"+"@@@@";
					}
				}
			}
		  }
		  // Validate to captha code
		  var txtCaptcha = document.getElementById("txtCaptcha");
		  if(txtCaptcha.value == '')
		  {
			  error_id += "err_id5"+"@@@@";
			  img_ids +="err_img_id5"+"@@@@";
			  count++;
		  }
		  //if((txtCaptcha.value).length >= 4){
		  valid_captcha();
		  var valid = document.getElementById("captcha_valid");
		   if(valid.value == '0') {
			   error_id += "err_id5"+"@@@@";
			   img_ids +="err_img_id5"+"@@@@";
			   count++;
			   custom_error += "1"+"@@@@";
			   
		  }else if(valid.value == '1'){
			sucess_id += "err_id5"+"@@@@";
			imgsu_ids +="err_img_id5"+"@@@@";
			scount++;
			custom_error += "0"+"@@@@";
			
		  }
		  //}
		  
		  
		  
		  
		  if((rState.value!='0')&&(error_id=='')&&((txtCaptcha.value).length >= 4)) {
				var rAttorney =  document.getElementById("attorney_id");
				document.getElementById("attroney_did").value = rAttorney.value; 
			    // If no error in the form then post to below variable to php file.
				var poststr = "request_name=" + encodeURI(rName.value)+
							"&request_phone=" + encodeURI(rPhone.value)+
							"&state=" + encodeURI(rState.value)+
							"&county=" + encodeURI(rCounty.value)+
							"&request_comm_ques=" + encodeURI(rComm.value)+
							"&request_email=" + encodeURI(rEmail.value)+
							"&valid=" + encodeURI(valid.value)+
							"&attorney_id=" + encodeURI(rAttorney.value);	
			 	  var frm = document.form1;
				  var len = frm.elements.length;
				  extraFeilds = "";
				  // Custom fields add to post variables.
				  for(p=0;p<len;p++){
					if((frm.elements[p].type == 'text')){
						if((frm.elements[p].name!='request_name')&&
						   (frm.elements[p].name!='request_phone')&&
						   (frm.elements[p].name!='state')&&
						   (frm.elements[p].name!='county')&&
						   (frm.elements[p].name!='request_comm_ques')&&
						   (frm.elements[p].name!='request_email')&&
						   (frm.elements[p].name!='valid')&&
						   (frm.elements[p].name!='attorney_id')){
						extraFeilds += "&"+frm.elements[p].name+"="+encodeURI(frm.elements[p].value);
						}
					}
					if( frm.elements[p].type == 'select-one'){
						extraFeilds += "&"+frm.elements[p].name+"="+frm.elements[p].value;
					}
					if( frm.elements[p].type == 'checkbox'){
						if(frm.elements[p].checked == true ){
						extraFeilds += "&"+frm.elements[p].name+"="+frm.elements[p].value;
						}
					}
					if( frm.elements[p].type == 'radio'){
						if(frm.elements[p].checked == true ){
						extraFeilds += "&"+frm.elements[p].name+"="+frm.elements[p].value;
						}
					}
				 }
						 
				  if(extraFeilds !=''){
					poststr += extraFeilds;  
				  }
		  
			 }
		 //alert(error_id);
		 // checking here - if error in the form then respective block will change the background color   
		 if(error_id!='') {
			 errors = error_id.split("@@@@");
			 images = img_ids.split("@@@@");
			 for ( con=0;con<count;con++){
			 document.getElementById(errors[con]).style.background='#FFCCCC';
			 document.getElementById(images[con]).innerHTML = '<img src="images/validationAlert.gif" />';
			 }
		  }
		  
		  // checking here - if no error in the form then respective block will change the background color as null
		 
		  if(sucess_id!='') {
			 sucess = sucess_id.split("@@@@");
			 images = imgsu_ids.split("@@@@");
			 //alert(images);
			 for ( scon=0;scon<scount;scon++){
			 document.getElementById(sucess[scon]).style.background='';
			 document.getElementById(images[scon]).innerHTML = '';
			 }
		  }
		  
		 
		 // If no error in the form then sumbit to the php file with below function 
		
			custom  = custom_error.split("@@@@");
					
			for(p=0;p<((custom.length)-1);p++){
				if(custom[p]=='1'){	
					custom_checking = 1;break;		
				}
			}
			
		if((error_id=='')&&(custom_checking == 0)){
			 makePOSTRequest('request_submit.php', poststr);
		}
			return false;
	 }

// Validate the captha code
function valid_captcha()
{
	    var txtCaptcha = document.getElementById("txtCaptcha").value;
	  
	    xmlHttpC=GetXmlHttpObject()
		if (xmlHttpC==null)
		 {
		 alert ("Browser does not support HTTP Request")
		 return
		 }
		//if(txtCaptcha.length >=4 ){
		document.getElementById('result').innerHTML = "";
		var url="get_counties.php"
		url=url+"?txtCaptcha="+txtCaptcha
		url=url+"&sid="+Math.random()
		xmlHttpC.onreadystatechange=stateChangedCaptcha 
		xmlHttpC.open("GET",url,true)
		xmlHttpC.send(null)
		//}
		
		
}
function stateChangedCaptcha() 
{ 
	if (xmlHttpC.readyState==4 || xmlHttpC.readyState=="complete")
	 { 
	 document.getElementById("captcha_valid").value=xmlHttpC.responseText; 
	 } 
}

	

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}


