//JAVASCRIPT CREATED BY INDUSNET IN ORDER TO CALCULATE SHIPPING IN THE PRODUCT PAGE ITSELF (configurePrd.asp)
var xmlHttp;
function populateState(country)
{
	document.getElementById("zip").value="";
	document.getElementById("city").value="";
		
	if(country=="")
	{
		alert("Please select a valid country");
	}
	else
	{
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
			alert ("Your browser does not support AJAX!");
			return;
		}
	
		var url="populateStates.asp?country="+country;
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=countryChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}
function countryChanged()
{
	if (xmlHttp.readyState==4)
	{
	document.getElementById("populateState").innerHTML=xmlHttp.responseText;
	}
}
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
	// Internet Explorer
	try
	{
	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	}
	return xmlHttp;
}


function showShippingCharges()
{
//var showShipping = document.additem.hidShowShipping.value;
var showShipping = document.getElementById("hidShowShipping").value;
//alert(showShipping);
if(showShipping=="0")
{
	var country ="";
	//country = document.additem.CountryCode[document.additem.CountryCode.selectedIndex].value;
	country = document.getElementById('hidCountryName').value;
	if(country =="")
	{
		alert("Please choose a country.");
	}
	else
	{
		var city = "";
		city = document.getElementById("city").value;
		
		var state = "";
		if(document.getElementById("hidStateTypeid").value=="2")
		{
			if(document.getElementById("StateCode"))
			state = document.getElementById("StateCode").value;
			else
			state = document.getElementById("cmbStateCode").value;
		}
		else
		{
			//alert(document.getElementById("cmbStateCode").options);
				if(document.getElementById("cmbStateCode").selectedIndex)
				{
					
					
					state = document.getElementById("cmbStateCode").options[document.getElementById("cmbStateCode").selectedIndex].value;
					
				}
				else
				{
						state = document.getElementById("cmbStateCode").value;
					
				}
		}

		var zip = "";
		var zip = document.getElementById("zip").value;
		
		var resident="";
		for(var i=0;i<document.additem.residentialShipping.length;i++)
		{
			if(document.additem.residentialShipping[i].checked==true)
			{
				resident = document.additem.residentialShipping[i].value;
				break;
			}
		}
		
		var alt="";
		
		if(city=="")
		{
			alt += ">> city\n";
		}
		if(state=="")
		{
			alt += ">> state\n";								
		}
		if(zip=="")
		{
			alt += ">> postal code\n";
		}
		if(resident=="")
		{
			alt += ">> Resident/Business\n";
		}
		
		if(alt!="")
		{
			alt = "Please fill the following fields\n\n" + alt;
			alert(alt);
			return false;
		}
		else
		{
			document.additem.hidShowShipping.value="1";
			document.getElementById("shipppingCharges").style.display="block";
			document.getElementById("shipppingChargesFrm").style.display="none";
			document.getElementById("btnCalculateShipping").style.display="none";
			//document.getElementById("btnEditShipping").style.display="block";
			showHint();	
		}
	}
}
if(showShipping=="1")
{
	document.additem.hidShowShipping.value="0";
	document.getElementById("shipppingChargesFrm").style.display="block";
	document.getElementById("shipppingCharges").style.display="none";
	document.getElementById("btnCalculateShipping").style.display="block";
	document.getElementById("btnEditShipping").style.display="none";
}
}
var http;
function showHint()
{
	http=GetXmlHttpObject();
	if (http==null)
	{
	alert ("Your browser does not support AJAX!");
	return;
	}
	
	document.getElementById("shipppingCharges").innerHTML="";
	document.getElementById("loaderDiv").style.display="block";
	
	//var url="test.asp";
	var url="instConfiguredPrdTemp.asp";						
	//url=url+"&sid="+Math.random();						
	http.onreadystatechange=stateChangedShip;
	//http.open("GET",url,true);
	http.open("POST",url,true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	var param="";
	param = getquerystring();
	//param = "name=niladri&com=CF"
	//http.send(null);
	//alert(param);
	http.send(param);
}

function stateChangedShip()
{ 
if (http.readyState==4)
{
	if(http.status==200)
	{
		//alert("Ready2");
		//alert(http.responseText);
		if(http.responseText!=null)
		{
			//document.getElementById("shipppingCharges").innerHTML=http.responseText;
			oldDiv = document.getElementById("shipppingCharges");
			newDiv = document.createElement(oldDiv.tagName);										
			newDiv.id = oldDiv.id;
			newDiv.className = oldDiv.className;
			newDiv.innerHTML = http.responseText;										
			oldDiv.parentNode.replaceChild(newDiv, oldDiv);
			document.getElementById("loaderDiv").style.display="none";
			document.getElementById("btnEditShipping").style.display="block";
		}									
	}								
}							
}

function getquerystring() 
{
	var form = document.additem;
	var qstr = "";
	
	function GetElemValue(name, value) {
	qstr += (qstr.length > 0 ? "&" : "")
	+ escape(name).replace(/\+/g, "%2B") + "="
	+ escape(value ? value : "").replace(/\+/g, "%2B");
	//+ escape(value ? value : "").replace(/\n/g, "%0D");
}

var elemArray = form.elements;
for (var i = 0; i < elemArray.length; i++) {
var element = elemArray[i];
var elemType = element.type.toUpperCase();
var elemName = element.name;

if (elemName) {
if (elemType == "TEXT"

|| elemType == "TEXTAREA"
|| elemType == "PASSWORD"
|| elemType == "BUTTON"
|| elemType == "RESET"
|| elemType == "SUBMIT"
|| elemType == "FILE"
|| elemType == "IMAGE"
|| elemType == "HIDDEN")
GetElemValue(elemName, element.value);
else if (elemType == "CHECKBOX" && element.checked)
GetElemValue(elemName,
element.value ? element.value : "On");
else if (elemType == "RADIO" && element.checked)
GetElemValue(elemName, element.value);
else if (elemType.indexOf("SELECT") != -1)
for (var j = 0; j < element.options.length; j++) {
var option = element.options[j];
if (option.selected)
GetElemValue(elemName,
option.value ? option.value : option.text);
}
}
}							
return qstr;							
}

function onChangeShipping()
{
	//document.getElementById("btnEditShipping").style.display="none";	
	if(document.getElementById("hidShowShipping").value=="1")
	{
		showHint();
	//showShipHint();	
	}							
}


function onChangeShippingNew()
{	
	if(document.getElementById("hidShowShipping").value=="1")
	{
			if(document.additem.shipoptioncombo)
			{
			showShipHint();	
			}
	}							
}



var httpProvider;
function onChangeProvider(param)
{	
	httpProvider=GetXmlHttpObject();
	if (httpProvider==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	
	document.getElementById("shipppingCharges").innerHTML="";
	document.getElementById("loaderDiv").style.display="block";
	document.getElementById("btnEditShipping").style.display="none";
	
	param = "&provider=" + param;
	var url="estimateShipCostTemp.asp?flag=2";
	url=url+param+"&sid="+Math.random();							
	httpProvider.onreadystatechange=stateChangedShipProvider;
	httpProvider.open("GET",url,true);
	httpProvider.send(null);														
}

function stateChangedShipProvider()
{ 
	if (httpProvider.readyState==4)
	{
		if(httpProvider.status==200)
		{
			if(httpProvider.responseText!=null)
			{
				oldDiv = document.getElementById("shipppingCharges");
				newDiv = document.createElement(oldDiv.tagName);										
				newDiv.id = oldDiv.id;
				newDiv.className = oldDiv.className;
				newDiv.innerHTML = httpProvider.responseText;																				
				oldDiv.parentNode.replaceChild(newDiv, oldDiv);
				document.getElementById("loaderDiv").style.display="none";
				document.getElementById("btnEditShipping").style.display="block";
			}									
		}								
	}							
}



/* NEW */
function showShippingCharges1()
{
	
//var showShipping = document.additem.hidShowShipping.value;
var showShipping = document.getElementById("hidShowShipping").value;
//alert(showShipping);
if(showShipping=="0")
{
	var country ="";
	//country = document.additem.CountryCode[document.additem.CountryCode.selectedIndex].value;
	country = document.getElementById('hidCountryName').value;
	if(country =="")
	{
		alert("Please choose a country.");
	}
	else
	{
		var city = "";
		city = document.getElementById("city").value;
		
		var state = "";
		if(document.getElementById("hidStateTypeid").value=="2")
		{
			if(document.getElementById("StateCode"))
			state = document.getElementById("StateCode").value;
			else
			state = document.getElementById("cmbStateCode").value;
		}
		else
		{
			//alert(document.getElementById("cmbStateCode").options);
				if(document.getElementById("cmbStateCode").selectedIndex)
				{
					
					
					state = document.getElementById("cmbStateCode").options[document.getElementById("cmbStateCode").selectedIndex].value;
					
				}
				else
				{
						state = document.getElementById("cmbStateCode").value;
					
				}
		}

		var zip = "";
		var zip = document.getElementById("ship").value;
		
		var resident="";
		/*for(var i=0;i<document.additem.residentialShipping.length;i++)
		{
			if(document.additem.residentialShipping[i].checked==true)
			{
				resident = document.additem.residentialShipping[i].value;
				break;
			}
		}
		*/
		resident = -1; // for residence
		
		var alt="";
		
		if(city=="")
		{
			alt += ">> city\n";
		}
		if(state=="")
		{
			alt += ">> state\n";								
		}
		if(zip=="")
		{
			alt += ">> postal code\n";
		}
		if(resident=="")
		{
			alt += ">> Resident/Business\n";
		}
		
		if(alt!="")
		{
			alt = "Please fill the following fields\n\n" + alt;
			alert(alt);
			return false;
		}
		else
		{
			document.additem.hidShowShipping.value="1";
			//document.getElementById("shipppingCharges").style.display="block";
			//document.getElementById("shipppingChargesFrm").style.display="none";
			//document.getElementById("btnCalculateShipping").style.display="none";
			//document.getElementById("btnEditShipping").style.display="block";
			showShipHint();	
		}
	}
}
if(showShipping=="1")
{
	document.additem.hidShowShipping.value="0";
	//document.getElementById("shipppingChargesFrm").style.display="block";
	//document.getElementById("shipppingCharges").style.display="none";
	//document.getElementById("btnCalculateShipping").style.display="block";
	//document.getElementById("btnEditShipping").style.display="none";
	
}
}

var httpsh;
function showShipHint()
{
	
	httpsh=GetXmlHttpObjectS();
	if (httpsh==null)
	{
	alert ("Your browser does not support AJAX!");
	return;
	}
	
	//document.getElementById("shipppingCharges").innerHTML="";
	//document.getElementById("shipDiv").innerHTML='';
	document.getElementById("loaderDiv").style.display="block";
	
	//var url="test.asp";
	var url="instConfiguredPrdTemp.asp?q="+Math.random()+"&optn=ship";						
	//url=url+"&sid="+Math.random();	
	httpsh.onreadystatechange=stateChangedShipNew;
	//http.open("GET",url,true);
	httpsh.open("POST",url,true);
	httpsh.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	var param="";
	param = getquerystring();
	//param = "name=niladri&com=CF"
	//http.send(null);
	//alert(param);
	httpsh.send(param);
}

function stateChangedShipNew()
{ 
if (httpsh.readyState==4)
{
	
	if(httpsh.status==200)
	{
		//alert("Ready2");

		if(httpsh.responseText!=null)
		{
			document.getElementById("shipDiv").innerHTML=httpsh.responseText;
			//oldDiv = document.getElementById("shipppingCharges");
			//newDiv = document.createElement(oldDiv.tagName);										
			//newDiv.id = oldDiv.id;
			//newDiv.className = oldDiv.className;
			//newDiv.innerHTML = http.responseText;										
			//oldDiv.parentNode.replaceChild(newDiv, oldDiv);
			document.getElementById("loaderDiv").style.display="none";
			//document.getElementById("btnEditShipping").style.display="block";
			New_calculateAll();
		}									
	}								
}							
}

function GetXmlHttpObjectS()
{
	var httpsh=null;
	try
	{
	// Firefox, Opera 8.0+, Safari
	httpsh=new XMLHttpRequest();
	}
	catch (e)
	{
	// Internet Explorer
	try
	{
	httpsh=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
	httpsh=new ActiveXObject("Microsoft.XMLHTTP");
	}
	}
	return httpsh;
}

