var ROOTPATH;
var xmlHttp;
var UPVS_PARTNER_ID;
var DIVNAME = "upvs_quote_area";
var requestURL;
var act="";
var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0; 
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0; 
var is_opera = ((navigator.userAgent.indexOf("Opera 6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0; 
var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0;

function fetch_remote_page(url,action)
{ 
if (url.length > 0)
	{
		act=action;
		requestURL = url;
		xmlHttp = GetXmlHttpObject(stateChangeHandler);
		xmlHttp_Get(xmlHttp, requestURL);
	}
	else 
	{
		alert('Invalid data');
	}
}

function stateChangeHandler()
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete')
	{
		var str = xmlHttp.responseText;
		//alert(str);
      
		str=str.split(">");
	
		switch(act)
		{
			case "getstate":
				//Getting State Dynamically 
				if(str!="")
				{
					if(str[1]!="")
					{
						var tmpArr = str[1].split("~");
						var StateValue=tmpArr[0].split(",");
						var length=StateValue.length;
						var StateID=tmpArr[1].split(",");
						var Load=tmpArr[2];
					}
					var CountryID=str;
					document.getElementById('StateID').length=0;
					document.getElementById('StateID').options[0]=new Option("Select State","0",false,false);
					for(i=0;i<length;i++)
					{
						if(StateValue[i]!="" && StateID[i]!="")
						{
							document.getElementById("StateID").options[eval(i+1)]=new Option(trim(StateValue[i]),StateID[i],false,false);
							count=false;
						}
						else
						{
							count=true;
						}
						
					}
					
					if(Load=="LOAD")
						get_state(requestURL,221,'state','StateName','StateID','CountryID','getmemberstate','NOTLOAD');
				}
			break;
			case "GetSelectedState":
				if(str[1]!="")
					{
						
					var tmpArr = str[1].split("~");
					var StateValue=tmpArr[0].split(",");
					var length=StateValue.length;
					var StateID=tmpArr[1].split(",");
					var Load=tmpArr[2];
					}
					var CountryID=str;
				var StateSelected=tmpArr[3];
				//var CurrencyCode = tmpArr[4];
				//Getting State Dynamically 
//				
				if(str!="")
				{
					
					document.getElementById("StateID").length=0;
					document.getElementById("StateID").options[0]=new Option("Select State","0",false,false);
					for(i=0;i<StateValue.length;i++)
					{
						if(StateValue[i]!="" && StateID[i]!="")
						{
							if(StateSelected==StateID[i])
							document.getElementById("StateID").options[eval(i+1)]=new Option(trim(StateValue[i]),StateID[i],false,true);
							else
							document.getElementById("StateID").options[eval(i+1)]=new Option(trim(StateValue[i]),StateID[i],false,false);
							count=false;
						}
						else
						{
							count=true;
						}
						
					}					
					if(count==false)
					{
						if(StateSelected=="0")
							document.getElementById("StateID").options[eval(i+1)]=new Option("Other","other",false,true);
						else
							document.getElementById("StateID").options[eval(i+1)]=new Option("Other","other",false,false);
					}
					else
					{
						if(StateSelected=="0")
							document.getElementById("StateID").options[1]=new Option("Other","other",false,true);
						else
							document.getElementById("StateID").options[1]=new Option("Other","other",false,false);
					}
					
					//get_state_currency(requestURL,CountryID,'CountryID','GetSelectedStateCurrency');
				}
			break;
		}
	}
}

function xmlHttp_Get(xmlhttp, url)
{
	xmlhttp.open('GET', url, true);
	xmlhttp.send(null);
}

function GetXmlHttpObject(handler)
{
	var objXmlHttp = null;
	if (is_ie)
	{
		var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP';
		try
		{
			objXmlHttp = new ActiveXObject(strObjName);
			objXmlHttp.onreadystatechange = handler;
		}
		catch(e)
		{
			alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled');
			return;
		}
	}
	else if (is_opera)
	{
		alert('Opera detected. The page may not behave as expected.');
		return;
	}
	else
	{
		objXmlHttp = new XMLHttpRequest();
		objXmlHttp.onload = handler;
		objXmlHttp.onerror = handler;
	}
	return objXmlHttp;
}

function Initialize(){var initstring = "";initstring+="<style type='text/css'>";initstring+=".body {";initstring+="font-family: Verdana, Arial, Helvetica, sans-serif;";initstring+="font-size: 12px;";initstring+="font-weight: normal;";initstring+="color: #666666;";initstring+="}";initstring+=".btn{";initstring+="font-family: Verdana, Arial, Helvetica, sans-serif;";initstring+="font-size: 11px;";initstring+="color: #FFFFFF;";initstring+="background: #839A32;";initstring+="}";initstring+="</style>";initstring+="<div id='upvs_quote_area' style='height:140;width:600;overflow:auto'>";initstring+="<table width='100%' border='0' cellspacing='0' cellpadding='0'>";initstring+="<tr>";initstring+="<td class='body' width='180'>Shipping Material Weight : </td>";initstring+="<td class='body'><input type='text' name='weight' id='weight' style='border:1px solid #000000'>&nbsp;lbs</td>";initstring+="</tr>";initstring+="<tr>";initstring+="<td class='body'>&nbsp;</td>";initstring+="<td class='body'>&nbsp;</td>";initstring+="</tr>";initstring+="<tr>";initstring+="<td class='body' width='180'>Enter your zip code : </td>";initstring+="<td><input type='text' name='destination_zip' id='destination_zip' style='border:1px solid #000000'></td>";initstring+="</tr>";initstring+="<tr> ";initstring+="<td height='30' class='body' width='180'><input type='button' name='Submit' value='GET QUOTE' class='btn' onClick='submit_quote_data();'></td>";initstring+="<td class='body'>&nbsp;</td>";initstring+="</tr>";initstring+="</table>";initstring+="</div>";document.write(initstring);}function rightTrim(strValue){var objRegExp = /^([\w\W]*)(\b\s*)$/;if(objRegExp.test(strValue)){strValue = strValue.replace(objRegExp, '$1');}return strValue;}function leftTrim(strValue){var objRegExp = /^(\s*)(\b[\w\W]*)$/;if(objRegExp.test(strValue)){strValue = strValue.replace(objRegExp, '$2');}return strValue;}function trim(strValue){var objRegExp = /^(\s*)$/;if(objRegExp.test(strValue)){strValue = strValue.replace(objRegExp, '');if( strValue.length == 0)return strValue}objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;if(objRegExp.test(strValue)){strValue = strValue.replace(objRegExp, '$2');}return strValue;}function submit_quote_data(){var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;if(trim(document.getElementById('weight').value)=="")alert("Please enter the weight of shipping material");else if(isNaN(trim(document.getElementById('weight').value)))alert("Weight of shipping material should be numeric");else if(trim(document.getElementById('destination_zip').value)=="")alert("Please enter destination ZIP Code");else if(!objRegExp.test(trim(document.getElementById('destination_zip').value)))alert("Please enter a valid ZIP Code");else{fetch_remote_page(trim(document.getElementById('destination_zip').value), trim(document.getElementById('weight').value));		document.getElementById('upvs_quote_area').innerHTML = "<br><br><br><br>Please wait .....";	}}
