function trimEnds(inputString) {
   if (typeof inputString != "string") 
	{ 
		inputString = inputString+""; 
	}
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ")
	{
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ")
	{
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	return retValue;
}


  function valid() 
 {
	 var name1 = trimEnds(document.forms[0].first_name.value);
	 var name2 = trimEnds(document.forms[0].last_name.value);
	 var email = trimEnds(document.forms[0].email.value);
	 var tel = trimEnds(document.forms[0].phone.value);
	 var title = trimEnds(document.forms[0].title.value);
     var company = trimEnds(document.forms[0].company.value);
	 var street = trimEnds(document.forms[0].street.value);
	 var city = trimEnds(document.forms[0].city.value);
	 var zip = trimEnds(document.forms[0].zip.value);

	if (name1 == "" )
	{ 
		alert("Please enter your First Name");
		document.forms[0].first_name.focus();
		return false;
	}
	  
	if (name2 == "" )
	{ 
		alert("Please enter your Last Name");
		document.forms[0].last_name.focus();
		return false;
	}	
	
	var emailPat=/^(.+)@(.+)$/	
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=email.match(emailPat)
	
	if (matchArray==null) 
	{
		alert("Email address seems incorrect (check @ and .'s)")
		document.forms[0].email.focus();
		return false;
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	
	if (user.match(userPat)==null) 
	{
		// user is not valid
		alert("The username doesn't seem to be valid.")
		document.forms[0].email.focus();
		return false;
	}
	
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) 
	{
		 // this is an IP address
		 for (var i=1;i<=4;i++) 
		 {
			if (IPArray[i]>255) 
			{
				alert("Destination IP address is invalid!");
				document.forms[0].email.focus();
				return false;
			}
		 }
		 return true;
	 }

	 // Domain is symbolic name
	 var domainArray=domain.match(domainPat)
	 if (domainArray==null) 
	 {
		alert("The domain name doesn't seem to be valid.");
		document.forms[0].email.focus();
		return false;
	}

	if (tel == "" )
	{
		alert("Please enter your Telephone number.");
		document.forms[0].phone.focus();
		return false;
	}

	if (title == "" )
	{ 
		alert("Please enter title");
		document.forms[0].title.focus();
		return false;
	}
	

	if (company == "" )
	{
		alert("Please enter your Company Name.");
		document.forms[0].company.focus();
		return false;
	}

	if (street == "" )
	{
		alert("Please enter your address.");
		document.forms[0].street.focus();
		return false;
	}


	if (city == "" )
	{
		alert("Please enter your City Name.");
		document.forms[0].city.focus();
		return false;
	}

	var State = document.userdetails.state.options.selectedIndex;
	if(document.userdetails.country.options[document.userdetails.country.options.selectedIndex].text == "USA")
	{
		if(State == 0)
		{
			alert("Select your State.");
			document.userdetails.state.disabled=false;
			return false;
		}
	}
	else if(document.userdetails.country.options[document.userdetails.country.options.selectedIndex].text == "Canada")
	{
		if(State == 0)
		{
			alert("Select your Province.");
			document.userdetails.state.disabled=false;
			return false;
		}
	}

	if (zip == "" )
	{
		alert("Please enter your Zip code.");
		document.forms[0].zip.focus();
		return false;
	}

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) 
	{
		// the address must end in a two letter or three letter word.
		alert("The address must end in a three-letter domain, or two letter country.")
		document.forms[0].email.focus();
		return false;
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) 
	{
		var errStr="This address is missing a hostname!"
		alert(errStr);
		document.forms[0].email.focus();
		return false
	}

	var atInd = email.indexOf("@");
	email = email.substring(atInd + 1, email.length);
	// alert(email);
	var dotInd = email.indexOf(".");
	var email1=email.substring(dotInd,email.length);
	email = email.substring(0, dotInd);
	email = email.toLowerCase();
	var sublength=email1.length;
	var domains=new Array("hotmail" ,"yahoo", "aol" ,"lycos" ,"rediff" ,"rediffmail" ,"msn" ,"juno" ,"excite" ,"cable" ,"techie" ,"europe" ,"usa" ,"earthlink" ,"myself" ,"cheerful" ,"writeme" ,"2die4" ,"artlover" ,"catlover" ,"bikerider" ,"doglover" ,"hot-shot" ,"cutey" ,"gardener" ,"lovelable" ,"playful" ,"pornstar" ,"seductive" ,"soon" ,"whoever" ,"poetic" ,"inorbit" ,"witty" ,"yours" ,"add gmx" ,"gmail" ,"mailinator" ,"fastmail" ,"netscape" ,"india" ,"indiatimes" ,"yandex" ,"web" ,"mail15" ,"prodigy" ,"wans" ,"flash" ,"sbcglobal" ,"sify" ,"netzero" ,"comcast" ,"spamgourmet" ,"bigfoot" ,"gmx" , "arcsight" , "netforensics" , "netiq" , "ca" , "gfi" , "esecurityinc" , "intellitactics" , "network-intelligence" , "micromuse" , "consul" , "sensage" , "openservice" , "trigeo" , "loglogic" , "marshal")
   
	if(email1==".ru" && sublength==3)
	{
		alert("We do not accept non-company personal or public domain email accounts.\nPlease provide your official eMail ID at your company to download the free trial.");
		document.forms[0].email.focus();
		return false;
	}

	var i;
	for(i=0;domains[i]!=null;i++)
	{
		//document.write(domains[i]+"<BR>");
		email=email.toUpperCase()
		domains[i]=domains[i].toUpperCase();
		if(email==domains[i])
		{
			alert("We do not accept non-company personal or public domain email accounts.\nPlease provide your official eMail ID at your company to download the free trial.");
			//document.write("<B>"+domains[i]+"</B>"+"<BR>");
			document.forms[0].email.focus();
			return false;
			//break;
		}
	}
	
	//CreateCookies();
	
	var product, description1;
	description1 = document.getElementById("details").value;
	product = document.getElementById("00N30000000l7pn").value;
	//CreateRequestCookies();
	
	if(document.userdetails.country.options[document.userdetails.country.options.selectedIndex].text!="USA" && document.userdetails.country.options[document.userdetails.country.options.selectedIndex].text != "Canada")
	 {
		document.userdetails.state.value = "";		
	 }
	//alert(document.userdetails.state.value);

	document.getElementById("00N30000000ytry").value = document.getElementById("details").value;
	//document.userdetails.action="http://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8"
	document.userdetails.action="/cgi-local/olf_download_eval.asp"
	document.userdetails.submit();
}

function showcountrystates(theform)
{	
	if(theform.country.options.selectedIndex != -1)
	if(theform.country.options[theform.country.options.selectedIndex].text == "USA" || theform.country.options[theform.country.options.selectedIndex].text == "Canada")
	{
		loadValues();
		theform.state.disabled=false;
	}
	else
	{
		loadValues();
		theform.state.disabled=true;
	}
}

function loadValues()
{
	//alert(document.userdetails.state.options.length);
	while(document.userdetails.state.options.length > 0)
	{
		document.forms[0].state.options[0] = null;
	}
	if(document.userdetails.country.options[document.userdetails.country.options.selectedIndex].text == "USA")
	{
		for(i=0; i<usaArr.length; i++)
		{
			len = document.forms[0].state.options.length;
			tempOpt = new Option();
			tempOpt.text = tempOpt.value = usaArr[i];
			document.forms[0].state.options[len] = tempOpt;
		}
	}
	else if(document.userdetails.country.options[document.userdetails.country.options.selectedIndex].text == "Canada")
	{
		for(i=0; i<canadaArr.length; i++)
		{
			len = document.forms[0].state.options.length;
			tempOpt = new Option();
			tempOpt.text = tempOpt.value = canadaArr[i];
			document.forms[0].state.options[len] = tempOpt;
		}
	}
	else
	{
		for(i=0; i<1; i++)
		{
			len = document.forms[0].state.options.length;
			tempOpt = new Option();
			tempOpt.text = tempOpt.value = usaArr[i];
			document.forms[0].state.options[len] = tempOpt;
		}
	}

	document.getElementById("first_name").focus();
}

//variables used in fucntions...


var download_olf = "Download - OLF";

//download
var url_download_olf = "http://www.openlogformat.org/download/olf_download.shtml";

//download Feedback pages
var retURL_download_olf = "http://www.openlogformat.org/download/olf_download_eval.shtml";

function loadHiddenValues()
{
	var filepath = window.location.pathname;
	var paths = filepath.split("/");
	var filename = paths[paths.length-1];
	var file = filename.split(".");
	var removeext = file[0];
	var result = removeext.split("_");
	var product = result[0];
	var description1 = result[1];
	//var description1;

	loadProAnddescription(description1.toLowerCase(),product.toLowerCase());	
	//CheckCookies(description1.toLowerCase(),product.toLowerCase());
}



function loadProAnddescription(description1,product)
{	

	if(product=="olf")
	{
		if(description1=="download")
		{
			document.getElementById("00N30000000l7pn").value = "Download - OLF";
			document.getElementById("details").value = download_olf;
			document.getElementById("retURL").value = retURL_download_olf;
		}
	}

}

function loadFormPage(description1,product)
{
	
	if(product=="olf")
	{		
		alert(description1);

		if(description1=="download")
		{			
			window.location = url_download_olf;
		}
	}

}
