// JavaScript Document

var targetWin;
function openWindow(pageURL,title,w,h)
{
	var left = (screen.width/2)-(w/2);
	var top = (screen.height/2)-(h/2);
	targetWin = window.open (pageURL, title, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
}

function afterFormSubmit()
{	
	targetWin.document.myForm.submit();
    targetWin.close();
	window.location = "http://www.nexvisionix.com/retailanalytics/retailanalytics.html";
}

var valid,v1
function validate_form( )
{
	valid=true;
	v1=false;
	for(i=0;i<4;i++)    
   {
		
		if(document.forms[0].elements[i].value == "")
		{
			alert(document.forms[0].elements[i].name+" field is mandatory.");
			document.forms[0].elements[i].focus();
			return false;
		}
	   
   }

  v1= (val_name())
  valid=(v1 && val_email()); 
  /*if(valid)
	{
		//window.opener.afterFormSubmit();
		return false;
	}*/
  if(valid)
	{
	  alert("Thanks for filling up your information on the nexvisionix website. The link to the demo will be emailed to the address provided by you shortly.")
	}
  return valid;
}
function val_name()
{
	//Validation for name fields
	for(j=0;j<0;j++)
	{
		var name=document.forms[0].elements[j].value;
		var f=false;
		for(i=0;i<name.length;i++)
		{
			if(!((name.charAt(i)>='A' && name.charAt(i)<='Z')||(name.charAt(i)>='a' && name.charAt(i)<='z')))
			{
				f=true;
				break;
			}
		}
		if(f)
		{
			alert("Enter valid "+document.forms[0].elements[j].name);
			document.forms[0].elements[j].value="";
			document.forms[0].elements[j].focus();
			return false;
//			break;
		}
	}
	return true;
}

function val_email()
{	
	//Validation for email field
	var email=document.forms[0].elements[2].value;
	var at="@"
	var dot="."
	var idat=email.indexOf(at)
	var lemail=email.length
	var iddot=email.indexOf(dot)
	//alert(lemail);
	//alert(email.indexOf(at))
	if (email.indexOf(at)==-1 || email.indexOf(at)==0 || email.indexOf(at)==(lemail-1))
	{
		    alert("Enter valid "+document.forms[0].elements[2].name);
		    document.forms[0].elements[2].value="";
		    document.forms[0].elements[2].focus();
		    return false;
	}
	if (email.indexOf(dot)==-1 || email.indexOf(dot)==0 || email.indexOf(dot)==(lemail-1))
	{
	        alert("Enter valid "+document.forms[0].elements[2].name);
		    document.forms[0].elements[2].value="";
		    document.forms[0].elements[2].focus();
		    return false;
	}
	if((lemail-1)-(email.indexOf(dot))<2)
	{
	        alert("Enter valid "+document.forms[0].elements[2].name);
		    document.forms[0].elements[2].value="";
		    document.forms[0].elements[2].focus();
		    return false;
	}
	if (email.indexOf(at,(idat+1))!=-1)
     {
		    alert("Enter valid "+document.forms[0].elements[2].name);
		    document.forms[0].elements[2].value="";
		    document.forms[0].elements[2].focus();
		    return false;
	 }
	 if (email.substring(idat-1,idat)==dot || email.substring(idat+1,idat+2)==dot)
	 {
	        alert("Enter valid "+document.forms[0].elements[2].name);
		    document.forms[0].elements[2].value="";
		    document.forms[0].elements[2].focus();
		    return false;
	 }
	 if (email.indexOf(dot,(idat+2))==-1)
	 {
		    alert("Enter valid "+document.forms[0].elements[2].name);
		    document.forms[0].elements[2].value="";
		    document.forms[0].elements[2].focus();
		    return false;
	 }
	 if (email.indexOf(" ")!=-1)
	 {
		    alert("Enter valid "+document.forms[0].elements[2].name);
		    document.forms[0].elements[2].value="";
		    document.forms[0].elements[2].focus();
		    return false;
	 }	
	else
	    return true;
}
