var httpcheck_shipping_carrier_ajax = getNewHTTPObject();
var httpcheck_coupon_ajax = getNewHTTPObject();
var httpcheck_redemption_ajax = getNewHTTPObject();
var httpcheck_product_ajax = getNewHTTPObject();
var httpcheck_shipping_method_ajax = getNewHTTPObject();
var httpcheck_product_delivery_exception_ajax = getNewHTTPObject();

function getNewHTTPObject() {
    if (typeof XMLHttpRequest != 'undefined') {
        return new XMLHttpRequest();
    }
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
    }
    return false;
}

function check_shipping_carrier_ajax(company_id, status, method_id, address_id, weight, quantity, subtotal, usage)
{
	httpcheck_shipping_carrier_ajax.open("GET","/apps/powerstore/commerce/cart/ajax_server.html?sci="+company_id+"&call=ping&status="+status+"&method_id="+escape(method_id)+"&address_id="+address_id+"&weight="+weight+"&quantity="+quantity+"&subtotal="+subtotal+"&usage="+usage+"&deliverydate="+document.getElementById("del_date"+address_id).value,true);
   httpcheck_shipping_carrier_ajax.onreadystatechange = handleHttpcheck_shipping_carrier_ajax;
   httpcheck_shipping_carrier_ajax.send(null);
}

function check_coupon_ajax(formname, company_id, coupon_code)
{
   httpcheck_coupon_ajax.open("GET","/apps/powerstore/commerce/cart/ajax_server.html?sci="+company_id+"&status=coupon&formname="+formname+"&coupon_code="+coupon_code,true);
   httpcheck_coupon_ajax.onreadystatechange = handleHttpcheck_coupon_ajax;
   httpcheck_coupon_ajax.send(null);
}

function check_redemption_ajax(formname, company_id, faction)
{	
   httpcheck_redemption_ajax.open("GET","/apps/powerstore/commerce/cart/ajax_server.html?sci="+company_id+"&status=redemption&formname="+formname+"&faction="+faction,true);
   httpcheck_redemption_ajax.onreadystatechange = handleHttpcheck_redemption_ajax;
   httpcheck_redemption_ajax.send(null);
}

function check_select_ajax(company_id, val,qty)
{
   httpcheck_product_ajax.open("GET","/apps/powerstore/commerce/cart/ajax_server.html?sci="+company_id+"&status=select&val="+val+"&qty="+qty,true);
   httpcheck_product_ajax.onreadystatechange = handleHttpcheck_product_ajax;
   httpcheck_product_ajax.send(null);
}

function check_shipping_method_ajax(company_id, country, state, postal, usage, sType, uType)
{
   httpcheck_shipping_method_ajax.open("GET","/apps/powerstore/commerce/cart/ajax_server.html?sci="+company_id+"&status=method_checking&country="+country+"&state="+state+"&postal="+postal+"&usage="+usage+"&sType="+sType+"&uType="+uType,true);
   httpcheck_shipping_method_ajax.onreadystatechange = handleHttpcheck_shipping_method_ajax;
   httpcheck_shipping_method_ajax.send(null);
}

function check_product_delivery_exception_ajax(company_id, country, state, postal, usage, uType, uAddress)
{
   httpcheck_product_delivery_exception_ajax.open("GET","/apps/powerstore/commerce/cart/ajax_server.html?sci="+company_id+"&status=product_delivery_exception_checking&country="+country+"&state="+state+"&postal="+postal+"&usage="+usage+"&uType="+uType+"&uAddress="+uAddress,true);
   httpcheck_product_delivery_exception_ajax.onreadystatechange = handleHttpcheck_product_delivery_exception_ajax;
   httpcheck_product_delivery_exception_ajax.send(null);
}

function handleHttpcheck_shipping_carrier_ajax() {
  if (httpcheck_shipping_carrier_ajax.readyState == 4) {
	//document.writeln(httpcheck_shipping_carrier_ajax.responseText);
   //Split the comma delimited response into an array
	var shipping_service; 
	var address_id; 
	var status; 
	var instruction;
	var handling_service;

	eval(httpcheck_shipping_carrier_ajax.responseText);
	show_shipping_service_radio(shipping_service, address_id, status, instruction, handling_service);
  }
}

function handleHttpcheck_coupon_ajax() {
  if (httpcheck_coupon_ajax.readyState == 4) {
	//document.writeln(httpcheck_coupon_ajax.responseText);
	var valid_code, formname;
	eval(httpcheck_coupon_ajax.responseText);
	check_coupon_usage(formname, valid_code);
  }
}

function handleHttpcheck_redemption_ajax() {
  if (httpcheck_redemption_ajax.readyState == 4) {
	//document.writeln(httpcheck_redemption_ajax.responseText);
	var valid_code, formname, faction, coupon_code, coupon_name;
	eval(httpcheck_redemption_ajax.responseText);
	check_redemption_usage(formname, faction, valid_code, coupon_code, coupon_name);
  }
}

function handleHttpcheck_product_ajax() {
  if (httpcheck_product_ajax.readyState == 4) {
	//document.writeln(httpcheck_product_ajax.responseText);
	var msg;
	eval(httpcheck_product_ajax.responseText);
	if(msg.length) {
		qty_check(false, msg);
	} else qty_check(true, '');
  }
}

function handleHttpcheck_shipping_method_ajax() {
  if (httpcheck_shipping_method_ajax.readyState == 4) {
	//document.writeln(httpcheck_shipping_method_ajax.responseText);
	//Split the comma delimited response into an array
	var allowed; 
	var uType; 
	eval(httpcheck_shipping_method_ajax.responseText);
	show_shipping_method_alert(allowed, uType);
  }
}

function handleHttpcheck_product_delivery_exception_ajax() {
  if (httpcheck_product_delivery_exception_ajax.readyState == 4) {
	var country,state,postal,usage,uType,is_delivery_except_location,uAddress;
	var company_id;
    eval(httpcheck_product_delivery_exception_ajax.responseText);

    if (uAddress == "single") {
		if (is_delivery_except_location == "y") {
		    alert("One of your product does not delivery to this location");
		    return false;
		}
		else {
		    check_shipping_method_ajax(company_id, country, state, postal, usage, 'cart', uType);
		}
    }
    else {
        if (is_delivery_except_location == "y") {
            alert("One of your product does not delivery to your shipment location");
            return false;
        }
        else {
            submit_form();
        }
    }
  }
}

