

  function getCookie(name)
 	{ 
	var DC = document.cookie;
	var index = DC.indexOf(name + "=");
    if (index == -1) return null;
    index = DC.indexOf("=", index) + 1;
    var endstr = DC.indexOf(";", index);
    if (endstr == -1) endstr = DC.length;
    return unescape(DC.substring(index, endstr));
  	}
  
  // Function setCookie sets the cookie for the site. This cookie is set for  160 days.
  
  function setCookie(name, value)
   { 
   	var DC = document.cookie;
	var today = new Date();
  	var expiry = new Date(today.getTime() + 160 * 24 * 60 * 60 * 1000); // plus 160 days

	var path = "/";

    if (value != null && value != "")
    {  
		document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString() + "; path=" + path  
		DC = document.cookie; 
	}
  }

// Function Check() is wtitten is called from the load event of the body. This function sets the value of all the fields.

function Check()
{
	document.cookie = 'formsubmit= ; expires=Thu, 2 Mar 2006 20:47:11 UTC; path=/';
	var str = getCookie("VALSET")
	//selectstate(document.getElementById('Country').options[document.getElementById('Country').selectedIndex].value);	
	//if(str != null)
	//{
		GetAllValuesOnForm();
	//}
	//alert(document.getElementById('Country').options[document.getElementById('Country').selectedIndex].value);
	selectstate(document.getElementById('Country').options[document.getElementById('Country').selectedIndex].value)
	GetAllValuesOnForm();
}

// Function SetAllValInCookies() will be called when any form is being submitted. This function sets the cookie for the site.

function SetAllValInCookies(myForm)
{	
	var str = getCookie("VALSET")
		
	var intStoreCookies = 0;
	if(str != null)
	{
		intStoreCookies = 1;
	}
	else
	{
		if (confirm("Is it OK to keep this information for later download requests? (If NOT 'Cancel'.)")) 
		{
			intStoreCookies = 1 ;
		}
		else
		{
			//added by santosh removed return false
			
			intStoreCookies = 0 ;
		}
	}
	if (intStoreCookies==1)
	{	
	setCookie("VALSET","true");

	var intCtr=0;
	for (intCtr=0;intCtr < myForm.elements.length - 1;intCtr++)
	{
		if (myForm.elements[intCtr].type == "text")
		{
			setCookie(myForm.elements[intCtr].name, myForm.elements[intCtr].value);
		}
		if (myForm.elements[intCtr].type == "textarea")
		{
			setCookie(myForm.elements[intCtr].name, myForm.elements[intCtr].value);
		}
		if (myForm.elements[intCtr].type == "select-one")
		{
			setCookie(myForm.elements[intCtr].name, myForm.elements[intCtr].value);
		}
		if (myForm.elements[intCtr].type == "select")
		{
			setCookie(myForm.elements[intCtr].name, myForm.elements[intCtr].value);
		}
	}
	
	}
	//Added by santosh
	else
	{
	
	
	}
	//setCookie('formsubmit','no');
	//document.cookie = "formsubmit=''; expires="+expires+"; path=/";
	//document.cookie = 'formsubmit= ; expires=Thu, 2 Mar 2006 20:47:11 UTC; path=/';

	return true;
}

function GetAllValuesOnForm()
{
	var intFormCount = 0;

	for (intFormCount=0; intFormCount < document.forms.length; intFormCount++)
	{
		var intCtr=0;
		for (intCtr=0;intCtr< document.forms[intFormCount].elements.length - 1;intCtr++)
		{
	
		//	if (document.forms[intFormCount].elements[intCtr].type == "text")
		//	{
		
			var cookievalue = ParseCookie(document.forms[intFormCount].elements[intCtr].name)
			
		
		if (cookievalue != "NULL")
		{
				document.forms[intFormCount].elements[intCtr].value = ParseCookie(document.forms[intFormCount].elements[intCtr].name)
		}
	    	//}
		}
	}
}

function ParseCookie(name)
{
	var RetVal = getCookie(name)
	if(RetVal == null)
	{
		RetVal = "NULL";
	}	
	return RetVal;
	
}

/*function written for dynamicly generation of states */

function selectstate(sel)
{

//alert(sel);
if (sel=="Canada" || sel=="United States")
{
	//alert(document.forms[0].onsubmit);
	/*if (document.forms[0].id == "IOS")
	{
	alert("training");
	}
	else
	{
	alert("normal form");
	}
	*/
	document.forms[0].onsubmit = function () { MM_validateForm('FirstName','','R','LastName','','R','Title','','R','Phone','','R','Email','','RisEmail','Company','','R','Country','','R','State','','R');return document.MM_returnValue;};
	//document.getElementById('lblstate').style.disply = 'block';
	//document.getElementById('lblstate').innerHTML = '*';
	//alert(document.forms[0].onsubmit);
	if (sel == "Canada")
	{
	document.getElementById('statetd').innerHTML = "<span class='mandatoryfield'>*</span> <select name='State' class='inputwidth278'><option value=''>Select Province</option><option value='AB'>AB</option><option value='BC'>BC</option><option value='MB'>MB</option><option value='NB'>NB</option><option value='NL'>NL</option><option value='NT'>NT</option><option value='NS'>NS</option><option value='NU'>NU</option><option value='ON'>ON</option><option value='PE'>PE</option><option value='QC'>QC</option><option value='SK'>SK</option><option value='YT'>YT</option></select>";
	}
	else if (sel == "United States")
	{
	document.getElementById('statetd').innerHTML = "<span class='mandatoryfield'>*</span> <select name='State' class='inputwidth278'><option value=''>Select State</option><option value='AL'>AL</option><option value='AK'>AK</option><option value='AS'>AS</option><option value='AZ'>AZ</option><option value='AR'>AR</option><option value='CA'>CA</option><option value='CO'>CO</option><option value='CT'>CT</option><option value='DE'>DE</option><option value='DC'>DC</option><option value='FM'>FM</option><option value='FL'>FL</option><option value='GA'>GA</option><option value='GU'>GU</option><option value='HI'>HI</option><option value='ID'>ID</option><option value='IL'>IL</option><option value='IN'>IN</option><option value='IA'>IA</option><option value='KS'>KS</option><option value='KY'>KY</option><option value='LA'>LA</option><option value='ME'>ME</option><option value='MH'>MH</option><option value='MD'>MD</option><option value='MA'>MA</option><option value='MI'>MI</option><option value='MN'>MN</option><option value='MS'>MS</option><option value='MO'>MO</option><option value='MT'>MT</option><option value='NE'>NE</option><option value='NV'>NV</option><option value='NH'>NH</option><option value='NJ'>NJ</option><option value='NM'>NM</option><option value='NY'>NY</option><option value='NC'>NC</option><option value='ND'>ND</option><option value='MP'>MP</option><option value='OH'>OH</option><option value='OK'>OK</option><option value='OR'>OR</option><option value='PW'>PW</option><option value='PA'>PA</option><option value='PR'>PR</option><option value='RI'>RI</option><option value='SC'>SC</option><option value='SD'>SD</option><option value='TN'>TN</option><option value='TX'>TX</option><option value='UT'>UT</option><option value='VT'>VT</option><option value='VI'>VI</option><option value='VA'>VA</option><option value='WA'>WA</option><option value='WV'>WV</option><option value='WI'>WI</option><option value='WY'>WY</option></select>";
	}

}

else
{
document.getElementById('statetd').innerHTML ='<span class="mandatoryfield">&nbsp;&nbsp;</span><input type="text" name="State" maxlength="45" class="inputwidth270" />';
document.forms[0].onsubmit = function () { MM_validateForm('FirstName','','R','LastName','','R','Title','','R','Phone','','R','Email','','RisEmail','Company','','R','Country','','R');return document.MM_returnValue;};
	//document.getElementById('lblstate').style.disply = 'none';
	//document.getElementById('lblstate').innerHTML = '&nbsp;';
/*document.forms[0].onSubmit = "MM_validateForm('Email','','RisEmail');return document.MM_returnValue";*/
}
/*	if (document.forms[0].attachEvent)
	{
	document.forms[0].removeEvent('onsubmit',function() {MM_validateForm('State','','R');return document.MM_returnValue}); 
	}
	else
	{
	document.forms[0].removeEventListener('submit',function() {MM_validateForm('State','','R');return document.MM_returnValue},false); 
	}
	
if (sel == "Canada" || sel == "United States")
{
	if (document.forms[0].attachEvent)
	{
	document.forms[0].attachEvent('onsubmit',function() {MM_validateForm('State','','R');return document.MM_returnValue}); 
	}
	else
	{
	document.forms[0].addEventListener('submit',function() {MM_validateForm('State','','R');return document.MM_returnValue},false); 
	}
}
	*/

}
