//standard function that understands only dataform & S_ field names
// to call the same cookie for non-standard forms call makecookie1(form_name, elementHash) function

function elementHash()
{
	this.length = 0;
	this.items = new Array();
	for (var i = 0; i < arguments.length; i += 2) {
		if (typeof(arguments[i + 1]) != 'undefined') {
			this.items[arguments[i]] = arguments[i + 1];
			this.length++;
		}
	}

   	this.get = function(in_key)//find in_key in the hash and return its in_value that was set using .put function (ie send the default key name)
	{
		return this.items[in_key];
	}
}

//JS hash with global name and actual field name in form respectively

var customElementNamesHash = new elementHash("S_name","S_first_name","S_lname","S_last_name", "S_email", "S_email", "S_organization", "S_organization", "country", "S_countryname", "S_country", "S_country", "S_phone_country_code", "S_ccode", "S_phone_area_code", "S_acode", "S_phone", "S_phone", "S_mobile", "S_mobile", "Website" , "S_website", "S_salute", "S_salute", "S_cmobile", "S_cmobile", "S_streetaddress", "S_streetaddress", "S_city", "S_city", "S_state", "S_state", "S_pin", "S_pin");
var myObj=customElementNamesHash;

function makeCookie(form_name)
{
	var newCookie='';

	if (eval("document."+form_name+"."+myObj.get('S_name')))
	{
		var usrfname = (eval("document."+form_name+"."+myObj.get('S_name')+".value") == 'First Name')
				? '' : eval("document."+form_name+"."+myObj.get('S_name')+".value");
		newCookie  = "|fn="+usrfname;
	}
	if (eval("document."+form_name+"."+myObj.get('S_lname')))
	{
		var usrlname = (eval("document."+form_name+"."+myObj.get('S_lname')+".value") == 'Last Name') 
				? '' : eval("document."+form_name+"."+myObj.get('S_lname')+".value");
		newCookie +="|ln="+usrlname;
	}
	if (eval("document."+form_name+"."+myObj.get('S_email')))
	{
		newCookie +="|em="+eval("document."+form_name+"."+myObj.get('S_email')+".value");
	}
	if (eval("document."+form_name+"."+myObj.get('S_organization')))
	{
		newCookie +="|co="+eval("document."+form_name+"."+myObj.get('S_organization')+".value");
	}
	if (eval("document."+form_name+"."+myObj.get('country')))
	{
		newCookie +="|cn="+eval("document."+form_name+"."+myObj.get('country')+".value");
	}
	if (eval("document."+form_name+"."+myObj.get('S_country')))
	{
		newCookie +="|iso="+eval("document."+form_name+"."+myObj.get('S_country')+".value");
	}
	if (eval("document."+form_name+"."+myObj.get('S_phone_country_code')))
	{
		newCookie +="|phcc="+eval("document."+form_name+"."+myObj.get('S_phone_country_code')+".value");
	}
	if (eval("document."+form_name+"."+myObj.get('S_phone_area_code')))
	{
		var usrphacode = (eval("document."+form_name+"."+myObj.get('S_phone_area_code')+".value") == 'Area Code') 
				  ? '' : eval("document."+form_name+"."+myObj.get('S_phone_area_code')+".value");
		newCookie +="|phac="+usrphacode;
	}
	if (eval("document."+form_name+"."+myObj.get('S_phone')))
	{
		var usrphone = (eval("document."+form_name+"."+myObj.get('S_phone')+".value") == 'Phone Number') 
				? '' : eval("document."+form_name+"."+myObj.get('S_phone')+".value")
		newCookie +="|ph1="+usrphone;
	}
	if (eval("document."+form_name+"."+myObj.get('S_mobile')))
	{
		var usrmobile = (eval("document."+form_name+"."+myObj.get('S_mobile')+".value") == 'Mobile Number' || eval("document."+form_name+"."+myObj.get('S_mobile')+".value") == 'Mobile / Cell Phone Number') ? '' : eval("document."+form_name+"."+myObj.get('S_mobile')+".value");
		newCookie +="|mb1="+usrmobile;
	}
	if (eval("document."+form_name+"."+myObj.get('Website')))
	{
		newCookie +="|url="+eval("document."+form_name+"."+myObj.get('Website')+".value");
	}
	if (eval("document."+form_name+"."+myObj.get('S_salute')))
	{
		newCookie +="|slt="+eval("document."+form_name+"."+myObj.get('S_salute')+".value");
	}
	if (eval("document."+form_name+"."+myObj.get('S_streetaddress')))
	{
		newCookie +="|ad="+eval("document."+form_name+"."+myObj.get('S_streetaddress')+".value");
	}
	if (eval("document."+form_name+"."+myObj.get('S_city')))
	{
		var usercity = (eval("document."+form_name+"."+myObj.get('S_city')+".value") == 'City') 
				? '' : eval("document."+form_name+"."+myObj.get('S_city')+".value") 
		newCookie +="|ct="+usercity;
	}
	if (eval("document."+form_name+"."+myObj.get('S_state')))
	{
		var usrstate = (eval("document."+form_name+"."+myObj.get('S_state')+".value") == 'State') 
				? '' : eval("document."+form_name+"."+myObj.get('S_state')+".value")
		newCookie +="|st="+usrstate;
	}
	if (eval("document."+form_name+"."+myObj.get('S_pin')))
	{
		var usrpipncode = (eval("document."+form_name+"."+myObj.get('S_pin')+".value") == 'Postal Code') 
				  ? '' : eval("document."+form_name+"."+myObj.get('S_pin')+".value")
		newCookie +="|zp="+usrpipncode;
	}

	newCookie +="|";

	return newCookie;
}

function setGlobalCookie(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() + ";" + "domain=.indiamart.com;path=/;";
	}
}

function getCookieValues(form_name)
{
	if( (cookie = readCookie("ImeshVisitor")) != "")	
	{
		if( getparamVal(cookie, "slt") != "" )
		{
			var v_salute = myObj.get('S_salute');
			var sltVal = getparamVal(cookie, "slt");
			sltVal = getparamVal(cookie, "slt").replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
			for (var i = eval("document."+form_name+"." + v_salute).options.length -1; i > -1; i--) 
			{
              			if ( eval("document."+form_name+"." + v_salute).options[i].value == sltVal) 
				{
					eval("document."+form_name+"." + v_salute).value = sltVal;
					break;
				}
            		}
		}
		if( getparamVal(cookie, "fn") != "" )
		{
			var sname = myObj.get('S_name');
			if(eval("document."+form_name+"." + sname)){
				eval("document."+form_name+"." + sname).value = getparamVal(cookie, "fn");
			}
		}
		if ( getparamVal(cookie, "ln") != "" )
		{
			var lname = myObj.get('S_lname');
			if(eval("document."+form_name+"." + lname)){
				eval("document."+form_name+"." + lname).value = getparamVal(cookie, "ln");
			}
		}
		if ( getparamVal(cookie, "em") != "" )
		{
			var email = myObj.get('S_email');
			if(eval("document."+form_name+"." + email)){
				eval("document."+form_name+"." + email).value = getparamVal(cookie, "em");
			}
		}
		if ( getparamVal(cookie, "co") != "" ) 
		{
			var company = myObj.get('S_organization');
			if(eval("document."+form_name+"." + company)){
				eval("document."+form_name+"." + company).value = getparamVal(cookie, "co");
			}
		}
		if ( getparamVal(cookie, "phcc") != "" ) 
		{
			var phcncode = myObj.get('S_phone_country_code');
			var mobileareacode = myObj.get('S_cmobile');

			if(!getparamVal(cookie, "phcc").match(/\+/))
			{
				if(eval("document."+form_name+"." + phcncode)){
					eval("document."+form_name+"." + phcncode).value =  '+' + getparamVal(cookie, "phcc");
				}
				if(eval("document."+form_name+"." + mobileareacode)){
					eval("document."+form_name+"." + mobileareacode).value = '+' + getparamVal(cookie, "phcc");
				}
			}
			else
			{
				if(eval("document."+form_name+"." + phcncode)){
					eval("document."+form_name+"." + phcncode).value =  getparamVal(cookie, "phcc");
				}
				if(eval("document."+form_name+"." + mobileareacode)){
					eval("document."+form_name+"." + mobileareacode).value = getparamVal(cookie, "phcc");
				}
			}
		}
		if ( getparamVal(cookie, "phac") != "" ) 
		{
			var phacode = myObj.get('S_phone_area_code');
			if(eval("document."+form_name+"." + phacode)){
				eval("document."+form_name+"." + phacode).value = getparamVal(cookie, "phac");
			}
		}
		if ( getparamVal(cookie, "ph1") != "" ) 
		{
			var phone1 = myObj.get('S_phone');
			if(eval("document."+form_name+"." + phone1)){
				eval("document."+form_name+"." + phone1).value = getparamVal(cookie, "ph1");
			}	
		}
		if ( getparamVal(cookie, "mb1") != "" ) 
		{
			var mobile1 = myObj.get('S_mobile');
			if(eval("document."+form_name+"." + mobile1)){
				eval("document."+form_name+"." + mobile1).value = getparamVal(cookie, "mb1");
			}	
		}
		if ( getparamVal(cookie, "url") != "" ) 
		{
			var website = myObj.get('Website');
			if(eval("document."+form_name+"." + website)){
				eval("document."+form_name+"." + website).value = getparamVal(cookie, "url");
			}
		}
		if ( getparamVal(cookie, "cn") != "" ) 
		{
			var country = myObj.get('country'); // country text value
			if(eval("document."+form_name+"." + country)){
				eval("document."+form_name+"." + country).value = getparamVal(cookie, "cn");
			}
		}
		if ( getparamVal(cookie, "iso") != "" || getparamVal(cookie, "cnv") != "" ) 
		{
			var countryVal = myObj.get('S_country'); // country value
			if(eval("document."+form_name+"." + countryVal)){
				eval("document."+form_name+"." + countryVal).value=getparamVal(cookie, "iso") || getparamVal(cookie, "cnv");
			}
		} else {
			// if iso OR cnv is blank then we need to put blank country field
			var country = myObj.get('country'); // country text value
			eval("document."+form_name+"." + country).value = "";
		}
		if ( getparamVal(cookie, "ad") != "" ) 
		{
			var address = myObj.get('S_streetaddress');
			if(eval("document."+form_name+"." + address)){
				eval("document."+form_name+"." + address).value = getparamVal(cookie, "ad");
			}
		}
		if ( getparamVal(cookie, "ct") != "" ) 
		{
			var city = myObj.get('S_city');
			if(eval("document."+form_name+"." + city)){
				eval("document."+form_name+"." + city).value = getparamVal(cookie, "ct");
			}
		}
		if ( getparamVal(cookie, "st") != "" ) 
		{
			var state = myObj.get('S_state');
			if(eval("document."+form_name+"." + state)){
				eval("document."+form_name+"." + state).value = getparamVal(cookie, "st");
			}
		}
		if ( getparamVal(cookie, "zp") != "" ) 
		{
			var zipcode = myObj.get('S_pin');
			if(eval("document."+form_name+"." + zipcode)){
				eval("document."+form_name+"." + zipcode).value = getparamVal(cookie, "zp");
			}
		}
		
	}

	return true;
}


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


function getparamVal(cookieStr, key)
{
	if( cookieStr > "")	
	{
		var val = "|"+cookieStr+"|";
		var pattern = new RegExp(".*?\\|"+key+"=([^|]*).*|.*");
		return val.replace(pattern, "$1");
	}
	else 
	{
		return "";
	}
}
