//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 makeCookie()
{
	var newCookie='';

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

function getCookieValues()
{
	if( (cookie = readCookie("ImeshVisitor")) > "")	
	{
		if( getparamVal(cookie, "slt") != "" )
		{
			var sltVal = getparamVal(cookie, "slt");
			sltVal = getparamVal(cookie, "slt").replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
			for (var i = document.dataform.S_salute.options.length -1; i > -1; i--) 
			{
              			if ( document.dataform.S_salute.options[i].value == sltVal) 
				{
					document.dataform.S_salute.value = sltVal;
					break;
				}
            		}
		}
		if( getparamVal(cookie, "fn") != "" )
		{
			if(document.dataform.S_name){
				document.dataform.S_name.value = getparamVal(cookie, "fn");
			}
		}
		if ( getparamVal(cookie, "ln") != "" )
		{
			if(document.dataform.S_lname){
				document.dataform.S_lname.value = getparamVal(cookie, "ln");
			}
		}
		if ( getparamVal(cookie, "em") != "" )
		{
			if(document.dataform.S_email){
				document.dataform.S_email.value = getparamVal(cookie, "em");
			}
		}
		if ( getparamVal(cookie, "co") != "" ) 
		{
			if(document.dataform.S_organization){
				document.dataform.S_organization.value = getparamVal(cookie, "co");
			}
		}
		if ( getparamVal(cookie, "phcc") != "" ) 
		{
			if(!getparamVal(cookie, "phcc").match(/\+/))
			{
				if(document.dataform.S_phone_country_code){
					document.dataform.S_phone_country_code.value =  '+' + getparamVal(cookie, "phcc");
				}
				if(document.dataform.S_cmobile){
					document.dataform.S_cmobile.value = '+' + getparamVal(cookie, "phcc");
				}
			}
			else
			{
				if(document.dataform.S_phone_country_code){
					document.dataform.S_phone_country_code.value =  getparamVal(cookie, "phcc");
				}
				if(document.dataform.S_cmobile){
					document.dataform.S_cmobile.value = getparamVal(cookie, "phcc");
				}
			}
		}
		if ( getparamVal(cookie, "phac") != "" ) 
		{
			if(document.dataform.S_phone_area_code){
				document.dataform.S_phone_area_code.value = getparamVal(cookie, "phac");
			}
		}
		if ( getparamVal(cookie, "ph1") != "" ) 
		{
			if(document.dataform.S_phone){
				document.dataform.S_phone.value = getparamVal(cookie, "ph1");
			}	
		}
		if ( getparamVal(cookie, "mb1") != "" ) 
		{
			if(document.dataform.S_mobile){
				document.dataform.S_mobile.value = getparamVal(cookie, "mb1");
			}	
		}
		if ( getparamVal(cookie, "url") != "" ) 
		{
			if(document.dataform.Website){
				document.dataform.Website.value = getparamVal(cookie, "url");
			}
		}
		if ( getparamVal(cookie, "cn") != "" ) 
		{
			if(document.dataform.S_country){
				document.dataform.S_country.value = getparamVal(cookie, "cn");
			}
		}
		if( getparamVal(cookie, "iso") != "" || getparamVal(cookie, "cnv") != "" ) 
		{
			var ccode = getparamVal(cookie, "iso") || getparamVal(cookie, "cnv");
			for (var i = document.dataform.country.options.length -1; i > -1; i--) 
			{
              			if ( document.dataform.country.options[i].value == ccode) 
				{
					document.dataform.country.value = ccode;
					break;
				}
            		}
			if(document.dataform.country_iso){
				document.dataform.country_iso.value = ccode;
			}
		}
		if ( getparamVal(cookie, "ad") != "" ) 
		{
			if(document.dataform.S_streetaddress){
				document.dataform.S_streetaddress.value = getparamVal(cookie, "ad");
			}
		}
		if ( getparamVal(cookie, "ct") != "" ) 
		{
			if(document.dataform.S_city){
				document.dataform.S_city.value = getparamVal(cookie, "ct");
			}
		}
		if ( getparamVal(cookie, "st") != "" ) 
		{
			if(document.dataform.S_state){
				document.dataform.S_state.value = getparamVal(cookie, "st");
			}
		}
		if ( getparamVal(cookie, "zp") != "" ) 
		{
			if(document.dataform.S_pin){
				document.dataform.S_pin.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 "";
	}
}
