// *** Tony Jiang May 26,2009 ***
//*****************************

var http_request = false;
var baseurl = "/location/shoppingcart?";
var bt;
var calflag=false;


if(window.XMLHttpRequest)
	{
		http_request=new XMLHttpRequest();
	}
	else if(window.ActiveXObject){
		http_request=new ActiveXObject("Microsoft.XMLHTTP");
	}

function ebiByName(a){
		return document.getElementsByName(a);
	}
	
function ebi(a){
		return document.getElementById(a);
	}

function sameaddress() {
//alert("in");
if(ebiByName('addchange')[0].checked) {
//alert("cardfirstname=="+ebiByName("cardfirstName")[0].value);
ebiByName("shipfirstName")[0].value=ebiByName("cardfirstName")[0].value;
ebiByName("shiplastName")[0].value=ebiByName("cardlastName")[0].value;
ebiByName("shipcompanyName")[0].value=ebiByName("companyName")[0].value;
ebiByName("shipstreetAddress")[0].value=ebiByName("streetAddress")[0].value;
ebiByName("shipcity")[0].value=ebiByName("city")[0].value;

var countryflag=ebi("countryid").value;
//alert("in sameaddress   countryflag=="+countryflag);
if (countryflag=="3") {
ebiByName("shipprovince")[0].value=ebiByName("province")[0].value;
}
//ebiByName("shippostalCode")[0].value=ebiByName("postalCode")[0].value;
//ebiByName("shipcountry")[0].value=ebiByName("country")[0].value;
ebiByName("shipphone")[0].value=ebiByName("phone")[0].value;



}
else {
//alert("no check");
}
}

function AddToMycart(productid) {
var page;
var countryflag=ebi("countryid").value;
//alert("productid="+productid+"  countryflag=="+countryflag);
if (countryflag=="1") {
if (productid=="1") page="s911_voiceoption";
if (productid=="2") page="s911_voiceoption";
if (productid=="29") page="s911_voiceoption";
if (productid=="3") page="s911_voiceoption";
if (productid=="63") page="s911_upgradefreq_ca";
if (productid=="64") page="acc_s911_ca";
if (productid=="68") page="sf_upgradefreq_ca";
if (productid=="69") page="starfinder_keyfob_cad";
if (productid=="66") page="show";
if (productid=="67") page="sf_upgradefreq_ca";
if (productid=="60") page="show";
if (productid=="61") page="show";
if (productid=="62") page="show";
if (productid=="70") page="mc_upgradefreq_ca";
if (productid=="71") page="mc_upgradefreq_ca";
if (productid=="72") page="show";
}

if (countryflag=="2") {
if (productid=="1") page="s911_upgradefreq_usa";//
if (productid=="2") page="s911_upgradefreq_usa";//
if (productid=="29") page="s911_upgradefreq_usa";//
//if (productid=="3") page="s911_voiceoption";
if (productid=="63") page="s911_upgradefreq_ca";
if (productid=="64") page="acc_s911_usa_global";//
if (productid=="68") page="sf_upgradefreq_usa";//
if (productid=="69") page="starfinder_keyfob_usa";//
if (productid=="66") page="show";//
if (productid=="67") page="sf_upgradefreq_usa";//
if (productid=="60") page="show";//
if (productid=="61") page="show";//
if (productid=="62") page="show";//
if (productid=="70") page="mc_upgradefreq_us";//
if (productid=="71") page="mc_upgradefreq_us";//
if (productid=="72") page="show";//
}
if (countryflag=="3") {
if (productid=="1") page="acc_s911_global";//
if (productid=="2") page="acc_s911_global";//
if (productid=="29") page="acc_s911_global";//
//if (productid=="3") page="s911_voiceoption";
//if (productid=="63") page="s911_upgradefreq_ca";
//if (productid=="64") page="acc_s911_usa_global";
if (productid=="68") page="show";//
if (productid=="69") page="starfinder_keyfob_global";//
//if (productid=="66") page="show";
if (productid=="67") page="show";//
if (productid=="60") page="show";//
if (productid=="61") page="show";//
if (productid=="62") page="show";//
if (productid=="70") page="show";//
if (productid=="71") page="show";//
//if (productid=="72") page="show";
}

calflag=false;
//alert("url=="+"/location/Show.do?....");
window.location.href="/location/Show.do?activity=addProduct&productId="+productid+"&page="+page;
//window.location.href="http://www.locationnow.com/location/Show.do?activity=addProduct&productId="+productid+"&page="+page;
/*
if (ebi('usermail').value=="") {
alert("Please login first!");
window.location.href="http://www.locationnow.com/location/PageManage.do?activity=customer_login";
 } else {
alert("user already login usermail=="+ebi('usermail').value); 
window.location.href="http://www.locationnow.com/location/Show.do?activity=addProduct&productId="+productid+"&page="+page;
 }
 */
}

function DelFromMycart(productid) {

/*
if (ebi('usermail').value=="") {
alert("user not login");
 } else {
alert("user already login usermail=="+ebi('usermail').value); 
window.location.href="http://www.locationnow.com/location/Show.do?activity=addProduct&productId="+productid;
 }
*/
//alert("productid=="+productid+" country=="+ebi('countryid').value); 
var promid="";
var province="";
var country="";
var countryflag= ebi('countryid').value;
if (countryflag=="1") {
 country="Canada"
 promid=ebi('promcode').value;
 province=ebi('provincename').value;
}
if (countryflag=="2") {
 country="USA"
 promid=ebi('promcode').value;
 province=ebi('provincename').value;
}
if (countryflag=="3") {
 country=ebi('countryname').value;
 promid=ebi('promcode').value;
 
}

window.location.href="/location/Show.do?activity=delProduct&productId="+productid+"&country="+country+"&province="+province+"&promid="+promid;
//window.location.href="http://www.locationnow.com/location/Show.do?activity=delProduct&productId="+productid+"&country="+country+"&province="+province+"&promid="+promid;

}
function ShowMycart() {
//alert("show mycart!"); 
window.location.href="/location/Show.do?activity=showMycart";
//window.location.href="http://www.locationnow.com/location/Show.do?activity=showMycart";
/*
if (ebi('usermail').value=="") {
alert("Please login first!");
window.location.href="http://www.locationnow.com/location/PageManage.do?activity=customer_login";
 } else {
window.location.href="http://www.locationnow.com/location/Show.do?activity=showMycart";
}
*/
}
function ShowMycart2() {
//alert("show mycart22222222!"); 
window.location.href="http://www.locationnow.com/location/Show.do?activity=showMycart";
}

function AddToMycartAcc() {

var productid=ebi('pdid').value;
if (productid=="10") {
if (ebi('colornb').checked==true) productid="11";
if (ebi('colorwh').checked==true) productid="12";
if (ebi('colorpk').checked==true) productid="13";
if (ebi('colorog').checked==true) productid="14";
}

//alert("id="+productid);
window.location.href="/location/Show.do?activity=addProduct&productId="+productid+"&page=show";
//window.location.href="http://www.locationnow.com/location/Show.do?activity=addProduct&productId="+productid+"&page=show";
/*
if (ebi('usermail').value=="") {
alert("Please login first!");
window.location.href="http://www.locationnow.com/location/PageManage.do?activity=customer_login";
 } else {
alert("user already login usermail=="+ebi('usermail').value); 
window.location.href="http://www.locationnow.com/location/Show.do?activity=addProduct&productId="+productid+"&page=show";
 }
*/
 }
 
function AddToMycartAcc2(productid) {

 bt=productid;
/*
if (ebi('usermail').value=="") {
alert("Please login first!");
window.location.href="http://www.locationnow.com/location/PageManage.do?activity=customer_login";
 } else {
 */
if (productid=="10") {
if (ebi('colornb').checked==true) productid="11";
if (ebi('colorwh').checked==true) productid="12";
if (ebi('colorpk').checked==true) productid="13";
if (ebi('colorog').checked==true) productid="14";
}

if( !http_request) 
			return;
var url = baseurl + "command=add&productId=" + productid; 
		//alert("Url=="+url);
		http_request.open("GET", url, true);
		http_request.onreadystatechange = handleAddtocartResponse;
		http_request.send(null);

 
}

function handleAddtocartResponse() {
if (http_request.readyState == 4 && http_request.status == 200) {
var txt = trim(http_request.responseText);
//alert("txt===="+txt+"  leng=="+txt.length);
if (txt=="") {   //handle session time out
//alert("txt session is null!");
window.location.href="http://www.locationnow.com/location/index1.jsp";
return;
}
var loc=txt.indexOf(",");	
var items=txt.substring(0,loc);
var tbprice=txt.substr(loc+1);
//alert("items=="+items+"  tbprice=="+tbprice);
ebi("itemid").innerHTML=items;
ebi("tbspriceid").innerHTML=tbprice;
	showdiv(bt);
		
}
}

function trim(str)
	{
	   return str.replace(/^\s*|\s*$/g,"");
	}

function closeWin() {
 ebi('addwin').style.display = "none";
 }
 
function showdiv(productid) {
var name="shwin"+productid;
var left=ebi(name).offsetLeft; 
var top=ebi(name).offsetTop;
//alert("nameid=="+name+"  left=="+left+"  top=="+top);

var pt=ebi(name).offsetParent;
while (pt!=null)
{ left+=pt.offsetLeft;
  top+=	pt.offsetTop;
  pt=pt.offsetParent;
}


ebi('addwin').style.display="";
ebi('addwin').style.position="absolute";
ebi('addwin').style.left=left;
ebi('addwin').style.top=top+28;
 }

function NumOnblur(productid) {
var inputid="t"+productid;
var num=ebi(inputid).value;
//alert("onblur prodid=="+productid+" num=="+num);

window.location.href="/location/Show.do?activity=udateProduct&productId="+productid+"&num="+num;
//window.location.href="http://www.locationnow.com/location/Show.do?activity=udateProduct&productId="+productid+"&num="+num;

}

function CalTotal() {


var promcode=ebi('promcode').value;
var countryflag= ebi('countryid').value;
//alert("countryflag=="+countryflag);
var country="";
var province="";
//var zone="";
var code="";
var postcode="";
var select;
postcode=ebi('postalcode').value;
if (countryflag=="1") {
country="Canada";
select = ebi('provincename'); 
code=select.options[select.selectedIndex].value;
province=select.options[select.selectedIndex].innerHTML;
if (code=="0") {
alert("Please choose a  province!");
return;
}
}
else if (countryflag=="2") {
country="USA";
select = ebi('provincename'); 
code=select.options[select.selectedIndex].value;
province=select.options[select.selectedIndex].innerHTML;
if (code=="0") {
alert("Please choose a  state!");
return;
}
} else {
select = ebi('countryname'); 
code= select.options[select.selectedIndex].value;
country=select.options[select.selectedIndex].innerHTML;
if (code=="0") {
alert("Please choose a country!");
return;
}
//alert("flag==3"+" country=="+country);
}
//alert("flag=="+countryflag+" country=="+country+"  provincename=="+province+"  code=="+code+" postalcode="+postcode);
var totalbfprice=ebi('totalbfprice').value;
//alert("procode=="+promcode+"  country=="+country+" province=="+province+" totalbfprice="+totalbfprice);
if (postcode=="") {
alert("Please input your postal code!");
return;
}

if( !http_request) 
			return;
ebi('processid').style.display="";			
var url = baseurl + "command=calculate&promcode=" + promcode+"&country=" + country+"&province=" + province+"&code="+code+"&postalcode="+postcode+"&totalbfprice="+totalbfprice; 
		//alert("Url=="+url);
		http_request.open("GET", url, true);
		http_request.onreadystatechange = handleCalculateResponse;
		http_request.send(null);
}

function handleCalculateResponse() {
if (http_request.readyState == 4 && http_request.status == 200) {
var xmlString = http_request.responseXML;
var shipfee=xmlString.documentElement.getElementsByTagName( "ShipFee" ).item(0).firstChild.data;
//alert("shipfee=="+shipfee);
if (shipfee=="00") {	//handle session time out
window.location.href="http://www.locationnow.com/location/index1.jsp";
return;
}
ebi('processid').style.display="none";
if (shipfee==".00") {
alert("Destination postal code missing or invalid,Please try input it again or call our sales!");
return;
}
var totalfee=xmlString.documentElement.getElementsByTagName( "TotalFee" ).item(0).firstChild.data;


//alert("shipfee="+shipfee+"  totalfee="+totalfee+" gst="+gst+" pst="+pst);	
ebi('shipfeeid').innerHTML="$"+shipfee;

ebi('totalfeeid').innerHTML="$"+totalfee;

var countryflag= ebi('countryid').value;
if (countryflag=="1") {
var gst=xmlString.documentElement.getElementsByTagName( "Gst" ).item(0).firstChild.data;
ebi('gstvalue').innerHTML="$"+gst;
var select = ebi('provincename'); 
province=select.options[select.selectedIndex].value;
/*
if (province=="ON") {
var pst=xmlString.documentElement.getElementsByTagName( "Pst" ).item(0).firstChild.data;
ebi('pstvalue').innerHTML="$"+pst;
}
*/
}
var promcode=xmlString.documentElement.getElementsByTagName( "Prom" ).item(0).firstChild.data;
var discount=xmlString.documentElement.getElementsByTagName( "Discount" ).item(0).firstChild.data;
var discpercent=xmlString.documentElement.getElementsByTagName( "DiscPercent" ).item(0).firstChild.data;
//alert("get prom=="+promcode);
ebi('promid').innerHTML="$"+discount+" ("+discpercent+"% off)";//promcode; //ebi('promcode').value;

	
calflag=true;	
}
}

function setChagProv() {
var select=ebi('provincename');
var province=select.options[select.selectedIndex].value;
calflag=false;
ebi('pst').innerHTML="";
ebi('pstvalue').innerHTML="";
/*if (province=="ON") {
//alert(" select on!!");
ebi('pst').innerHTML="PST: ";
ebi('pstvalue').innerHTML="N/A";
} else {
ebi('pst').innerHTML="";
ebi('pstvalue').innerHTML="";
}
*/

}

function postcodeChange() {
//alert("postal code change");
calflag=false;
}

function promcodeChange(){
//alert("promotion code change");
calflag=false;
}

function countryChange() {
//alert("country change");
calflag=false;
}

function CheckOut1() {
if (calflag) {
if (ebi('agreeid').checked==true) {
window.location.href="/location/PageManage.do?activity=checkout1";
//window.location.href="/location/PageManage.do?activity=redirectshop";
} else {
alert("Please select agreement checkbox first!");
}
} else {
alert("Please Click Calculate Total Amount Button first!");
}
}

function ContShopping() {

window.location.href="/location/PageManage.do?activity=continueshop";
}

function SubmitOrder() {
window.location.href="/location/PageManage.do?activity=submit";
}

function Logout() {
window.location.href="/location/PageManage.do?activity=logout";
}

function LogoutAcc() {
window.location.href="/location/PageManage.do?activity=logoutacc";
}

function ShowDetail(transactionid) {
//alert("transid=="+transactionid);
window.open('/location//ReportSearch.do?activity=getdetail&transactionid='+transactionid, '', 'fullscreen=1, scrollbars=auto');//'fullscreen=1    or width=500,height=600

}

function ReLogin() {
window.location.href="/location/PageManage.do?activity=relogin";
}

function ShowDetailerr(transactionid) {
//alert("transid=="+transactionid);
window.open('/location//ErrReportSearch.do?activity=getdetail&transactionid='+transactionid, '', 'fullscreen=1, scrollbars=auto');//'fullscreen=1    or width=500,height=600

}
