/**
* @version 1.0
* @author Deepak Sharma
* This file is utilized for the validation of form using javascript in conjugation with ajax
* @copyright www.uttarakhandshadi.com 2009
*
*/

function getXmlHttpRequestObject() {
 if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); //Mozilla, Safari ...
 } else if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); //IE
 } else {
    //Display our error message
    alert("Your browser doesn't support the XmlHttpRequest object.");
 }
}

//Our XmlHttpRequest object
var receiveReq = getXmlHttpRequestObject();
//Initiate the AJAX request
/*function makeRequest(url, param) {
//If our readystate is either not started or finished, initiate a new request
// if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
   //Set up the connection to captcha_test.html. True sets the request to asyncronous(default) 
   receiveReq.open("POST", url, true);
   //Set the function that will be called when the XmlHttpRequest objects state changes
   receiveReq.onreadystatechange = updatePage; 
   receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   receiveReq.setRequestHeader("Content-length", param.length);
   receiveReq.setRequestHeader("Connection", "close");
   //Make the request
   receiveReq.send(param);
 //}
}*/

var msg='';
function makeRequestCaptcha(url, param) {
//alert("deepak");
//return false;
   receiveReq.open("POST", url, true);
   receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   receiveReq.setRequestHeader("Content-length", param.length);
   receiveReq.setRequestHeader("Connection", "close");
   receiveReq.send(param);
   receiveReq.onreadystatechange = function(){
     if (receiveReq.readyState == 4){
       document.getElementById('Ucaptcha').innerHTML = msg ='';
	   if(receiveReq.responseText==0){
         document.getElementById('Ucaptcha').innerHTML = msg = 'Please fill correct Verification code';
	     //img = document.getElementById('imgCaptcha'); 
	     //img.src = './create_image.php?' + Math.random();
	   }else{	/*alert(receiveReq.responseText);*/   }
    }
   } 
   return msg;
}

function checkUserName(txtfield){
  var val = txtfield.value;
  if(txtfield.value.length<5){
 	showUserMsg("username should have atleast 5 characters");
	return false;
  }else{
	showUserMsg("");
  }
 //var val = registeruser.txtUserName.value
  http.abort();
  var url = "availbilty.php?key="+val;
  http.open("GET", url, true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
    if (http.status == 200) {
	if(http.responseText==1){
	   document.getElementById('validateUser').value=1;
 	   showUserMsg("username already exists");
       return false;		
	}else if(http.responseText==2){
	   document.getElementById('validateUser').value=2;
	   showUserMsg("<font color='green'>Profile name available.</font>");
       return true;
	}
	}
   }
  }
  http.send(null);	
}

function checkUser(txtfield){ 
 var val = txtfield.value;
 //alert(val);
 //var val = registeruser.txtUserName.value
  http.abort();
  var url = "availbilty.php?key="+val;
  http.open("POST", url, true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
    if(http.responseText == 0){
 	   alert("This Profile Name is not available");
       return false;		
	}
   }
  }
  http.send(null);	
}

//Called every time our XmlHttpRequest objects state changes
function updatePage(){
 //Check if our response is ready
 if (receiveReq.readyState == 4){
 //Set the content of the DIV element with the response text
 if(receiveReq.responseText==0){
  //document.getElementById('verifyCaptcha').innerHTML = receiveReq.responseText;
  if((receiveReq.responseText!='')){
	   document.getElementById('errormsg').innerHTML ="Sorry, you have provided an invalid security code.";
	   window.document.getElementById("txtCaptcha").value='';
	   window.document.getElementById("captcharesp").value='0';
	   //Get a reference to CAPTCHA image
	   img = document.getElementById('imgCaptcha'); 
	   //Change the image
	   img.src = './create_image.php?' + Math.random();
	   return false;
	   }
   }else{
	   document.getElementById('errormsg').innerHTML ="";
	   window.document.getElementById("captcharesp").value='1';
	   document.registeruser.submit();
   }
 }
}

// check captcha code
function checkCaptcha(theForm){
  url = 'captcha.new.php';
  //Set up the parameters of our AJAX call
  postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value);
  //Call the function that initiate the AJAX request

  setTimeout("makeRequestCaptcha(url, postStr)",500);	
}

//Called every time when form is perfomed
function getParam(theForm) {
  //alert(theForm);
  //return false;
  var formError='';
  var filter2=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
  var filter3 = new RegExp("^(?=.*\\W).*$", "g");
  
  document.getElementById('Uuname').innerHTML='';
  document.getElementById('Ufname').innerHTML='';
  document.getElementById('Ulname').innerHTML='';
  document.getElementById('Uemail').innerHTML='';
  document.getElementById('Upwd').innerHTML='';
  document.getElementById('Ucpwd').innerHTML='';
  document.getElementById('Udate').innerHTML='';
  document.getElementById('Umonth').innerHTML='';
  document.getElementById('Uyear').innerHTML='';
  document.getElementById('Ubelong').innerHTML='';
  document.getElementById('Ucaste').innerHTML='';
  document.getElementById('Ucaptcha').innerHTML='';
  document.getElementById('Utc').innerHTML='';

	if(theForm.txtUserName.value==''){
			formError+='Please enter profile name.<br/>';
			document.getElementById('Uuname').innerHTML='<br/>Please enter profile name.';
			//alert('Please enter profile name.<br/>');
			//return false;
	}
	else if(theForm.txtUserName.value.length<3){
			formError+='Profile name should be minimum three character.<br/>';
			document.getElementById('Uuname').innerHTML='<br/>Profile name should be minimum three character.';
			//return false;
	}
	else if(filter2.test(theForm.txtUserName.value)){
			formError+="Email id can't be taken as Profile Name.<br/>";
			document.getElementById('Uuname').innerHTML="<br/>Email id can't be taken as Profile Name.";
			//return false;
	}else if(filter3.test(theForm.txtUserName.value)){
			formError+="Don't use special character in Profile Name.<br/>";
			document.getElementById('Uuname').innerHTML="<br/>Don't use special character in Profile Name.";
			//return false;
	}
/* 	var val = theForm.txtUserName.value;
	//alert(theForm.txtUserName.value);
	http.abort();
	var url = "availbilty.php?key="+val;
	http.open("POST", url, true);
	http.onreadystatechange=function(){
	if(http.readyState == 4) {
	  if(http.responseText ==0){
	   alert("This user_name is already registered with us");
	   formError+="This user_name is already registered with us";
	  }else{
	   //showErrMsg("<font color='green'>You can register with this profile name.</font>");
	}
	}
	}
	http.send(null);
	*/
/*    txtuser = theForm.txtUserName;
    setTimeout("checkUser(txtuser)",1000);
*/	
	if(theForm.validateUser.value==1){
		formError+="This profile name already exists.<br/>";
		document.getElementById('Uuname').innerHTML="<br/>This profile name already exists.";
	}
	
    if(theForm.txtFirstName.value==''){
		formError+="Please enter First Name.<br/>";
		document.getElementById('Ufname').innerHTML="<br/>Please enter First Name.";
		//return false;
	}
	else if(theForm.txtFirstName.value.length<3){
		formError+="First name should be minimum three character.<br/>";
		document.getElementById('Ufname').innerHTML="<br/>First name should be minimum three character.";
		//return false;
		}
	if(theForm.txtLastName.value==''){
		formError+='Please enter Last Name.<br/>';
		document.getElementById('Ulname').innerHTML='<br/>Please enter Last Name.';
		//return false;
	}
	else if(theForm.txtLastName.value.length<3){
		formError+='Last name should be minimum three character.<br/>';
		document.getElementById('Ulname').innerHTML='<br/>Last name should be minimum three character.';
		//return false;
		}

	if(theForm.txtEmail.value==''){
		formError+='Please enter Email.<br/>';
		document.getElementById('Uemail').innerHTML='<br/>Please enter Email.';
		//return false;
	}else{
		if(!checkEmail(theForm.txtEmail.value,1)){
		formError+='Invalid E-mail Address! Please re-enter.<br/>';	
		document.getElementById('Uemail').innerHTML='<br/>Invalid E-mail Address! Please re-enter.';	
		//return false;
		}
	}

	/*var val = theForm.txtEmail.value;
	http.abort();
	var url = "availbilty_email.php?key="+val;
	http.open("POST", url, true);
	http.onreadystatechange=function() {
	if(http.readyState == 4) {
	if(http.responseText == "This email is already registered with us"){
	showErrMsg("This email already registered with us");
	
	return false;		
	}else{
	
	return true;
	  }
	 }
	}
	http.send(null);*/	

	if(theForm.txtPassword.value==''){
		formError+='Please enter Password.<br/>';
		document.getElementById('Upwd').innerHTML='<br/>Please enter Password.';
		//return false;
	}
	else if(theForm.txtPassword.value.length<6){
		formError+='Password should be minimum six character.<br/>';
		document.getElementById('Upwd').innerHTML='<br/>Password should be minimum six character.';
		//return false;
		}

	if(theForm.txtConfirmPassword.value==''){
		formError+='Please enter Confirm Password.<br/>';
		document.getElementById('Ucpwd').innerHTML='<br/>Please enter Confirm Password.';
		//return false;
	}

	else if(theForm.txtPassword.value!=theForm.txtConfirmPassword.value){
		formError+='Confirm Password is not matching.<br/>';
		document.getElementById('Ucpwd').innerHTML='<br/>Confirm Password is not matching.';
		//return false;
	}
	if(theForm.v_month.value==''){
		formError+='Please select Month.<br/>';
		document.getElementById('Umonth').innerHTML='<br/>Please select Month.';
		//return false;
	}
	if(theForm.v_day.value==''){
		formError+='Please select Date.<br/>';
		document.getElementById('Udate').innerHTML='<br/>Please select Date.';
		//return false;
	}
	if(theForm.v_year.value==''){
		formError+='Please select Year.<br/>';
		document.getElementById('Uyear').innerHTML='<br/>Please select Year.';
		//return false;
	}

	
	var countr1;
	var check_radio1=false;
	var check_radio=false;
	for(countr2=0;countr2 < 3;countr2++){
	  // var or = document.getElementsByName(oRadio);
	  if(theForm.Belong[countr2].checked==true){
		var check_radio1=true;
	  }
	}
	
	for(countr1=0;countr1 < 4;countr1++){
	  // var or = document.getElementsByName(oRadio);
	  if(theForm.Belong1[countr1].checked==true){
		var check_radio=true;
	  }
	}
	
	if(check_radio1==false){
	  formError+="Please select Belongs.<br/>";
	  document.getElementById('Ubelong').innerHTML="<br/>Please select Belongs.";
	  //return false;
	}
	if(check_radio==false){
	  formError+="Please select Belongs To Caste.<br/>";
	  document.getElementById('Ucaste').innerHTML="<br/>Please select Belongs To Caste.";
	  //return false;
	}
	    //set url
    if(theForm.txtCaptcha.value==''){
	  formError+="Please Fill The Verification Code.<br/>";
	  document.getElementById('Ucaptcha').innerHTML="<br/>Please Fill The Verification Code.";
	  //return false;
    }else{
		
/*	  var url = 'captcha.php';
      //Set up the parameters of our AJAX call
      var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value);
      //Call the function that initiate the AJAX request
      makeRequest(url, postStr);*/
	  if(!msg == ''){
		formError+=msg+"<br/>";
		document.getElementById('Ucaptcha').innerHTML=msg+"<br/>";
		//msg = '';
	  }
	} 

	
/*	if(theForm.txtPostCode.value==''){
		formError+='Please enter Post Code.<br/>';
		//return false;
	}
*/   
   
   if(theForm.tnc.checked==false){
	 formError+="Please accept terms & conditions.<br/>";
	 document.getElementById('Utc').innerHTML="<br/>Please accept terms & conditions.";
    //return false;
   }
   if(formError!=''){
	  alert("You have some error on this page, Please check the respective fields");
	//showErrMsg(formError);
	 return false;
   }
}

function getParam2(){
 var formError='';
 var ddleducationcat = document.getElementById("ddleducationcat").value;
 var membership = document.getElementById("membership").value;
 var phoneno = document.getElementById("phone_no");
 var placeofbirth = document.getElementById("txtPob");

 document.getElementById('Ueducation').innerHTML='';
 document.getElementById('Uphone').innerHTML='';
 document.getElementById('Upob').innerHTML='';
 document.getElementById('Uaboutme').innerHTML='';
 document.getElementById('Uaboutmatch').innerHTML='';
 document.getElementById('Uaboutfamily').innerHTML='';


   if(ddleducationcat == "null"){
     formError+="Please select education.<br/>";
	document.getElementById('Ueducation').innerHTML="<br/>Please select education.";

   }
  

   if(membership == "null"){
     formError+="Please select Membership.<br/>";
   }
   
if((phoneno.value == "") || ((phoneno.value == null)) || (phoneno.value == "Phone Number")){}
else{
 if((phoneno.value.length < 7)  && (phoneno.value.length > 15)){
   formError+="Please enter a valid phone number.<br/>";
   document.getElementById('Uphone').innerHTML="<br/>Please enter a valid phone number.";
  }
 if(isNaN(phoneno.value)){
   formError+="Please enter a valid phone number.<br/>";
   document.getElementById('Uphone').innerHTML="<br/>Please enter a valid phone number.";
  }
 }
  
  if(placeofbirth.value != ""){
  if(!isNaN(placeofbirth.value)){
   formError+="Place of birth can't be numeric.<br/>";
   document.getElementById('Upob').innerHTML="<br/>Place of birth can't be numeric.";
  }
  }

  if(trim(document.getElementById("about_me").value)==""){
	document.getElementById("about_me").value="";
    formError+="Please Enter About me.<br/>";
	document.getElementById('Uaboutme').innerHTML="Please Enter About me.<br/>";

  }
  if(trim(document.getElementById("about_match").value)==""){
	document.getElementById("about_match").value="";
    formError+="Please Enter About match.<br/>";
	document.getElementById('Uaboutmatch').innerHTML="Please Enter About match.<br/>";
}
  if(trim(document.getElementById("about_family").value)==""){
	document.getElementById("about_family").value="";
    formError+="Please Enter About family.";
	document.getElementById('Uaboutfamily').innerHTML="Please Enter About family.";
}
  if(formError!=''){
	  alert("You have some error on this page, Please check the respective fields");
	//showErrMsg(formError);
	return false;
  }
}


function getNewImage(){
  img = document.getElementById('imgCaptcha'); 
  //Change the image
  img.src = './create_image.php?' + Math.random();
  return false;
}

function clearThis(txtField){
	txtField.value="";
}