﻿var xmlHttp;
var xmlhttp;
var curStateObj;
var prvStateObj;
var isSavingOrder = false;
var sCurrentCode;

//=========================================
//Australian Post
//=========================================
    var width = 100; //mm
    var height = 100; //mm
    var length = 100; //mm
    var weight = 500; //g
    var Pickup_Postcode = 3037;
    var isComputed = false;
    var AJAX_URL = "";
    
    function OnChangeInfo(){
        isSavingOrder = false;
        isComputed = false;
    }       
    
    function testFunction() {     
    }
        
    function sendFormShipping() {                              
	    var obj;
	    var url = "";
	    var params = "";
	        
	    url = "default4.aspx";	      

	    params = "Height=" + height;
	    params = params + "&Length=" + length;
	    params = params + "&Width=" + width;
	    params = params + "&Weight=" + weight;	    

	    obj = document.getElementById('quantity');
	    params = params + "&Quantity=" + obj.value;

	    params = params + "&Pickup_Postcode=" + Pickup_Postcode;

	    obj = document.getElementById("txtSZipCode");
	    params = params + "&Destination_Postcode=" + obj.value;

	    params = params + "&Country=" + sCurrentCode;

	    if(sCurrentCode=="AU")
	        params = params + "&Service_Type=Express";
	    else
	        params = params + "&Service_Type=Air";	        

        xmlhttp=GetXmlHttpObject();
        if (xmlhttp==null){
            alert ("Your browser does not support AJAX!");
            return;
        }                 
           
	   // alert("Computing");
        xmlhttp.onreadystatechange=australianPost;
        xmlhttp.open("POST", url, true);

        //Send the proper header information along with the request
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", params.length);
        xmlhttp.setRequestHeader("Connection", "close");
        xmlhttp.send(params); 	
    }
    
    function bridgeResult(eventName, args, cb) {
        return window.parent.ajaxResult(eventName, args, cb);
    }
    
    function a(){    
        var iframe = document.getElementById("australianpost");
        alert(iframe.contentWindow.document.body.innerHTML );
    }   
    
    function australianPost() {             
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		    var obj = document.getElementById('Message');

	   // alert("test");
		    var str = xmlhttp.responseText;		
		    		   		    
		    var pos1 = str.indexOf("charge",1) + 8;
		    var pos2 = str.indexOf("days",pos1);
		    var charge = str.substring(pos1,pos2);

		    var pos1 = str.indexOf("days",1) + 5;
		    var pos2 = str.indexOf("err_msg",pos1);
		    var days = str.substring(pos1,pos2);

		    var pos1 = str.indexOf("err_msg",1) + 8;
		    var pos2 = str.indexOf("\n",pos1);
		    var err_msg = str.substring(pos1,pos2);

		    /* display results */ 
		    document.getElementById("shipping").value = Math.ceil(charge);
		    
		    if(sCurrentCode=="AU")
		        obj.innerHTML = "<b>Charge</b> : AUD $" + Math.ceil(charge) + ".00<br><b>Days</b> : Express Parcels (averages "+ days +" business days)<br>";
		    else
		        obj.innerHTML = "<b>Charge</b> : AUD $" + Math.ceil(charge) + ".00<br><b>Days</b> : Expedited International Shipping (averages "+ days +" business days)<br>";		        		            
	    }
	    //alert("Priced Computed");
	    isComputed = true;	    	          
    }    
//=========================================    

function CheckUserNameAndPassord(username, password){
	if(document.getElementById(documentParentName+ "txtUsername").value.length <= 0){
	    alert("Please enter your username.");	   
	    document.getElementById(documentParentName+ "txtUsername").focus(); 
	    return;
	}
	
	if(document.getElementById(documentParentName+ "txtPassword").value.length <= 0){
	    alert("Please enter your password.");	   
	    document.getElementById(documentParentName+ "txtPassword").focus(); 
	    return;
	}	
	
	if(document.getElementById(documentParentName+ "txtPassword").value!=document.getElementById(documentParentName+ "txtConfirmPassword").value){
	    alert("Password is not match.");	   
	    document.getElementById(documentParentName+ "txtConfirmPassword").value = "";
	    document.getElementById(documentParentName+ "txtPassword").focus(); 
	    return;
	}	

    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null){
        alert ("Your browser does not support AJAX!");
        return;
    }  
    
    var url = "ccheckuser.aspx";
    var params = "username="+ username +"&password="+ password;
    
    xmlHttp.onreadystatechange=checkUserChanged;
    xmlHttp.open("POST", url, true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(params);                        
}

function checkUserChanged() {             
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {          
        if(xmlHttp.responseText=="Available")       
            alert("OK!");
        else
            alert("Username and password had been used by other users!");
    }            
}

function GetCity(str, object){
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null){
        alert ("Your browser does not support AJAX!");
        return;
    }  
    
    //alert(document.getElementById(documentParentName+ "selSCountry").selectedIndex);
    try{
        var sCode = document.getElementById(documentParentName+ "sCode").value;
        
        var listOfSCode = sCode.split(",");
        
        sCurrentCode = listOfSCode[document.getElementById(documentParentName+ "selSCountry").selectedIndex];
    }catch(err_1){}
    
    try{
        if(document.getElementById(documentParentName+ "selSCountry").value=="AUS"){
            document.getElementById(documentParentName+ "selSTerritory").disabled = false;
        }else{
            document.getElementById(documentParentName+ "selSTerritory").selectedIndex = 0;
            document.getElementById(documentParentName+ "selSTerritory").disabled = true;        
        }     
    
        if(document.getElementById(documentParentName+ "selCountry").value=="AUS"){
            document.getElementById(documentParentName+ "selTerritory").disabled = false;
        }else{
            document.getElementById(documentParentName+ "selTerritory").selectedIndex = 0;
            document.getElementById(documentParentName+ "selTerritory").disabled = true;        
        }
        
        if(document.getElementById(documentParentName+ "selCCountry").value=="AUS"){
            document.getElementById(documentParentName+ "selCTerritory").disabled = false;
        }else{
            document.getElementById(documentParentName+ "selCTerritory").selectedIndex = 0;
            document.getElementById(documentParentName+ "selCTerritory").disabled = true;        
        }    
        
  
    }catch(err){}    

     
    curStateObj = documentParentName + object;

    var url = "ccity.aspx";
    var params = "countrycode="+ str;

    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("POST", url, true);
     
    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(params);                          
} 

function stateChanged() {             
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {          
        var xmlDoc=xmlHttp.responseXML.documentElement;
        
        RemoveAllItemOnSelectObject(curStateObj);
        for(i=0;i < xmlDoc.getElementsByTagName("cityname").length; i++){
            var item = xmlDoc.getElementsByTagName("cityname")[i].childNodes[0].nodeValue;
            AddItemOnSelectObject(curStateObj, item, item);
        }
    }            
}

function SaveOrderAndShipping(str){    
    xmlHttp=GetXmlHttpObject();
    
    //alert(str);
    
      
        document.getElementById("btnBuyNow").style.cursor = "wait";    
            
        if (xmlHttp==null){
            alert ("Your browser does not support AJAX!");
            return;
        }  
        
        var url = "saveorderandshipping.aspx";
        var params = "userID="+ document.getElementById("custom").value;      
        
        try{
            params += "&selReferral="+ document.getElementById(documentParentName+ "selReferral").value;
        }catch(error){}   
        
        //alert(1); 
        
        params += "&quantity="+ document.getElementById(documentParentName+ "quantity").value;
        params += "&devicetype="+ document.getElementById(documentParentName+ "devicetype").value;
        params += "&devicetypename="+ document.getElementById(documentParentName+ "item_name").value;
        params += "&shipping="+ document.getElementById(documentParentName+ "shipping").value;
        params += "&amount="+ document.getElementById(documentParentName+ "amount").value;
        
        //params += "&pincode="+ document.getElementById(documentParentName+ "pincode").value;
        //params += "&simlock="+ document.getElementById(documentParentName+ "simlock").value;
        //params += "&Carrier="+ document.getElementById(documentParentName+ "Carrier").value;
        //params += "&txtSMS="+ document.getElementById(documentParentName+ "txtSMS").value;
        
        params += "&pincode=";
        params += "&simlock=";
        params += "&Carrier=";
        params += "&txtSMS=";    
       
        params += "&txtSStreet="+ document.getElementById(documentParentName+ "txtSStreet").value;
        params += "&txtSSuburb="+ document.getElementById(documentParentName+ "txtSSuburb").value;
        params += "&selSTerritory="+ document.getElementById(documentParentName+ "selSTerritory").value;
        params += "&selSRegion="+ document.getElementById(documentParentName+ "selSRegion").value;
        params += "&selSCountry="+ document.getElementById(documentParentName+ "selSCountry").value;
        params += "&txtSZipCode="+ document.getElementById(documentParentName+ "txtSZipCode").value;
        //params += "&txtSTelNo_Areacode="+ document.getElementById(documentParentName+ "txtSTelNo_Areacode").value;
        params += "&txtSTelNo="+ document.getElementById(documentParentName+ "txtSTelNo").value;
        //params += "&txtSMobileNo_Areacode="+ document.getElementById(documentParentName+ "txtSMobileNo_Areacode").value;
        params += "&txtSMobileNo="+ document.getElementById(documentParentName+ "txtSMobileNo").value;
        params += "&txtSEmail="+ document.getElementById(documentParentName+ "txtSEmail").value;    
        
        xmlHttp.onreadystatechange=ordershippingChanged;
        xmlHttp.open("POST", url, true);
        
        //document.getElementById("tc").innerHTML = params;

        //Send the proper header information along with the request
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-length", params.length);
        xmlHttp.setRequestHeader("Connection", "close");
        xmlHttp.send(params);    
    
    
    //alert(params);    
}

function savedata(){
    alert("Saving Data!!!!!");
    
    document.forms[0].submit();
    return false;
}

function ordershippingChanged() {             
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {  
        isSavingOrder = false;
        document.getElementById("btnBuyNow").style.cursor = "pointer";
        
        if(xmlHttp.responseText=="ERROR"){                   
            alert("Please try again later!");     
        }else{
            //alert(xmlHttp.responseText);
            document.getElementById("return").value = xmlHttp.responseText;
            document.forms[0].submit();        
        }
            
    }            
}

//================Begin Map Center====

function MapCenterDB(userid, lon, lat, unitid){    
    xmlHttp=GetXmlHttpObject();
              
    if (xmlHttp==null){
        alert ("Your browser does not support AJAX!");
        return;
    }  
    
    var url = "saveMapSetting.aspx";
    var params = "userID="+ userid;
    params += "&lon="+ lon;                     
    params += "&lat="+ lat;
    params += "&unitid="+ unitid;
    
    
    xmlHttp.onreadystatechange=mapcenterChanged;
    xmlHttp.open("POST", url, true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(params);      
}


function mapcenterChanged() {             
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {       
    }            
}


//================End Map Center=====

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");
        }
      }
      
//    var xmlHttp=false;
//    if(!xmlHttp)try{ xmlHttp=new XMLHttpRequest(); }catch(e){xmlHttp=false;}
//    if(typeof ActiveXObject != "undefined"){
//    if(!xmlHttp)try{ xmlHttp=new ActiveXObject("MSXML2.XMLHTTP"); }catch(e){xmlHttp=false;}
//    if(!xmlHttp)try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){xmlHttp=false;}
//    }
//    if(!xmlHttp)try{ xmlHttp=createRequest(); }catch(e){xmlHttp=false;}      
//      
    return xmlHttp;
}

function GetXMLFile(){
    var xmlDoc=xmlHttp.responseXML.documentElement;
    
    document.getElementById(documentParentName+ "companyname").innerHTML=xmlDoc.getElementsByTagName(documentParentName+ "compname")[0].childNodes[0].nodeValue;
    document.getElementById(documentParentName+ "contactname").innerHTML=xmlDoc.getElementsByTagName(documentParentName+ "contname")[0].childNodes[0].nodeValue;
    document.getElementById(documentParentName+ "address").innerHTML=xmlDoc.getElementsByTagName(documentParentName+ "address")[0].childNodes[0].nodeValue;
    document.getElementById(documentParentName+ "city").innerHTML=xmlDoc.getElementsByTagName(documentParentName+ "city")[0].childNodes[0].nodeValue;
    document.getElementById(documentParentName+ "country").innerHTML=xmlDoc.getElementsByTagName(documentParentName+ "country")[0].childNodes[0].nodeValue;        
}

function AddItemOnSelectObject(obj, Text,Value){
    var opt = document.createElement("option");
    // Add an Option object to Drop Down/List Box            
    document.getElementById(obj).options.add(opt);

    // Assign text and value to Option object
    opt.text = Text;
    opt.value = Value;
}

function RemoveAllItemOnSelectObject(obj){
    while (document.getElementById(obj).options.length) 
        document.getElementById(obj).options[0] = null;
    
    AddItemOnSelectObject(obj, "[City]", "");
}

function divide_string()
{
    var where_is_mytool="home/mytool/mytool.cgi";
    var mytool_array=where_is_mytool.split("/");
    alert(mytool_array.length);
    alert(mytool_array[0]+" "+mytool_array[1]+" "+mytool_array[2]);
}        

function PopupWindow_getXYPosition(anchorname) {
    var coordinates;

    coordinates = getAnchorPosition(anchorname);
    
    this.x = coordinates.x;
    this.y = coordinates.y;
    
    document.getElementById("boxToDrag").style.left = this.x;
    document.getElementById("boxToDrag").style.top = this.y;
    document.getElementById("boxToDrag").style.visibility = "visible";
}    

function PopupWindow_getXYPosition1(anchorname, obj1, obj2) {
    var coordinates;

    coordinates = getAnchorPosition(anchorname);
    
    this.x = coordinates.x;
    this.y = coordinates.y;
    
    document.getElementById(obj1).style.left = this.x;
    document.getElementById(obj1).style.top = this.y;
    document.getElementById(obj1).style.visibility = "visible";
    
    document.getElementById(obj2).style.visibility = "hidden";
}     

function PopupWindow_getXYPosition2(anchorname, obj1, obj2, obj3) {
    var coordinates;

    coordinates = getAnchorPosition(anchorname);
    
//    this.x = coordinates.x;
//    this.y = coordinates.y;
    document.getElementById(obj1).style.visibility = "visible";
    if (browser.isNS) {
        document.addEventListener("mousemove", dragGo, true);
        document.addEventListener("mouseup", dragStop, true);
    }
//    alert("here");
    dragObj.elNode = document.getElementById(obj1);
    dragObj.elNode.style.left = coordinates.x + "px";;
    dragObj.elNode.style.top = coordinates.y + "px";;
//    alert("here 1");
    if (browser.isNS) {
        document.removeEventListener("mousemove", dragGo, true);
        document.removeEventListener("mouseup", dragStop, true);
    }
    //document.getElementById(obj1).style.left = this.x;
    //document.getElementById(obj1).style.top = this.y;
    
    
    document.getElementById(obj2).style.visibility = "hidden";
    document.getElementById(obj3).style.visibility = "hidden";
}     


