
// Modification history
//               20090907 - Comment the alert on line 87 that displays when submit is
//                          pressed
//               20091124 - Pass Captcha form variables to the ajax program
//                              - Validate the text area for spam variables

/////////////////////////////////////

 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;
      }
      
      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 == 4) {
         if (http_request.status == 200) {
			       //alert(http_request.responseText);
            result = http_request.responseText;
		    document.getElementById('myspans').innerHTML =result;            
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   function get(obj) {
	   
	  var poststr="";
	  var textfields;
	  x=0;
	  for(i=1;i<=5;i++)
	  {
		 
		 if(eval("document.formss.yname"+i).value!="")
		{
			j= "yname"+i+"='"+ encodeURI(eval("document.formss.yname"+i).value ) +"'&";
	        poststr= poststr + j; 
		} 
		
		if(eval("document.formss.yemail"+i).value!="")
		{
			
			k= "yemail"+i+"='"+ escape(encodeURI(eval("document.formss.yemail"+i).value )) +"'&";
			
			poststr=poststr + k;
			 x++;
		}	
	 
	 
	  }
	   if(document.formss.msg.value!="")
		{
			l= "msg='"+ escape(encodeURI(document.formss.msg.value)) +"'&";
	        poststr= poststr + l; 
		} 
	n= "x="+ x +"&";	
	poststr= poststr + n;
	
  // 20091124  - Pass Captcha code to php program
	
 	poststr= poststr + "recaptcha_challenge_field=" + Recaptcha.get_challenge() + "&";
 	poststr= poststr + "recaptcha_response_field=" + Recaptcha.get_response() + "&";
	
	// 20091124  - End of Captcha code 

	
	m= "ur_l='"+ escape(encodeURI(document.formss.url.value)) +"'";	
	poststr= poststr + m;
	
	
// 20090907 - comment the alert below	
//	alert(poststr);

document.formss.yname1.value="";
document.formss.yemail1.value="";
document.formss.yname2.value="";
document.formss.yemail2.value="";
document.formss.yname3.value="";
document.formss.yemail3.value="";
document.formss.yname4.value="";
document.formss.yemail4.value="";
document.formss.yname5.value="";
document.formss.yemail5.value="";
document.formss.msg.value="";
document.getElementById("myInput_1").innerHTML="";
document.getElementById("myinput").innerHTML="";
document.getElementById("myspans").innerHTML="";
	      makePOSTRequest('ns_w_ajax.php', poststr);

   }

// 20091124  - Captcha code

function showRecaptcha(element,  themeName) {
  Recaptcha.create("6LcKnwkAAAAAAEUr8EzjTulugR7FqG-jK4deQkNY", element, {
        theme: themeName,
        callback: Recaptcha.focus_response_field
  });

}



// 20091124  - Validate Text area

function form_text(text_str)
{

	          document.getElementById("myinput").innerHTML="";

              var myArray = [];
              myArray[0] = /money/i;
              myArray[1] = /http/i;
              myArray[2] = /www/i;
              myArray[3] = /%/;
			  myArray[4] = /cash/i;
			  myArray[5] = /home/i;
			  myArray[6] = /work/i;
			  myArray[7] = /survey/i;
			  myArray[8] = /quick/i;

              for (var i=0; i<myArray.length; i++)
                {
                  var matchPos1 = text_str.search(myArray[i]);
                 
                  if(matchPos1 != -1)
                    {
            	        document.getElementById("myInput_1").innerHTML="<font color='#ff0000'><b>- Text contains invalid words which could be interpreted as spam</font></b>"; 
                      return false;
                      break;
                   }
               }

}

// 20091124  - Validate captcha

function captcha_validate()
 {

    document.getElementById("myinput").innerHTML="";
    if  ((Recaptcha.get_challenge()==null)||(Recaptcha.get_response()==null))
        {
           document.getElementById("myInput_1").innerHTML="<font color='#ff0000'><b>- Click Continue button and type both words separated by space</font></b>"; 
           return false;
        }
}


function email(emailID)
{
	document.getElementById("myinput").innerHTML="";
	
if ((emailID==null)||(emailID=="")){
			document.getElementById("myInput_1").innerHTML="<font color='#ff0000'><b>- Please enter valid email-id</font></b>";	  
			//emailID.focus();
		 return false;
	}
if (echeck(emailID)==false){
		
		//emailID.focus();
		return false;
	}
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   document.getElementById("myInput_1").innerHTML="<font color='#ff0000'><b>- Invalid E-mail ID</font></b>";
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   document.getElementById("myInput_1").innerHTML="<font color='#ff0000'><b>- Invalid E-mail ID</font></b>";
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    document.getElementById("myInput_1").innerHTML="<font color='#ff0000'><b>- Invalid E-mail ID</font></b>";
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    document.getElementById("myInput_1").innerHTML="<font color='#ff0000'><b>- Invalid E-mail ID</font></b>";
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    document.getElementById("myInput_1").innerHTML="<font color='#ff0000'><b>- Invalid E-mail ID</font></b>";
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    document.getElementById("myInput_1").innerHTML="<font color='#ff0000'><b>- Invalid E-mail ID</font></b>";
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    document.getElementById("myInput_1").innerHTML="<font color='#ff0000'><b>- Invalid E-mail ID</font></b>";
		    return false;
		 }

 						
	}


		 

		  


