﻿function SetCaptcha() {
    var control1 = $('img_SpecificCode1');
    var control = $('img_SpecificCode');
    if (control)
        control.onclick = this.RefreshCaptcha.bind(this, control);
    if (control1)
        control1.onclick = this.RefreshCaptcha.bind(this, control1);
}
function RefreshCaptcha() {
    var control = arguments[0]; //
    var control1 = $('img_SpecificCode');
    if (control.id == 'img_SpecificCode')
        control1 = $('img_SpecificCode1');
    new Ajax.Request("../VNVN_Web/VNVN_19_Server.aspx",
    {
        method: "post", // Setup Method            
        parameters:
        {
            cmd: 2030
        }, // Add Parameters
        onSuccess: function(transport) {
            control.src = transport.responseText;
            control1.src = transport.responseText;
        },
        onFailure: function() { alert(sError); return false; } // Event Failure --> do...     

    });

}

function SetCaptcha111(id) {
    if ($(id))
        $(id).onclick = this.RefreshCaptcha.handle(this, $(id));
}
function Addtocart(pid) {

    new Ajax.Request(s_Path + "VNVN_Web/VNVN_19_Server.aspx",
    {
        method: "post", // Setup Method            
        parameters:
        {
            cmd: 1,
            ProductID: pid
        }, // Add Parameters
        onSuccess: function(transport) {
            window.location = window.location;
            
        },
        onFailure: function() { alert("error"); return false; } // Event Failure --> do...     

    });

}
function assamechange(ParentControl, ParentControl2, GroupName, GroupName2, ControlID, event, SameControl, SameControl1, reload, flag) {
    ControlID = "oneStep_" + ControlID;
    if ($(ControlID)) {
        if (SameControl == null || SameControl == "") {
            var elem = document.getElementById(ParentControl);
            var elem2 = document.getElementById(ParentControl2);
            var childs = elem.getElementsByTagName("select");
            var childs2 = elem2.getElementsByTagName("select");
            for (i = 0; i < childs.length; i++) {
                if (childs[i].id.indexOf(GroupName) >= 0) {
                    childs[i].disabled = flag;
                    childs[i].options[childs[i].selectedIndex].text = childs2[i].options[childs2[i].selectedIndex].text;
                    childs[i].options[childs[i].selectedIndex].value = childs2[i].options[childs2[i].selectedIndex].value;
                }
            }
            childs = elem.getElementsByTagName("input");
            childs2 = elem2.getElementsByTagName("input");
            for (i = 0; i < childs.length; i++) {
                if (childs[i].id.indexOf(GroupName) >= 0) {
                    childs[i].disabled = flag;
                    childs[i].value = (flag == true) ? childs2[i].value : "";
                }
            }
            childs = elem.getElementsByTagName("span");
            for (i = 0; i < childs.length; i++) {

                if ((childs[i].controltovalidate) && childs[i].controltovalidate.indexOf(GroupName) >= 0) {
                    if (flag == true)
                        childs[i].style.display = 'none';

                    childs[i].validationGroup = (flag == true) ? "GrpTemp" : "GroupRegis";
                }
            }
            //SelectCountry('Country_grpShipping_ddl_Country_A5_005','gCountry_grpShipping_ddl_State_A5_005','Country_grpShipping_tbx_State_A5_005','Country_pnl_A5_L6_X6_Y1_045','rfv_Asterisk_A5_080','rev_CheckZipcode_A5_010','Country_pnl_A5_L6_X6_Y1_050','rfv_Asterisk_A5_085','grpShipping_tbx_Zipcode_A5_005',event);
            if (flag == true)
                $(ParentControl).style.display = 'none';
            else
                $(ParentControl).style.display = 'block';
        }
        else {
            flag = $(ControlID).checked;
            if ($(ControlID).checked == true) {
                SameControl = "oneStep_" + SameControl;
                SameControl1 = "oneStep_" + SameControl1;
                if ($(SameControl).type == "select-one") {
                    $(SameControl).disabled = flag;
                    $(SameControl).options[$(SameControl).selectedIndex].text = $(SameControl1).options[$(SameControl1).selectedIndex].text;
                    $(SameControl).options[$(SameControl).selectedIndex].value = $(SameControl1).options[$(SameControl1).selectedIndex].value;
                    if (SameControl == 'oneStep_cmb_s_6')
                        SelectCountry('oneStep_cmb_s_6', 'oneStep_cmb_s_7', 'oneStep_txt_s_7', 'oneStep_sp_s_8', 'oneStep_rq_s_8', 'oneStep_rq_s_Code', 'oneStep_sp_s_9', 'oneStep_rq_s_9', 'oneStep_txt_s_8', event,1);
                }
                else {
                    $(SameControl).disabled = flag;
                    $(SameControl).value = (flag == true) ? $(SameControl1).value : "";
                }
            }
        }
        if (reload == 1)
            ReloadShippingMethod('grpBilling_UC_UPS_A5_005_pnl_ShippingCompany', 'oneStep_cmb_s_6', 'oneStep_txt_s_7', 'oneStep_txt_s_8', event);

        return false;
    }
}
function SelectState(StateComboBox, StateTextBox) {
    var state = $(StateComboBox);
    if (state != null) {
        $(StateTextBox).value = state.options[state.selectedIndex].text;
        /*if(StateComboBox=='gCountry_grpBilling_ddl_State_A5_005')
        GetTotalValueInOneStepCheckOut('lbl_Tax_A5_010,lbl_GrandTotal_A5_010','1010,1014','gCountry_grpBilling_ddl_State_A5_005,',',',null);*/
    }
    else {
        state = document.getElementById('Country_grpRegistration_ddl_State_A5_005');
        if (state != null) {
            document.getElementById('Country_grpRegistration_tbx_State_A5_005').value = state.options[state.selectedIndex].text;
        }
    }

}
function parse(xml) {
    var doc = null;
    if (window.ActiveXObject) {
        doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async = "false";
        doc.loadXML(xml);
    }
    else {
        var parser = new DOMParser();
        doc = parser.parseFromString(xml, "text/xml");
    }
    return doc;
}
function SelectCountry(CountryControl, StateControl1, StateControl2, PanelZip, ReqZip, CheckZip, PanelPone, ReqPhone, ZipCode, event, reload) {

    OnPleaseWait("AdminDvWait", event);
    if (reload == null)
        reload = 0;
    var elem = $(CountryControl);
    if (elem != null) {
        new Ajax.Request(s_Path + "VNVN_Web/VNVN_19_Server.aspx",
	    {
	        method: "post", // Setup Method            
	        parameters:
		    {
		        cmd: 2006,
		        asynchronous: true,
		        ProductID: elem.options[elem.selectedIndex].value
		    },
	        onSuccess: function(transport) { // Event Success --> do...		  		       
	            var dom = parse(transport.responseText);

	            var ndProduct = dom.getElementsByTagName('option');
	            elem = document.getElementById(StateControl1);
	            var display1 = 'block';
	            var display2 = true;
	            if (dom.childNodes[0].getAttribute('checkus') != 'True') {
	                display1 = 'none';
	                display2 = false;
	            }

	            if (ndProduct != null) {
	                if (elem.options[0].text != ndProduct[0].getAttribute('text')) {
	                    elem.innerHTML = "";
	                    for (var iNode = 0; iNode < ndProduct.length; iNode++) {
	                        var ndItem = ndProduct[iNode];

	                        var oOption = document.createElement("OPTION");

	                        elem.options.add(oOption);
	                        oOption.innerHTML = ndItem.getAttribute('text');
	                        oOption.value = ndItem.getAttribute('value');
	                    }
	                    if (ZipCode != '') {
	                        $(ZipCode).value = '';
	                        if (display2 == false && reload == 1)
	                            ReloadShippingMethod('grpBilling_UC_UPS_A5_005_pnl_ShippingCompany', 'oneStep_cmb_s_6', 'oneStep_txt_s_7', 'oneStep_txt_s_8', event);
	                        else {
	                            $('grpBilling_UC_UPS_A5_005_pnl_ShippingCompany').innerHTML = '';
	                            $('oneStep_td_ShippingMethod').innerHTML = "$0.00";
	                            GetTotalValueInOneStepCheckOut('oneStep_td_TotalMoney,oneStep_td_Tax,oneStep_td_ShippingMethod,oneStep_td_GrandTotal', 'oneStep_td_ShippingMethod,', ',', event);
	                        }
	                    }
	                    document.getElementById(StateControl2).value = ndProduct[0].getAttribute('text');
	                }
	            }

	           
	            var elems = $(PanelZip);
	            if (elems != null) {
	                elems.style.display = display1;
	                $(ReqZip).enabled = display2;
	                $(CheckZip).enabled = display2;
	            }
	            elems = $(PanelPone);
	            if (elems != null) {
	                elems.style.display = display1;
	                $(ReqPhone).enabled = display2;
	            }
	            OffPleaseWait("AdminDvWait");
	           

	        },
	        onFailure: function() { alert(sError); OffPleaseWait("AdminDvWait"); } // Event Failure --> do...     
	    });

    }
    else
        OffPleaseWait("AdminDvWait");
}
function ReloadShippingMethod(Container, Country, State, ZipCode, event) {
    //Thay cho nay = tim kiem
    var product = document.getElementById(ZipCode);
    var s_Country = document.getElementById(Country);
    if (product != null) {
        if (product.value == '' && Country == "")
            document.getElementById('rfv_Asterisk_A4_002').style.display = "inline";
        else {
            OnPleaseWait("AdminDvWait", event);
            new Ajax.Request(s_Path + "VNVN_Web/VNVN_19_Server.aspx",
		    {
		        method: "post", // Setup Method            
		        parameters:
			    {
			        cmd: 2016,
			        ProductID: s_Country.options[s_Country.selectedIndex].text,
			        sData: product.value
			    },
		        onSuccess: function(transport) { // Event Success --> do...
		            var response = transport.responseText;
		            if (response != "") {
		                $(Container).innerHTML = response;
		            }
		            DisplayShippingPrice('grpBilling_UC_UPS_A5_005_pnl_ShippingCompany', 'oneStep_td_ShippingMethod');
		            GetTotalValueInOneStepCheckOut('oneStep_td_TotalMoney,oneStep_td_Tax,oneStep_td_ShippingMethod,oneStep_td_GrandTotal', 'oneStep_td_ShippingMethod,', ',', event);

		            OffPleaseWait("AdminDvWait");
		        },
		        onFailure: function() { alert(sError); OffPleaseWait("AdminDvWait"); } // Event Failure --> do...     		    
		    });
        }
    }
    return false;
}
function GetTotalValueInOneStepCheckOut(ControlArray, InputArray, Split, event) {
    OnPleaseWait("AdminDvWait", event);
    var ValueArray = 0;
    if (InputArray != '') {

        var Values = ControlArray.split(Split);
        for (var i = 0; i < Values.length - 1; i++) {
            if ($(Values[i]) != null) {

                var temp = parseFloat($(Values[i]).innerHTML.replace('$', '').replace(',', '')) * 1.0;
                ValueArray = ValueArray + temp;
            }
        }

        $(Values[i]).innerHTML = "$" + Math.round(ValueArray * 100) /100;
    }
    OffPleaseWait("AdminDvWait");
}
function DisplayShippingPrice(ControlParent, Control) {
    //OnPleaseWait("AdminDvWait",event);    
    var elem = document.getElementById(ControlParent);
    var childs = elem.getElementsByTagName("input");
    value = "";
    for (var i = 0; i < childs.length; i++) {
        if (childs[i].type == "radio")
            if (childs[i].checked == true) {
            value = childs[i].value;
            break;
        }
    }
    $(Control).innerHTML = "$" + value.split(';')[value.split(';').length - 1];
    if ($(Control).innerHTML == "$")
        $(Control).innerHTML = "$0";
    new Ajax.Request(s_Path + "VNVN_Web/VNVN_19_Server.aspx",
    {
        method: "post", // Setup Method            
        parameters:
	    {
	        cmd: 2028,
	        ProductID: value
	    },
        onSuccess: function(transport) { // Event Success --> do...	           
            var response = transport.responseText;
        },
        onFailure: function() { alert(sError); } // Event Failure --> do...     		    
    });
    return false;
}
function GetTax(ControlValue, Container, Control, event) {
    OnPleaseWait("AdminDvWait", event);
    new Ajax.Request(s_Path + "VNVN_Web/VNVN_19_Server.aspx",
    {
        method: "post", // Setup Method            
        parameters:
	    {
	        cmd: 2029,
	        ProductID: $(ControlValue).value
	    },
        onSuccess: function(transport) { // Event Success --> do...
            var response = transport.responseText;
            if (response != "") {
                var value = $(Control).innerHTML.replace('$', '');
                $(Container).innerHTML = "$" + Math.round(response * value) / 100;
            }
            OffPleaseWait("AdminDvWait");
        },
        onFailure: function() { alert(sError); OffPleaseWait("AdminDvWait"); } // Event Failure --> do...     		    
    });
    return false;
}
function Counter1(field, countfield, maxlimit) {
    if (field.value.length > maxlimit) {
        field.value = field.value.substring(0, maxlimit);

    } //name em dat khac id nen ben FF noi hiu,IE ko hiu
    else {
        countfield.value = maxlimit - field.value.length;
    }
} 