//Post Form Javascripts

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


// // Start of on focus js
function tes(obj){
	if(document.getElementById){
	var el = document.getElementById(obj);
	var ar = document.getElementById("testimonial").getElementsByTagName("div"); 
		
			for (var i=0; i<ar.length; i++){
				if(ar[i].className == "hepl_test") {
					ar[i].className = "hepl_test_off";
				}
			}
		document.getElementById(obj).className = "hepl_test";
	}
}


function hide_t(obj){
	if(document.getElementById){
	var el = document.getElementById(obj);
	var ar = document.getElementById("testimonial").getElementsByTagName("div"); 
		
			for (var i=0; i<ar.length; i++){
				if(ar[i].className == "hepl_test_off") {
					ar[i].className = "hepl_test_off";
				}
			}
		document.getElementById(obj).className = "hepl_test_off";
	}
}



function form_tst()
{
if(document.getElementById('form_tst1').style.display == "none")
{
document.getElementById('form_tst1').style.display=""; 
} 
else
{
document.getElementById('form_tst1').style.display="none"; 
}
}


function value_edit(input_border)
{
document.getElementById(input_border).className="select_sp_new1";
}

function value_edit1(input_border)
{
document.getElementById(input_border).className="select_sp_new";
}


function edit(form)
{
document.getElementById(form).onkeypress="return true";
}




function t_description()
{
	if(document.postForm.desc.value=='Please enter your buying requirements in detail. Provide key information like product specifications, estimated quantity etc.')
	{
		document.postForm.desc.value='';
		document.postForm.desc.className="txt-input3";
	}
}

function settext()
{
	if(document.postForm.desc.value=='')
	{
		document.postForm.desc.value='Please enter your buying requirements in detail. Provide key information like product specifications, estimated quantity etc.';
		
		document.postForm.desc.className="txt-input2";
	}
}	

function t_description1()
{
	if
	(
		document.form2.first.value=='First Name')	{
		document.form2.first.value='';
	}		
}

function settext1()
{
	if 
	(document.form2.first.value=='')
		{
		document.form2.first.value='First Name';
		}
}	

function t_description2()
{
	if
	(
		document.form2.last.value=='Last Name')	{
		document.form2.last.value='';
	}		
}

function settext2()
{
	if 
	(document.form2.last.value=='')
		{
		document.form2.last.value='Last Name';
		}
}	


function product()
{
document.getElementById('product').style.display="block";
document.getElementById('ad_m').style.display="none";
}

// // End of on focus js




//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;
	//alert(val);
	if(val == 0)
	{
		document.postForm.ph_country.value='';
		return false;
	}
	//alert(document.postForm.country_iso.value);
	if(phonecode[document.postForm.country.selectedIndex] != '')
	{
		var a = phonecode[document.postForm.country.selectedIndex].split("-");
		document.postForm.ph_country.value = a[0];

		if(a[1] != undefined)
		{
		document.postForm.ph_area.value = a[1];
		}
		else
		{
		document.postForm.ph_area.value = '';
		}
	}
	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;
	}

	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;
	}
// 	if(trim(Form.title1.value) != "")
// 	{
// 		var title = Form.title1.value;
// 		if(trim(Form.title2.value) != "")
// 			title +=","+Form.title2.value;
// 		if(trim(Form.title3.value) != "")
// 			title +=","+Form.title3.value;
// 		if(trim(Form.title4.value) != "")
// 			title +=","+Form.title4.value;
// 
// 		Form.title.value = title;
// 	}
}

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;
		}
	}

	var title='';
	if(trim(Form.title1.value) != "")
		title = Form.title1.value;
	if(trim(Form.title2.value) != "")
		title +=","+Form.title2.value;
	if(trim(Form.title3.value) != "")
		title +=","+Form.title3.value;
	if(trim(Form.title4.value) != "")
		title +=","+Form.title4.value;
	
	title = title.replace(/^,/g,'');
	Form.title.value = title;

	if(Form.title.value == "") {
		alert("Kindly enter your Products.");
		Form.title1.focus();
		return false;
	}

	if(Form.title1.value.length < 3 && trim(Form.title1.value) != "") {
		alert("Your product name should not be less than 3 charecters.");
		Form.title1.focus();
		return false;
	}
	if(Form.title2.value.length < 3 && trim(Form.title2.value) != "") {
		alert("Your product name should not be less than 3 charecters.");
		Form.title2.focus();
		return false;
	}
	if(Form.title3.value.length < 3 && trim(Form.title3.value) != "") {
		alert("Your product name should not be less than 3 charecters.");
		Form.title3.focus();
		return false;
	}
	if(Form.title4.value.length < 3 && trim(Form.title4.value) != "") {
		alert("Your product name should not be less than 3 charecters.");
		Form.title4.focus();
		return false;
	}

	if(Form.title.value.length > 100) {
		alert("Your product name should not be greater than 100 charecters.");
		Form.title1.focus();
		return false;
	}

	if(trim(Form.desc.value) == "" || trim(Form.desc.value) == "Please enter your buying requirements in detail. Provide key information like product specifications, estimated quantity etc." ) {
		alert("Kindly describe your currect requirements.");
		Form.desc.focus();
		return false;
	}

	if(Form.desc.value.length < 20) {
		alert("Your description should not be less than 20 charecters.");
		Form.desc.focus();
		return false;
	}

	if(Form.desc.value.length > 1200) {
		alert("Your description should not be greater than 1200 charecters.");
		Form.desc.focus();
		return false;
	}

	if(Form.email.value == "" ) {
		alert("Kindly enter your E-mail ID.");
		Form.email.focus();
		return false;
	}
	else if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(Form.email.value))) {
			alert("Invalid Email ID. Kindly enter the correct ID.");
			Form.email.focus();
			return (false);
	}


	if(Form.first_name.value == "" ) 
	{
		alert("Kindly enter your Name.");
		Form.first_name.focus();
		return false;
	}

	if((/[0-9]+/.test(Form.first_name.value)))
	{
		alert("Kindly enter your Name in alphabets only.");
		Form.first_name.focus();
		return false;
	}


	
	
	if(Form.first_name.value.length < 3) {
		alert("Your Name should not be less than 3 charecters.");
		Form.first_name.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.comp_name.value.length < 3) {
		alert("Your Company Name should not be less than 3 charecters.");
		Form.comp_name.focus();
		return false;
	}



	if (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.country_name.value == "" || Form.country.value == 0) 
	{
		alert("Kindly select the Country.");
		Form.country.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.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.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.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.value.length > 100) {
		alert("Company Name should not exceed 100 characters.");
		Form.comp_name.focus();
		return false;
	}

	newCookie =Form.email.value;
	newCookie +="|"+Form.salute.options[Form.salute.selectedIndex].value;
	newCookie +="|"+Form.first_name.value;
	newCookie +="|"+Form.comp_name.value;
	newCookie +="|"+Form.website.value;
	newCookie +="|"+Form.country.options[Form.country.selectedIndex].value;
	newCookie +="|"+Form.ph_country.value;
	newCookie +="|"+Form.ph_area.value;
	newCookie +="|"+Form.ph_no.value;

	setCookie("newImeshCTL",newCookie);
	return true;
}



function setCookie(name, value)
{
	expires = new Date();
	expires.setTime (expires.getTime() + 24 * 60 * 60 * 150 * 1000);

	if (value.length > 0)
	document.cookie = name + "=" + escape(value)+ ";"+"expires=" + expires.toGMTString() + ";";
}



function get()
{
	if(document.postForm.desc.value=='Please enter your buying requirements in detail. Provide key information like product specifications, estimated quantity etc.')
	{
		document.postForm.desc.className="txt-input2";
	}
	else
	{
		document.postForm.desc.className="txt-input3";
	}

	if( (cookie = getCookie("newImeshCTL")) > "")
	{
		Values = cookie.split("|");

		if (Values.length >= 9)
		{
			if (document.postForm.email.value.length == 0)
				document.postForm.email.value = Values[0];
			if(Values[1] == 'Mr.')
			{
				document.postForm.salute.selectedIndex=0;
			}
			else
			{
				document.postForm.salute.selectedIndex=1;
			}
			if (document.postForm.first_name.value.length == 0)
				document.postForm.first_name.value=Values[2];
/*			if (document.postForm.last_name.value.length == 0)
				document.postForm.last_name.value=Values[3];*/
			if (document.postForm.comp_name.value.length == 0)
				document.postForm.comp_name.value=Values[3];
			if (document.postForm.website.value.length == 0)
				document.postForm.website.value=Values[4];

			for (i=0;i<document.getElementById('country_len').value;i++)
			{
				if (document.postForm.country.options[i].value.toUpperCase() == Values[5].toUpperCase()) {
				break;
				}
			}
			if (i == document.getElementById('country_len').value)
			{
				if (document.postForm.country.selectedIndex == 0)
					document.postForm.country.selectedIndex=0;
			}
			else
			{
				if (document.postForm.country.selectedIndex == 0)
					document.postForm.country.selectedIndex=i;
			}

			if (document.postForm.ph_country.value.length == 0)
				document.postForm.ph_country.value=Values[6];
			if (document.postForm.ph_area.value.length == 0)
				document.postForm.ph_area.value=Values[7];
			if (document.postForm.ph_no.value.length == 0)
				document.postForm.ph_no.value=Values[8];

			document.postForm.country_name.value=document.postForm.country.options[document.postForm.country.selectedIndex].text;
		}
	}
return true;
}

function getCookie(Name)
{
	var search = Name + "="
	if (document.cookie.length > 0)
	{ // if there are any cookies
		offset = document.cookie.indexOf(search)
		if (offset != -1)
		{ // if cookie exists
			offset += search.length
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset)
			// set index of end of cookie value
			if (end == -1) end = document.cookie.length
			return unescape(document.cookie.substring(offset, end))
		}
	}

	return "";
}


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;
	}
}





function addEvent( obj, type, fn ){ 
   if (obj.addEventListener){ 
	  obj.addEventListener( type, fn, false );
   }
   else if (obj.attachEvent){ 
	  obj["e"+type+fn] = fn; 
	  obj[type+fn] = function(){ obj["e"+type+fn]( window.event ); } 
	  obj.attachEvent( "on"+type, obj[type+fn] ); 
   } 

}

addEvent(window, 'load', get);


