//Post Form Javascripts

// $Id: $
// $HeadURL: $

//Captcha code Starts
var d = new Date();
var epoch_time =(d.getTime()-d.getMilliseconds())/1000;
var randomnumber=Math.floor(Math.random()*9999);
var randomnumber1=Math.floor(Math.random()*9999);
var ran_num=randomnumber+"."+epoch_time+"."+randomnumber1;
var url="/cgi/get_captcha.cgi?cref="+ran_num+"&style=2";
function setCaptchaRef()
{
	document.postForm.captcha_ref.value = ran_num;
}
//Captcha code Ends

function ajaxFunction()
{
	var xmlHttp;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	
	return xmlHttp;
}

function rem_default()
{
	if(document.getElementById("txtTitle").value=="Looking for..." || document.getElementById("txtTitle").value=="Please write your prdouct name here...")
	{
		document.getElementById("txtTitle").value='';
	}
}

function get_keywords()
{
	var xmlHttp=ajaxFunction();
	var ofrTitle=document.getElementById("txtTitle").value;
	if(xmlHttp)
	{
		if(ofrTitle && ofrTitle.length > 2 && ofrTitle != "Looking for...")
		{
			xmlHttp.onreadystatechange=function()
			{
				if(xmlHttp.readyState==4)
				{
				document.getElementById("txtKeywords").value=xmlHttp.responseText;
				}
			}
			xmlHttp.open("GET","/cgi/eto-dirsearch-mcat.mp?ss="+escape(ofrTitle),true);
			xmlHttp.send(null);
		}
	}
}

function get_area()
{
	var xmlHttp=ajaxFunction();
	var city=document.getElementById("txtCity").value;
	if(xmlHttp)
	{
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
			document.getElementById("txtArea").value=xmlHttp.responseText;
			}
		}
		xmlHttp.open("GET","/cgi/GetAreaCode.mp?city="+escape(city),true);
		xmlHttp.send(null);
	}
}

function trim(sString)
{ 
	while (sString.substring(0,1) == ' ')
	{
	sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
	sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function check_count_change()
{
	var val = document.postForm.country.options[document.postForm.country.selectedIndex].value;
	if(val != document.postForm.country_iso.value)
	{
		if((document.postForm.city) && (document.postForm.city_others))
		{
			document.postForm.city.value = '';
			document.postForm.city_others.value = '';
		}
		
		if((document.postForm.state) && (document.postForm.state_others))
		{
			document.postForm.state.value = '';
			document.postForm.state_others.value = '';
		}
		
		if(phonecode[document.postForm.country.selectedIndex] != '')
		{
			document.postForm.ph_country.value = phonecode[document.postForm.country.selectedIndex];

			if(document.postForm.fax_country)
			{
				document.postForm.fax_country.value = phonecode[document.postForm.country.selectedIndex];
			}
		}
	}
	document.postForm.country_iso.value = val;
	document.postForm.country_name.value = document.postForm.country.options[document.postForm.country.selectedIndex].text;
}

function assign_hidden_param(Form)
{
	if (document.referrer && document.referrer!="")
	{
		Form.page_referrer.value=document.referrer;
	}
	
	if(location.hostname)
	{
		Form.referer_url.value="http://"+location.hostname;
	}
	
	if((Form.state) && (Form.state_others) && (Form.txtState) && (Form.txtState_SelectedValue))
	{
		if(Form.txtState_SelectedValue.value != '')
		{
			Form.state_others.value=Form.txtState.value;
			Form.state.value=Form.txtState_SelectedValue.value;
		}
		else
		{
			Form.state_others.value=Form.txtState.value;
			Form.state.value='';
		}
	}
	
	if((Form.city) && (Form.city_others) && (Form.txtCity) && (Form.txtCity_SelectedValue))
	{
		if(Form.txtCity_SelectedValue.value != '')
		{
			Form.city_others.value=Form.txtCity.value;
			Form.city.value=Form.txtCity_SelectedValue.value;
		}
		else
		{
			Form.city_others.value=Form.txtCity.value;
			Form.city.value='';
		}
	}
	
	Form.usr_pass.value = Math.floor(Math.random()*9999)+100000;
	
	var is_input = document.URL.indexOf('?');
	
	if (is_input != -1)
	{
		var x;
		var addr_str='';
		var pname='';
		var strarr = new Array();
		
		addr_str = document.URL.substring(is_input+1, document.URL.length);
		strarr = addr_str.split("&");
		
		for (x in strarr)
		{
			if((strarr[x].split("="))[0] == "ref")
			{ 
				pname = unescape((strarr[x].split("="))[1]);
			}
		}
		
		Form.ref.value = pname;
	}
}

function Check_ModReg(Form)
{
	assign_hidden_param(Form); // setting the hidden parameters of states & cites ...
	
	/* Trade offer feilds checking Starts... */

	if (Form.nature_of_business)
	{
		var check = 0;
		for(var i = 0; i < Form.nature_of_business.length; i++)
		{
			if(Form.nature_of_business[i].checked == true)
			{
				check = 1;
			}
		}
		if(check == 0)
		{
			alert("Please tell us your Nature of Business");
			Form.nature_of_business[0].focus();
			return false;
		}
	}

	if(trim(Form.title.value) == "" || trim(Form.title.value) == "Looking for..." || trim(Form.title.value) == "Please write your prdouct name here...") {
		alert("Kindly enter your Products.");
		Form.title.focus();
		return false;
	}

	if(trim(Form.desc.value) == "" || trim(Form.desc.value) == "Provide key information like product specifications, estimated quantity, delivery instructions, packaging details etc." || trim(Form.desc.value) == "Provide key information like describing what services you need, when will you need the service, how often do you require such service etc.") {
		alert("Kindly describe your correct requirements.");
		Form.desc.focus();
		return false;
	}
// 	if(Form.desc.value.length > 1200) {
// 		alert("Your description should be less than 1200 charecters.");
// 		Form.desc.focus();
// 		return false;
// 	}
	if(Form.desc.value.length > 1700) {
		alert("Your description should be less than 1700 charecters.");
		Form.desc.focus();
		return false;
	}

	/*
	if (Form.qty && Form.qty.value == "")
	{
		alert("Please enter your Quantity.");
		Form.qty.focus();
		return false;
	}
	*/
	
	/* Trade offer feilds checking Ends... */
	
	/* Gluser feilds checking Starts... */
	/*
	if(Form.comp_name.value == "" ) {
		alert("Kindly enter your Company Name.");
		Form.comp_name.focus();
		return false;
	}
	*/
	if ((Form.website) && Form.website.value.length != 0)
	{
		if(Form.website.value.indexOf(" ") != -1)	{
		alert("Kindly enter correct URL without any spaces in it.");
		Form.website.focus();
		return false;
		}
		if(Form.website.value.indexOf("@") != -1)	{
		alert("Invalid Website ! Kindly enter correct website.");
		Form.website.focus();
		return false;
		}
		if(Form.website.value.indexOf(".") == -1)	{
		alert("Invalid Website ! Kindly enter correct website.");
		Form.website.focus();
		return false;
		}
		validarr = Form.website.value.split(".");
	
		if(validarr[0].length<2)   	{
		alert("Invalid Website ! Kindly enter correct website.");
		Form.website.focus();
		return false;
		}
		if(validarr[1].length<2)   	{
		alert("Invalid Website ! Kindly enter correct website.");
		Form.website.focus();
		return false;
		}
	}
	
	if(Form.first_name.value == "" ) {
		alert("Kindly enter your First Name.");
		Form.first_name.focus();
		return false;
	}
	
	if(Form.email.value == "" ) {
		alert("Kindly enter your E-mail ID.");
		Form.email.focus();
		return false;
	}
	else if (Form.email.value.length != 0)
	{
//		var e_check  = Form.email.value;
	
		if(Form.email.value.indexOf(" ") != -1)	{
		alert("Kindly enter correct E-Mail ID without any spaces in it.");
		Form.email.focus();
		return false;
		}
	
		if(Form.email.value.indexOf("@") == -1)	{
		alert("Invalid E-Mail ID! Kindly enter correct E-Mail ID.");
		Form.email.focus();
		return false;
		}
		if(Form.email.value.indexOf(",") != -1)	{
		alert("Invalid E-Mail ID! Kindly enter correct E-Mail ID.");
		Form.email.focus();
		return false;
		}
		if(Form.email.value.indexOf(")") != -1)	{
		alert("Invalid E-Mail ID! Kindly enter correct E-Mail ID.");
		Form.email.focus();
		return false;
		}
		if(Form.email.value.indexOf("(") != -1)	{
		alert("Invalid E-Mail ID! Kindly enter correct E-Mail ID.");
		Form.email.focus();
		return false;
		}
		validarr = Form.email.value.split("@");
	
		if(validarr[0].length==0)   	{
		alert("Invalid E-Mail ID! Kindly enter the correct E-mail ID.");
		Form.email.focus();
		return false;
		}
		if(validarr[0].length != 0)   	{
			if(validarr[0].indexOf(".") != -1)     	{
			validemail = validarr[0].split(".");
			if(validemail[0].length<2)   		{
			alert("Invalid E-Mail ID! Kindly enter the correct E-mail ID.");
			Form.email.focus();
			return false;
			}
			if(validemail[1].length<2)  		{
			alert("Invalid E-Mail ID! Kindly enter the correct E-mail ID.");
			Form.email.focus();
			return false;
			}
			}
		}   // end of of validemail
		if(validarr[1].indexOf("@") >=0)   	{
		alert("Invalid E-Mail ID! Kindly enter the correct E-mail ID.");
		Form.email.focus();
		return false;
		}
		if(validarr[1].length==0)   	{
		alert("Invalid E-Mail ID! Kindly enter the correct E-mail ID.");
		Form.email.focus();
		return false;
		}
		if(validarr[1].length != 0)   	{
	
			if(validarr[1].indexOf(".") == -1)     	{
			alert("Invalid E-Mail ID! Kindly enter the correct E-mail ID.");
			Form.email.focus();
			return false;
			}
			validemail = validarr[1].split(".");
			if(validemail[0].length<2)   		{
			alert("Invalid E-Mail ID! Kindly enter the correct E-mail ID.");
			Form.email.focus();
			return false;
			}
			if(validemail[1].length<2)  		{
			alert("Invalid E-Mail ID! Kindly enter the correct E-mail ID.");
			Form.email.focus();
			return false;
			}
		}   // end of of validemail
	
	} // end of valid email-id check
	
/*	if(trim(Form.str_add1.value) == "") {
		alert("Kindly enter your Address.");
		Form.str_add1.focus();
		return false;
	}
*/	
	if(Form.country_name.value == "") {
		alert("Kindly select the Country.");
		Form.country.focus();
		return false;
	}
	
/*	if(trim(Form.city_others.value) == "") {
		alert("Kindly enter your City.");
		Form.txtCity.focus()
		return false;
	}
	
	if(trim(Form.zip.value) == "" ) {
		alert("Kindly enter the Zip/Pin Code.");
		Form.zip.focus();
		return false;
	}
*/	
	if(Form.ph_country.value == "") {
		alert("Kindly enter country code for Telephone Number.");
		Form.ph_country.focus();
		return false;
	}
	else if(Form.ph_no.value == "") {
		alert("Kindly enter your Telephone Number.");
		Form.ph_no.focus();
		return false;
	}

	//fax start
	if(Form.fax_country && Form.fax_country.value.length > 6) 
	{
		alert("Fax (Country Code) should not exceed 6 characters.");
		Form.fax_country.focus();
		return false;
	}
	if(Form.fax_area && Form.fax_area.value !='') 
	{
		if(isNaN(Form.fax_area.value))
		{
			alert("Fax (Area Code) should be an integer value.");
			Form.fax_area.focus();
			return false;
		}

		if(Form.fax_area.value.length > 6) 
		{
			alert("Fax (Area Code) should not exceed 6 characters.");
			Form.fax_area.focus();
			return false;
		}
	}
	
	if(Form.fax_no && Form.fax_no.value.length > 35) 
	{
		alert("Fax (Number) should not exceed 35 characters.");
		Form.fax_no.focus();
		return false;
	}
	//fax end

	if ((Form.captcha_text) && Form.captcha_text.value.length == 0)  {
			alert ("Kindly enter the code as appearing in the Image.");
			Form.captcha_text.focus();
			return false;
	}

	if (Form.first_name.value.length  > 20)
	{
		alert("First Name should not exceed 20 characters.");
		Form.first_name.focus();
		return false;
	}
	if ((Form.last_name) && Form.last_name.value.length  > 20)
	{
		alert("Last Name should not exceed 20 characters.");
		Form.last_name.focus();
		return false;
	}
	if (Form.email.value.length > 100)
	{
		alert("Email should not exceed 100 characters.");
		Form.email.focus();
		return false;
	}
	
	if(Form.country_name.value.length > 40) {
		alert("Country Name should not exceed 40 characters.");
		Form.country_name.focus();
		return false;
	}
	if(Form.ph_country.value.length > 6) {
		alert("Telephone (Country Code) should not exceed 6 characters.");
		Form.ph_country.focus();
		return false;
	}
	if((Form.ph_area) && Form.ph_area.value !='') {
		if(isNaN(Form.ph_area.value))
		{
			alert("Telephone (Area Code) should be an integer value.");
			Form.ph_area.focus();
			return false;
		}
	}
	if((Form.ph_area) && Form.ph_area.value.length > 6) {
		alert("Telephone (Area Code) should not exceed 6 characters.");
		Form.ph_area.focus();
		return false;
	}
	if(Form.ph_no.value.length > 35) {
		alert("Telephone (Number) should not exceed 35 characters.");
		Form.ph_no.focus();
		return false;
	}
	
	if(Form.comp_name && Form.comp_name.value.length > 100) {
		alert("Company Name should not exceed 100 characters.");
		Form.comp_name.focus();
		return false;
	}
}

function productcode()
{
	//Getting parameter name from url starts
	var is_input = document.URL.indexOf('?');

	var code = '';
	if (is_input != -1)
	{
		addr_str = document.URL.substring(is_input+1, document.URL.length);
		strarr = addr_str.split("&");
		var param_nam = unescape((strarr[0].split("="))[0]);
		if(param_nam == "code")
		{
			code = unescape((strarr[0].split("="))[1]);
			code = code.replace(/\+/g," ");
		}
	}
	//Getting parameter name from url ends
	//End
	if(code)
	{
		document.postForm.title.value = code;
	}
}

