﻿// JScript File
function PostProduct(id_number)
{
    var stock_status;
    if(!(document.getElementById("var_stock_"+id_number)))
    {
        //alert("not found");
        stock_status = "";
    }
    else 
    {
        //alert("found");
        var product_to_find = "var_stock_" + id_number;
        //alert(product_to_find);
        stock_status = document.getElementById(product_to_find).innerHTML;
        //alert(stock_status);
    }       
    prod_id = document.getElementById("select_color_"+id_number).value;
    qty = document.getElementById("qty_"+id_number).value;

    if(parseInt(qty)!= qty-0) 
    {
        alert("Item quantity has to be a numeric value.");
    }
    else
    if(stock_status == "Discontinued")
    {
        alert ("This product has been marked as discontinued. You can NOT purchase it anymore");
    }
    else
    if(parseInt(qty) > 0)
    {
        addProduct(prod_id, parseInt(qty), id_number);
    }
    else
    {
        alert("Item quantity can't be zero.");
    }
}

//************************************************** ajax scripts ******************************************//
var xmlHttp
//var xmlHttp1

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;
}
//************************************************** ajax scripts ******************************************//


//************************************************** product color scripts ******************************************//
var xhColor
var select_id_number
function getColors(product_size_id, id_number)
{
    //alert(product_size_id);
    //alert(id_number);
    select_id_number = id_number;
    xhColor=GetXmlHttpObject();
    if (xhColor==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    }     
    else
    {
        //document.getElementById("statsandtestimonies").style.backgroundImage = "url('img/pic/ajax_processing.gif')";
        var url = ""; var websitename = ExtractWebsiteName(location.href); var handlerpage = "/jshandler.aspx"; url = websitename + handlerpage;
        //alert(url);
        xhColor.onreadystatechange=showColors;//showColors is the next function called
        xhColor.open("POST",url,true);
        xhColor.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhColor.send("optn=get&item=prodcolor&val="+product_size_id+"&idnumber="+id_number);               
    }
}

function showColors()
{
    if (xhColor.readyState==4)
    { 
 //       alert(xhColor.responseText);
        document.getElementById("div_color_"+select_id_number).innerHTML = xhColor.responseText;
        if(xhColor.responseText != "")
        {
         
            getStockStatus(document.getElementById("select_color_"+select_id_number).value, select_id_number);
            getPrice(document.getElementById("select_color_"+select_id_number).value, select_id_number);
        }
    }    
}

//************************************************** product color scripts ******************************************//


//************************************************** product price scripts ******************************************//
var xhPrice
var price_idnumber
var price_pscid
function getPrice(product_size_color_id, id_number)
{
//    alert(product_size_color_id);
//    alert(id_number);
    price_idnumber = id_number;
    price_pscid = product_size_color_id;
    xhPrice=GetXmlHttpObject();
    csign = document.getElementById("hidden_c_sign").value;
    crate = document.getElementById("hidden_c_rate").value;
    //alert(csign + crate);
    if (xhPrice==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    }     
    else
    {
        //document.getElementById("statsandtestimonies").style.backgroundImage = "url('img/pic/ajax_processing.gif')";
        var url = ""; var websitename = ExtractWebsiteName(location.href); var handlerpage = "/jshandler.aspx"; url = websitename + handlerpage;
        //alert(url);
        xhPrice.onreadystatechange=showPrice;//showColors is the next function called
        xhPrice.open("POST",url,true);
        xhPrice.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhPrice.send("optn=get&item=prodprice&val="+product_size_color_id+"&csign="+csign+"&crate="+crate);               
    }
}

function showPrice()
{
    if (xhPrice.readyState==4)
    { 
        if(xhPrice.responseText != "")
        {
            document.getElementById("price_block_"+price_idnumber).innerHTML = xhPrice.responseText;
        }
        else
        {
            document.getElementById("price_block_"+price_idnumber).style.display = "none";
        }
        getSpecialPrice(price_pscid, price_idnumber);
    }
        
}

//************************************************** product price scripts ******************************************//


//************************************************** product special price scripts ******************************************//
var xhSplPrice
var splprice_idnumber
var splprice_pscid
function getSpecialPrice(product_size_color_id, id_number)
{
//    alert(product_size_color_id);
//    alert(id_number);
    splprice_idnumber = id_number;
    splprice_pscid = product_size_color_id;
    xhSplPrice=GetXmlHttpObject();
    csign = document.getElementById("hidden_c_sign").value;
    crate = document.getElementById("hidden_c_rate").value;
    if (xhSplPrice==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    }     
    else
    {
        //document.getElementById("statsandtestimonies").style.backgroundImage = "url('img/pic/ajax_processing.gif')";
        var url = ""; var websitename = ExtractWebsiteName(location.href); var handlerpage = "/jshandler.aspx"; url = websitename + handlerpage;
        xhSplPrice.onreadystatechange=showSpecialPrice;//showColors is the next function called
        xhSplPrice.open("POST",url,true);
        xhSplPrice.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhSplPrice.send("optn=get&item=prodsplprice&val="+product_size_color_id+"&csign="+csign+"&crate="+crate);               
    }
}

function showSpecialPrice()
{
    if (xhSplPrice.readyState==4)
    { 
        if(xhSplPrice.responseText != "")
        {
            document.getElementById("sp_price_block_"+splprice_idnumber).style.display  = "block";
            document.getElementById("sp_price_block_"+splprice_idnumber).innerHTML = xhSplPrice.responseText;
        }
        else
        {
            document.getElementById("sp_price_block_"+splprice_idnumber).style.display = "none";
        }
    }    
}

//************************************************** product special price scripts ******************************************//

//************************************************** product status scripts ******************************************//
var xhStatus
var status_id_number
var status_pscid
function getStockStatus(product_size_color_id, id_number)
{
    status_id_number = id_number;
    xhStatus = GetXmlHttpObject();
    if (xhStatus==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    }     
    else
    {
        //document.getElementById("statsandtestimonies").style.backgroundImage = "url('img/pic/ajax_processing.gif')";
        var url = ""; var websitename = ExtractWebsiteName(location.href); var handlerpage = "/jshandler.aspx"; url = websitename + handlerpage;
        xhStatus.onreadystatechange=showStockStatus;//showColors is the next function called
        xhStatus.open("POST",url,true);
        xhStatus.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhStatus.send("optn=get&item=prodstatus&val="+product_size_color_id+"&idnumber="+status_id_number);               
    }
}

function showStockStatus()
{
    if (xhStatus.readyState==4)
    { 
        //alert(xhStatus.responseText);
        if(xhStatus.responseText != "")
        {
            document.getElementById("stock_block_"+status_id_number).style.display  = "block";
            document.getElementById("stock_block_"+status_id_number).innerHTML = xhStatus.responseText;
        }
        else
        {
            document.getElementById("stock_block_"+status_id_number).style.display = "none";
        }
    }    
}

//************************************************** product status scripts ******************************************//

//************************************************** product add scripts ******************************************//
var xhAddProduct
var prodadd_id_number
function addProduct(product_size_color_id, quantity, idnumber)
{
    prodadd_id_number = idnumber;
    //alert("in add product");
    //alert(prodadd_id_number);
    xhAddProduct=GetXmlHttpObject();
    if (xhAddProduct==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    }     
    else
    {
    //        document.write("http://www.pakcosmetics.com/jshandler.aspx?optn=set&item=prodadd&val="+product_size_color_id+"&qty="+quantity+"&linkurl="+document.URL+"&idnumber="+prodadd_id_number);
        //document.getElementById("statsandtestimonies").style.backgroundImage = "url('img/pic/ajax_processing.gif')";
        var url = ""; var websitename = ExtractWebsiteName(location.href); var handlerpage = "/jshandler.aspx"; url = websitename + handlerpage;
        xhAddProduct.onreadystatechange=showConfirmation;//showColors is the next function called
        xhAddProduct.open("POST",url,true);
        xhAddProduct.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhAddProduct.send("optn=set&item=prodadd&val="+product_size_color_id+"&qty="+quantity+"&linkurl="+document.URL+"&idnumber="+prodadd_id_number);               
    }
}

function showConfirmation()
{
    if (xhAddProduct.readyState==4)
    { 
        //alert(xhAddProduct.responseText);
        if(xhAddProduct.responseText != "")
        {
            document.getElementById("confirm_block_"+prodadd_id_number).style.display = "block";
            document.getElementById("confirm_block_"+prodadd_id_number).innerHTML = xhAddProduct.responseText; 
        }
        else
        {
            alert("Oops! looks like your session expired, please refresh this page and try again.");
        }
    }    
}

//************************************************** product add scripts ******************************************//

function ExtractWebsiteName(urltoextract) {
    var websitename = "";
    var indexend = urltoextract.indexOf('/', 8);
    if (indexend > -1) {
        try { websitename = urltoextract.substring(0, indexend) }
        catch (err) { websitename = ""; }
    }
    else {
        websitename = urltoextract;
    }
    return websitename;
}
