  //-----------  For capcha ------------------------------------//
var chars = "0123456789ABCDEFGHIJKLMNPQRSTUVWXTZabcdefghikmnpqrstuvwxyz";
var string_length = 7;
var randomstring = '';
for (var i=0; i<string_length; i++) {
	var rnum = Math.floor(Math.random() * chars.length);
	randomstring += chars.substring(rnum,rnum+1);
}
//-----------  end For capcha ------------------------------------//

//-----------  end For capcha ------------------------------------//

String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};

//----------------End of validation---------------------
function Inint_AJAX_Lead() 
{
   try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
   try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
   alert("XMLHttpRequest not supported");
   return null;
}
//-------------------------------------------
  
  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){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr-1){
		    alert("Invalid E-mail ID")
		    return false
		}	
		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}
	
function Validator(theForm)
{

	if (theForm.name.value == "")
		{
		alert("Please Enter Name.");
		theForm.name.focus();
		return false ;
		}
	
	if (theForm.email.value == "")
		{
			alert("Please Enter Email.");
			theForm.email.focus();
			return false ;
		}
	if (echeck(theForm.email.value)==false)
		{
		theForm.email.focus();
		return false;
		}
	
	if (theForm.phone.value == "")
		{
		alert("Please Enter Phone Number.");
		theForm.phone.focus();
		return false ;
		}
	if(isNaN(theForm.phone.value)) 
		{ 
		 alert("Only numbers are allowed in Phone Number."); 
		 theForm.mobile.focus(); 
		 return false; 
    	}
		
	if (theForm.company.value == "")
		{
		alert("Please Enter Company Name.");
		theForm.company.focus();
		return false ;
		}
		
	if (theForm.designation.value == "")
		{
		alert("Please Enter Designation.");
		theForm.designation.focus();
		return false ;
		}
		
	/*if (theForm.prducts.value == "")
		{
		alert("Please Enter List of Prducts Description.");
		theForm.prducts.focus();
		return false ;
		}*/
		
	// ************ For captcha validation ***************
		
	if (theForm.comments.value == "")
		{
		alert("Please Enter Description/Comments.");
		theForm.comments.focus();
		return false ;
		}
		
		 if (theForm.txtCaptcha.value=='' )
		  {
			alert("Please enter image verification code.");
			theForm.txtCaptcha.focus();
			return (false);
		  }
		  else
		  {
				if(theForm.txtCaptcha.value > randomstring || theForm.txtCaptcha.value < randomstring )
					{
						alert('Invalid Security code! Please enter again!');
						theForm.txtCaptcha.focus();
						return false;
					}
				
			 
		  }
		  
	// ************ end For captcha validation ***************
		  
	//---------Asign value---------------------
	
	var email=theForm.email.value;
	var name=theForm.name.value;
	var phone=theForm.phone.value;
	
	var company=theForm.company.value;
	var title=theForm.designation.value;
	
	var message=theForm.comments.value;
	var send_to=theForm.send_to.value;
	var subject_email=theForm.subject_email.value;
	var website_url=theForm.website_url.value;
	var form_type=theForm.form_type.value;
	var salutation=theForm.salutation.value;
	//var topics='';
	
	var fnamestr=name.trim();
	
	if(fnamestr.lastIndexOf(' ')==-1)
	{
		theForm.first_name.value='';
		theForm.last_name.value=fnamestr;
	}
	else
	{
		var indexpos=fnamestr.lastIndexOf(' ');
		theForm.last_name.value=fnamestr.substr(indexpos,(fnamestr.length-indexpos));
		
		var fname_plus_middle=fnamestr.substr(0,indexpos);
		var fname_plus_middle_trim=fname_plus_middle.trim();
		
		if(fname_plus_middle_trim.indexOf(' ')==-1)
		{
			theForm.first_name.value=fname_plus_middle_trim;
			theForm.middle_name_c.value='';
		}
		else
		{
			var indexpos_f=fname_plus_middle_trim.indexOf(' ');
			theForm.first_name.value=fname_plus_middle_trim.substr(0,indexpos_f);
			theForm.middle_name_c.value=fnamestr.substr(indexpos_f,(fname_plus_middle_trim.length-indexpos_f));	
		}
	}
	//----------Trimming------------------
	theForm.first_name.value=theForm.first_name.value.trim();;
	theForm.middle_name_c.value=theForm.middle_name_c.value.trim();;
	theForm.last_name.value=theForm.last_name.value.trim();
	//------------End of Trimming----------------------
	theForm.email1.value=email;
	theForm.phone_work.value=phone;
	theForm.description.value=message;
	
	
	theForm.title.value=title;
	theForm.account_name.value=company;
	
	/*
	
	if(theForm.project1.checked==true)
	topics+='- '+theForm.project1.value;
	if(theForm.project2.checked==true)
	topics+=' - '+theForm.project2.value;
	if(theForm.project3.checked==true)
	topics+=' - '+theForm.project3.value;
	
	topics+=' - '+form_type;*/
	
	theForm.topic_c.value=website_url+' -'+form_type;
	//-----------------Ajax code for sending email---------------------------
	
 	var  req_lead = Inint_AJAX_Lead();
	req_lead.onreadystatechange = function ()
	{ 
	          if (req_lead.readyState==4)
			  {
               if (req_lead.status==200)
			   {
				//document.getElementById("countrydivcontainer").innerHTML=req_lead.responseText;
				//alert(req_lead.responseText);
				return true;
               } 
            }
     };
     req_lead.open("GET", "http://www.sonyplace.com/request.php?email="+email+"&name="+name+"&phone="+phone+"&message="+escape(message)+"&designation="+title+"&subject_email="+subject_email+"&send_to="+send_to+"&company="+company+"&salutation="+salutation,0); //make connection
     req_lead.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
     req_lead.send(null); //send value  
		  
}


