function showOrderDetails(j) {

    if ($('#trOrderDetails' + j).is(":hidden")) {
        //alert("opening");
        $('#hrefShowArrow' + j).html('<img src=Images/history_up_arrow.png>');
    } else {
        //alert("closing");
        $('#hrefShowArrow' + j).html('<img src=Images/history_down_arrow.png>');
    }
    $('#trOrderDetails' + j).toggle();
}


function IsInt(value, divid) {
    if ((parseFloat(value) == parseInt(value)) && !isNaN(value)) {
    } else {
            if ($("#" + divid + "db").length > 0) {
                document.getElementById(divid).value = $("#" + divid + "db").val();
            } else {
            //alert(value);            
            document.getElementById(divid).value = 0;
            }
    }
}


function IsZero(divid, j) {
    max = ("avail_" + divid);
    max_amount = parseInt(document.getElementById(max).value);
    if (max_amount == 0) {
        ShowCartError("<div style='height:15px;'></div><div class='error_quantity_notice_zero'>&nbsp;&nbsp;No quantity available.</div><div style='height:9px;'></div>", j);
        document.getElementById(divid).value = 0; 
        return 1;
    }
}

function AddItem(divid, j, product_id, val) {
    //Check if manual input was a integer
    IsInt(val, divid);

    quantity = parseInt(document.getElementById(divid).value);



    var chkZero = IsZero(divid, j);
    if (chkZero == 1) {
        return false;
    }    
    
     
    //alert(quantity); 
    if (quantity <= 0 ){
        ShowCartError("<div style='height:15px;'></div><div class='error_quantity_notice'>Please enter the quantity <br>you would like to request.</div><div style='height:9px;'></div>", j);
    return false;
}


max = ("avail_" + divid);
max_amount = parseInt(document.getElementById(max).value);
if (quantity > max_amount) {
    document.getElementById(divid).value = max_amount;
    ShowCartError("<div style='height:10px;'></div>Quantity requested exceeds<br> quantity available.  Please  <br>enter an amount of " + max_amount + " or less ", j);
    return false;
    //alert(max_amount);
}

    //alert("Product_ID=" + product_id + "&Quantity=" + quantity + "&Order_Session=" + document.getElementById("Order_Session").value + "&Physician_ID=" + Get_Cookie("Fosrenol_ID"));
    //return false;
    $.ajax({
        type: "POST",
        url: "CartValidation.aspx",
        data: "Product_ID=" + product_id + "&Quantity=" + quantity + "&Order_Session=" + Get_Cookie("Order_Session") + "&Physician_ID=" + Get_Cookie("Fosrenol_ID"),
        success: function (msg) {
            //alert(msg);       

            //ShowCartError(msg,j)
            chk_result = msg.indexOf("ERR");
            //alert(chk_result);
            if (chk_result == 0) {
                ShowCartError(msg, j)
            } else {
                //ShowItemsSaved(ItemsInCartNotice());
                $(".ui-dialog-content").dialog("close");
                window.location.href = "Catalogue.aspx?Order_Session=" + Get_Cookie("Order_Session") + "&CartCount=" + msg + "&ShowMsg=0&product_type=" + getQuerystring("product_type") + "&page=" + getQuerystring("page");
                //document.getElementById("cartQuantity").innerHTML = msg + " Items in Cart";
                //scroll(0,0);
            }
            //document.getElementById(div).innerHTML = msg;
        }
    });

}

function RemoveItem(cart_id) {
    document.getElementById("divPlaceOrderButtons").style.display = "none";
document.getElementById('divUserCart').innerHTML='<center><img src=\"images/progress.gif\"></center>';
setTimeout("window.location.href='RemoveItem.aspx?cart_id=" + cart_id + "'",1250);
//[CART_ID]
}

function UpdateCount(cart_id,qamount) {

    $.ajax({
        type: "POST",
        url: "UpdateCount.aspx",
        data: "CART_ID=" + cart_id + "&QUANTITY=" + qamount,
        success: function (msg) {
        }
    });

}

function UpdateItem(divid, j, product_id, val, cart_id) {

    IsInt(val, divid);


        //alert(document.getElementById('pageDesc').value);
        if (document.getElementById('pageDesc').value == "/YourCart.aspx") {
            if (val == "0"){
            //alert(val);
            RemoveItem(cart_id);
            return false;
            }
        }


	$(".ui-dialog-content").dialog("close");
	quantity = parseInt(document.getElementById(divid).value);

    max = ("avail_" + divid);
    max_amount = parseInt(document.getElementById(max).value);

    if (max_amount==0) {
        document.getElementById(divid).value = max_amount;
        ShowCartError("<div style='height:15px;'></div><div class='error_quantity_notice_zero'>&nbsp;&nbsp;No quantity available.</div><div style='height:9px;'></div>", j);
        return false;
        //alert(max_amount);
    }

    if (quantity > max_amount) {
        document.getElementById(divid).value = max_amount;
        ShowCartError("<div style='height:10px;'></div>Quantity requested exceeds<br> quantity available.  Please  <br>enter an amount of " + max_amount + " or less ", j);
        return false;
        //alert(max_amount);
    }

    //alert(val); /* */
 
    //alert(quantity);
    if ((quantity <= 0) || (quantity == "NaN")) {
        ShowCartError("<div style='height:9px;'></div><div class='err_spacing'>Please enter the quantity you<br> would like to request.</div><div style='height:9px;'></div>", j);
    return false;
    }


document.getElementById("divPlaceOrderButtons").style.display = "none";

    $.ajax({
        type: "POST",
        url: "CartUpdate.aspx",
        data: "Product_ID=" + product_id + "&QuantityAvailable=" + max_amount + "&Quantity=" + quantity + "&Physician_ID=" + Get_Cookie("Fosrenol_ID") + "&Cart_ID=" + cart_id,
        success: function (msg) {

            chk_result = msg.indexOf("ERR");
            //alert(msg);
            if (chk_result == 0) {
                ShowCartError(msg, j);
                document.getElementById(divid).value = max_amount;

                document.getElementById("divPlaceOrderButtons").style.display = "block";
            } else {

                document.getElementById("divPlaceOrderButtons").style.display = "none";
                if (document.getElementById('divUpdateError').value > 0) {
                    //alert("1");
                    //document.getElementById("divPlaceOrderButtons").style.display = "block";
                    document.getElementById('divUserCart').innerHTML = '<center><img src=\"images/progress.gif\"></center>';
                    setTimeout("window.location.href='YourCart.aspx?Order_Session=" + Get_Cookie("Order_Session") + "&CartCount=" + getQuerystring("CartCount") + "'", 1250);
                } else {
                    //alert("2");
                    document.getElementById('divUserCart').innerHTML = '<center><img src=\"images/progress.gif\"></center>';
                    setTimeout("window.location.href='YourCart.aspx?Order_Session=" + Get_Cookie("Order_Session") + "&CartCount=" + getQuerystring("CartCount") + "'", 1250);
                }
            }

        }
    });      /**/

}

function closeDialog(){
$(".ui-dialog-content").dialog("close");
}

function ItemsInCartNotice (){
    return 'You have items saved in your cart. <br> <a href="YourCart.aspx?Order_Session=' + getQuerystring("Order_Session") + '&CartCount=' + getQuerystring("CartCount") + '" style="font-family: Arial, Helvetica, sans-serif;font-size: 11px;color: #016C64;">View Cart</a> &nbsp;&nbsp;&nbsp; <a href="#" OnClick="hideDiv(\'divCartMsgTop\');" style="font-family: Arial, Helvetica, sans-serif;font-size: 11px;color: #016C64;">Continue Browsing</a>';

}
function hideDiv(div) {
    //alert(div);
    document.getElementById(div).style.display = "none";
}

function ShowCartError(msg, j) {
    // alert("here");
    $('#quantity' + j).css('background-color', '#FF0000');
msg = (msg.replace("ERR:", ""));
$(document).ready(function () {
    document.getElementById("divQuantityError" + j).innerHTML = "<table border='0' cellpadding=0 cellspacing=0 class='quantity_error' id ='cartError" + j + " style='color:red;margin: 0 auto;overflow: hidden;'><tr><td width='25px'></td><td width='200px'><div class='err_spacing'>" + msg + "</div></td></tr></table>";
});
$("#divQuantityError"+j).show(10);
  /*  $(document).ready(function () {
    
   var width = 0;
   var height = 0;
    var $div = $('#posQuantityError' + j);
    var left = $div.offset().left;
    var top = $div.offset().top;

    var left = $div.offset().left - $(document).scrollLeft() - width + $div.outerWidth();
    var top = $div.offset().top - $(document).scrollTop() + $div.outerHeight();   
             
         $('<div class="quantity_error" id ="cartError' + j + '" style="color:red;margin: 0 auto;overflow: hidden;background-image: url(Images/cart_error_Qty_big.png);">' + msg + '<!--Quantity requested exceeds quantity available.  Please enter an amount less than 1--><!--<div style="position:fixed;margin-left:2;margin-top:6px" align="right"><img src="images/down_arrow.png" style=""></div>--></div>').dialog(
        {
        position: [left - 140, top - 85],
        resizable: false,
        height: 85,
        width: 175,
        }
        ); 
        $('.ui-dialog').css('border', '0px');
        $(".ui-dialog-titlebar").hide();
    });
    */

}



function ShowReturnCartError(msg, j) {
    // alert("here");
    $('#quantity' + j).css('background-color', '#FF0000');
    msg = (msg.replace("ERR:", ""));
    $(document).ready(function () {
        document.getElementById("divQuantityReturnError" + j).innerHTML = "<table border='0' cellpadding=0 cellspacing=0 class='quantity_error' id ='cartError" + j + " style='color:red;margin: 0 auto;overflow: hidden;'><tr><td width='12px'></td><td width='250px'><div class='err_spacing'>" + msg + "</div></td></tr></table>";
    });
    $("#divQuantityReturnError" + j).show(10);
    /*  $(document).ready(function () {
    
    var width = 0;
    var height = 0;
    var $div = $('#posQuantityError' + j);
    var left = $div.offset().left;
    var top = $div.offset().top;

    var left = $div.offset().left - $(document).scrollLeft() - width + $div.outerWidth();
    var top = $div.offset().top - $(document).scrollTop() + $div.outerHeight();   
             
    $('<div class="quantity_error" id ="cartError' + j + '" style="color:red;margin: 0 auto;overflow: hidden;background-image: url(Images/cart_error_Qty_big.png);">' + msg + '<!--Quantity requested exceeds quantity available.  Please enter an amount less than 1--><!--<div style="position:fixed;margin-left:2;margin-top:6px" align="right"><img src="images/down_arrow.png" style=""></div>--></div>').dialog(
    {
    position: [left - 140, top - 85],
    resizable: false,
    height: 85,
    width: 175,
    }
    ); 
    $('.ui-dialog').css('border', '0px');
    $(".ui-dialog-titlebar").hide();
    });
    */

}


function ShowItemsSaved(msg) { 
//alert(msg);
$(document).ready(function () {
document.getElementById("divCartMsgTop").innerHTML = "<table border='0' cellpadding=5 cellspacing=5 class='cat_copy' id ='cartError' style='margin: 0 auto;overflow: hidden;'><tr><td width='238px'>" + msg + "</td></tr></table>";
});
$("#divCartMsgTop").show(10);



    /*$(document).ready(function () {
    j = 0
   var width = 0;
   var height = 0;
    var $div = $('#posItemsSaved' + j);
    var left = $div.offset().left;
    var top = $div.offset().top;

    var left = $div.offset().left - $(document).scrollLeft() - width + $div.outerWidth();
    var top = $div.offset().top - $(document).scrollTop() + $div.outerHeight();   
             
         $('<div class="quantity_error" style="margin: 0 auto;overflow: hidden;"><table width=100% cellpadding="0" cellspacing="0"><tr><td>' + msg + '</td><td><img src="images/right_arrow.png" style="position:fixed;padding-left:12px;margin-top:-20px;z-index:1;"></td></tr></table></div>').dialog(
        {
        position: [left - 425, top - 45],
        resizable: false,
        height: 75,
        width: 210,
        }
        ); 
        //$('.ui-dialog').css('border', '0px');
        $(".ui-dialog-titlebar").hide(); 
    });
    */

}
 
 

function getCartSummary(order_session) {  
    $.ajax({
        type: "POST",
        url: "CartSummary.aspx",
        data: "order_session=" + order_session,
        success: function (msg) {


            //alert(msg);
            if (msg != ""){
            $(document).ready(function () {
            document.getElementById("posItemsSaved0").innerHTML = "<table border='0' cellpadding=5 cellspacing=5 id ='cartError' ><tr><td width='278px'><div style='overflow:auto;overflow-x:hidden;'>" + msg + "</div></td></tr></table>";
            });
        //$("#posItemsSaved0").show(10);
        $("#posItemsSaved0").css("visibility", "visible");
        $("#posItemsSaved0").mouseover(function () {
            $("#posItemsSaved0").css("visibility", "visible");
        }).mouseout(function () {
            //alert("2"); 
            $("#posItemsSaved0").css("visibility", "hidden");
        });
            }

            /*ShowCartSummary(msg);*/
        }
    }); 
}

function ShowCartSummary(msg) {
    //alert(msg);

    /*
    $(document).ready(function () {
    j = 0
    var width = 0;
    var height = 0;
    var $div = $('#posItemsSaved' + j);
    var left = $div.offset().left;
    var top = $div.offset().top; 




    var left = $div.offset().left - $(document).scrollLeft() - width + $div.outerWidth();
    var top = $div.offset().top - $(document).scrollTop() + $div.outerHeight();   
             
    $('<div class="quantity_error" style="margin: 0 auto;overflow:auto;overflow-x:hidden;"><table width=100% cellpadding="0" cellspacing="0"><tr><td><img src="images/up_arrow.png" style="position:fixed;padding-left:155px;margin-top:-21px;z-index:1;"></td></tr><tr><td>' + msg + '</td><!----></tr></table></div>').dialog(
    {
    position: [left - 290, top + 5],
    resizable: false,
    height: 350,
    width: 210,
    }
    );
    //$('.ui-dialog').css('border', '0px');
    $(".ui-dialog-titlebar").hide(); 
    });
    */

}




function CountUp(divid) {
IsInt(document.getElementById(divid).value, divid);
val = parseInt(document.getElementById(divid).value);


max = ("avail_" + divid);
max_amount = parseInt(document.getElementById(max).value);

val = (val) + 1;
document.getElementById(divid).value = val;

    if (val >= max_amount) {
        document.getElementById(divid).value = max_amount; 
    }

    if (val <= 0) {
        document.getElementById(divid).value = 0; 
    }
 
}


function CountDown(divid, cart_id) {
    IsInt(document.getElementById(divid).value, divid);
    val = parseInt(document.getElementById(divid).value); 

    //alert(val);
    if (val == 0){
    val = 0;
    }
    if (val <= 0){
    val = 0;
    }


    if (val != 0) {
    val = (val) - 1;
    document.getElementById(divid).value = (val);
    }

    //alert(val + " " + cart_id);
    //alert(val);
    if (val == 0) {
        if (cart_id != "[CART_ID]") {
        RemoveItem(cart_id);
        }
    }
}


function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function off(divid) {
   document.getElementById(divid).style.display = 'none';
}
function on(divid) {
   document.getElementById(divid).style.display = '';
}

function backgroundOnFocus(divid, errmsg) {
    //alert(divid);
    document.getElementById('ctl00_MainContent_' + divid).style.background = "white";
    document.getElementById('ctl00_MainContent_errlbl' + errmsg).innerHTML = "";
}

$(function () {
    // Dialog
    $('#dialog').dialog({
        autoOpen: false,
        width: 600,
        resizable: false,
        buttons: {
            "Ok": function () {
                $(this).dialog("close");
            },
            "Cancel": function () {
                $(this).dialog("close");
            }
        }
    });
});




function showDialog(divid) {
    //alert(divid)
    $("#" + divid).dialog({ width: 'auto' });
    $(".ui-dialog-titlebar").hide();
    return false;
}

function SetWhite(id) {
    $('#ctl00_MainContent_' + id).css('background-color', '#ffffff');
    //alert(id);
}
//http://stackoverflow.com/questions/1301508/change-textboxs-css-class-when-asp-net-validation-fails
        function RegClick() {
            var v1 = "#<%= rev1.ClientID %>";
            var v2 = "#<%= rev2.ClientID %>";
            var val = Page_ClientValidate();
            //alert("here");
            //alert(v1);
            if (!val) {
                var i = 0;
                for (; i < Page_Validators.length; i++) {
                    if (!Page_Validators[i].isvalid) {
                        //alert(Page_Validators[i].controltovalidate);
                        $("#" + Page_Validators[i].controltovalidate).css("background-color", "red");
                    }
                }
            }            
            return val;
        } 
