function ExpandProductList(id, clicked){
    var p = clicked.parentNode.parentNode;
	$("#div_"+id, p).toggle();
	if ($("#img_" + id, p).attr("src") == "/satcms/images/collapse.gif")
	    $("#img_" + id, p).attr("src", "/satcms/images/expand.gif");
	else
	    $("#img_" + id, p).attr("src", "/satcms/images/collapse.gif");
}

function LoadProduct(productID){
    $.ajax({
        url: "/SATCMS/includes/frm_product_view.asp?id=" + productID,
        success: function(data, textStatus){
            $('#tabareaProducts_View').html(data);
            SelectThisTab('tablinkProducts_View');
            $('#tablinkProducts_NoComm').attr("href", "/SATCMS/includes/Product.asp?id=" + productID);
            $('#tablinkProducts_NoComm').css('display', 'block');
            $('#tablinkProducts_Edit').css('display', 'none');
            $.ajax({
                url: "/SATCMS/includes/frm_product_edit.asp?id=" + productID,
                success: function(data, textStatus){
                    $('#tabareaProducts_Edit').html(data);
                    PrepProductEdit();
                    $('#tablinkProducts_Edit').css('display','block');
                }
            });
        } 
    });
}

function PrepProductEdit(){

    $('.ajax').ajaxContent();

    $('#ProductDelete_form').ajaxForm({ 
        beforeSubmit: function(formData, jqForm, options){
            if(confirm('Really delete this product?')==false) return false;
        },
        success: function(responseText, statusText){
            $('#tabareaProducts_View').html('');
            $('#tabareaProducts_Edit').html('');
            $('#tablinkProducts_View').css('display','none');
            $('#tablinkProducts_NoComm').css('display','none');
            $('#tablinkProducts_Edit').css('display','none');
            RefreshProductList();
            SelectThisTab('tablinkProducts_Search');
        }
    });

    $('#ProductClone_form').ajaxForm({ 
        success: function(responseText, statusText){
            LoadProduct(responseText);
            RefreshProductList();
        }
    });

    $('#ProductVariant_form').ajaxForm({ 
        success: function(responseText, statusText){
            LoadProduct(responseText);
            RefreshProductList();
        }
    });

    $('#ProductBasic_form').ajaxForm({ 
        success: function(responseText, statusText) {
            $("#ProductNameHeader").html($("#ProductBasic_form #ProductName").val());
            RefreshProductList();
            $.cursorMessage('Update Complete', { offsetX: 20, offsetY: 8 });
        }
    });

    $('#ProductLiquidity_form').ajaxForm({
        success: function(responseText, statusText) {
            $.cursorMessage('Update Complete', { offsetX: 20, offsetY: 8 });
        }
    });

}

function RefreshProductList(){
    $.ajax({
        url: "/SATCMS/includes/frm_product_search.asp",
        success: function(data, textStatus){
            $('#tabareaProducts_Search').html(data);
        }
    });
    $.ajax({
        url: "/SATCMS/includes/frm_product_list.asp",
        success: function(data, textStatus){
            $('#tabareaProducts_Browse').html(data);
        }
    });
}

function SaveValue(oButton,sValueID,sTable){
    oButton.disabled = true;
    url = "/SATCMS/includes/ajax.asp?object=Product&id=" + sValueID + "&table=" + sTable;
    $("#table_"+sTable+" select,#table_"+sTable+" input").each(function(i){
        if(this.id.substring(this.id.search('~')+1, this.id.length)==sValueID) {
            url += '&'+this.id.substr(0,this.id.search('~'))+'='+encodeURIComponent(this.value);
        }
    });
    url += '&action=SaveTable&sid='+Math.random();
    $.ajax({
        url: url,
        success: function(data, textStatus){
            oButton.disabled = false;
            $.cursorMessage('Update Complete', { offsetX: 20, offsetY: 8 });
        }
    });
}

function AddValue(oButton,sTable){
    oButton.disabled = true;
    url = "/SATCMS/includes/ajax.asp?object=Product&table=" + sTable + "&ProductID=" + sProductID;
    $("#table_"+sTable+" select,#table_"+sTable+" input").each(function(i){
        if(this.id.substring(this.id.search('~')+1, this.id.length)==sTable) {
            url += '&'+this.id.substr(0,this.id.search('~'))+'='+encodeURIComponent(this.value);
        }
    });
    url += '&action=AddTable&sid='+Math.random();
    $.ajax({
        url: url,
        success: function(data, textStatus){
	        $("#table_"+sTable).html(data);
        }
    });
}

function DeleteValue(oButton,sValueID,sTable){
    oButton.disabled = true;
    url = "/SATCMS/includes/ajax.asp?object=Product&id=" + sValueID + "&table=" + sTable;
    url += '&action=DeleteTable&sid='+Math.random();
    $.ajax({
        url: url,
        success: function(data, textStatus){
	        $("#table_"+sTable).html(data);
        }
    });
}

function SelectAllStates(){
    $("#tabareaProductEdit_States :checkbox").attr("checked",true);
}

function UnselectAllStates(){
    $("#tabareaProductEdit_States :checkbox").attr("checked",false);
}

function SaveStates(oButton){
    oButton.disabled = true;
    var states = '';
    $("#tabareaProductEdit_States :checkbox").each(function(i){
        if(this.checked) states += this.id.substr(6)+',';
    });
    url = "/SATCMS/includes/ajax.asp?object=Product&ProductID=" + sProductID;
    url += '&states='+states.substring(0,states.length - 1);
    url += '&action=SaveStates&sid='+Math.random();
    $.ajax({
        url: url,
        success: function(data, textStatus){
            oButton.disabled = false;
            $.cursorMessage('Update Complete', { offsetX: 20, offsetY: 8 });
        }
    });
}

function EnableField(sFieldID){
    if ($("#"+sFieldID).attr("disabled") == true) {
        $("#"+sFieldID).attr("disabled", false);
    }
    else {
        if (confirm('Do you want to revert back to the base product value?')) {
            $("#"+sFieldID).val($("#"+sFieldID+"__basevalue").html());
            $("#"+sFieldID).attr("disabled", true);
        }
        else {
            return false;
        }
    }
}

function EnableTable(oCheckBox,sProductID,sTable){
    if (oCheckBox.checked) {
        //copy table records from base to variant
        CopyBaseTable(oCheckBox,sProductID,sTable);
    }
    else {
        if (confirm('Do you want to revert back to the base product values?')) {
            //delete table records for variant
            DeleteVariantTable(oCheckBox,sProductID,sTable);
        }
        else {
            return false;
        }
    }
}

function CopyBaseTable(oButton,sProductID,sTable){
    oButton.disabled = true;
    url = "/SATCMS/includes/ajax.asp?object=Product&ProductID=" + sProductID + "&table=" + sTable;
    url += "&action=CopyBaseTable&sid=" + Math.random();
    $.ajax({
        url: url,
        success: function(data, textStatus){
	        $("#table_"+sTable).html(data);
        }
    });
}

function DeleteVariantTable(oButton,sProductID,sTable){
    oButton.disabled = true;
    url = "/SATCMS/includes/ajax.asp?object=Product&ProductID=" + sProductID + "&table=" + sTable;
    url += '&action=DeleteVariantTable&sid='+Math.random();
    $.ajax({
        url: url,
        success: function(data, textStatus){
	        $("#table_"+sTable).html(data);
        }
    });
}

function EnableStates(oCheckBox,sProductID){
    if (oCheckBox.checked) {
        //copy table records from base to variant
        CopyBaseStates(oCheckBox,sProductID);
    }
    else {
        if (confirm('Do you want to revert back to the base product values?')) {
            //delete table records for variant
            DeleteVariantStates(oCheckBox,sProductID);
        }
        else {
            return false;
        }
    }
}

function CopyBaseStates(oButton,sProductID){
    oButton.disabled = true;
    url = "/SATCMS/includes/ajax.asp?object=Product&ProductID=" + sProductID;
	url += '&action=CopyBaseStates&sid='+Math.random();
    $.ajax({
        url: url,
        success: function(data, textStatus){
	        $("#tabareaProductEdit_States").html(data);
        }
    });
}

function DeleteVariantStates(oButton,sProductID){
    oButton.disabled = true;
    url = "/SATCMS/includes/ajax.asp?object=Product&ProductID=" + sProductID + "&states=";
	url += "&action=SaveStates&refresh=y&sid=" + Math.random();
    $.ajax({
        url: url,
        success: function(data, textStatus){
	        $("#tabareaProductEdit_States").html(data);
        }
    });
}

function ReleaseProduct(sProductID) {
    url = "/SATCMS/includes/ajax.asp?object=Product&ProductID=" + sProductID;
    url += "&action=ToggleReleased&sid=" + Math.random();
    $.ajax({
        url: url,
        success: function(data, textStatus) {
            if (data > '') {
                alert(data);
            }
            else {
                $.cursorMessage('Update Complete', { offsetX: 20, offsetY: 8 });
            }
        }
    });
}

function TopPickProduct(sProductID) {
    url = "/SATCMS/includes/ajax.asp?object=Product&ProductID=" + sProductID;
    url += "&action=ToggleTopPick&sid=" + Math.random();
    $.ajax({
        url: url,
        success: function(data, textStatus) {
            if (data > '') {
                alert(data);
            }
            else {
                $.cursorMessage('Update Complete', { offsetX: 20, offsetY: 8 });
            }
        }
    });
}

function EditValueList(sSelect,sValue,sType){
    url = "/SATCMS/includes/ajax.asp?object=Value&type=" + sType + "&select=" + sSelect + "&value=" + sValue;
	url += "&action=EditValueList&sid=" + Math.random();
    $.ajax({
        url: url,
        success: function(data, textStatus){
			$("#div_EditValueList").html(data);
			$("#div_EditValueList").css("display","block");
			$.scrollTo("#div_EditValueList", 700);
}
    });
}

function SaveListChanges(oButton,idValue){
    oButton.disabled = true;
    url = "/SATCMS/includes/ajax.asp?object=Value&id=" + idValue;
    url += "&short=" + encodeURIComponent($('#short_'+idValue).val());
    url += "&long=" + encodeURIComponent($('#long_'+idValue).val());
    if($('#extra_'+idValue)[0]) url += "&extra=" + encodeURIComponent($('#extra_'+idValue).val());
    if($('#category_' + idValue)[0]) url += "&category=" + encodeURIComponent($('#category_'+idValue).val());
    url += "&action=SaveValue&sid=" + Math.random();
    $.ajax({
        url: url,
		beforeSend: function(xhr) {
			xhr.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
		},
        success: function(data, textStatus){
	        oButton.disabled = false;
	        $.cursorMessage('Update Complete', { offsetX: 20, offsetY: 8 });
	    }
    });
}

function AddListValue(oButton,idType){
    oButton.disabled = true;
    url = "/SATCMS/includes/ajax.asp?object=Value&id=" + idType;
    url += "&short=" + encodeURIComponent($('#short_'+idType).val());
    url += "&long=" + encodeURIComponent($('#long_'+idType).val());
    if($('#extra_'+idType)[0]) url += "&extra=" + encodeURIComponent($('#extra_'+idType).val());
    url += "&action=AddValue&sid=" + Math.random();
    $.ajax({
        url: url,
		beforeSend: function(xhr) {
			xhr.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
		},
        success: function(data, textStatus){
   	        $("#div_"+idType).html(data);
        }
    });
}

function DeleteListValue(oButton,idValue,idType){
    oButton.disabled = true;
    url = "/SATCMS/includes/ajax.asp?object=Value&id=" + idValue;
    url += "&action=DeleteValue&sid=" + Math.random();
    $.ajax({
        url: url,
        success: function(data, textStatus){
   	        $("#div_"+idType).html(data);
        }
    });
}

function MoveListValue(oButton,idValue,idType,iMove){
    oButton.disabled = true;
    url = "/SATCMS/includes/ajax.asp?object=Value&id=" + idValue;
    url += "&action=MoveValue&move=" + iMove + "&sid=" + Math.random();
    $.ajax({
        url: url,
        success: function(data, textStatus){
   	        $("#div_"+idType).html(data);
        }
    });
}

function RefreshSelect(sSelect,sValue){
    url = "/SATCMS/includes/ajax.asp?object=Value&select=" + sSelect + "&value=" + sValue;
	url += "&action=RefreshSelect&sid=" + Math.random();
    $.ajax({
        url: url,
        success: function(data, textStatus){
			$("#tabareaProducts_Edit #"+sSelect.replace('~','\\~')).parent().html(data);
			$('#div_EditValueList').css("display","none");
			$('#div_EditValueList').html("");
        }
    });
}

function MarkRatesCurrent(sInsCompanyID){
    url = "/SATCMS/includes/ajax.asp?object=Product&company=" + sInsCompanyID;
    url += "&action=MarkRatesCurrent&sid=" + Math.random();
    $.ajax({
        url: url,
        success: function(data, textStatus){
   	        $("#RateDate_"+sInsCompanyID).html(data);
        }
    });
}

function SearchProductsBasic(oButton) {
    oButton.disabled = true;
    url = "/SATCMS/includes/ajax.asp?object=Product&action=SearchProductsBasic";
    $("#BasicProductSearchFields select,#BasicProductSearchFields input").each(function(i) {
        if (this.id.search('__') == -1 && this.value > '' && $('#' + this.id.replace('~', '\\~').replace('!', '\\!') + '__operator')[0]) {
            url += '&' + this.id + '=' + encodeURIComponent($('#' + this.id.replace('~', '\\~').replace('!', '\\!') + '__operator').val() + '_' + this.value);
        }
    });
    var states = '';
    $("#tabareaProductBasicSearch_States :checkbox").each(function(i) {
        if (this.checked) states += this.id.substr(6) + ',';
    });
    url += '&states=' + states.substring(0, states.length - 1);
    url += '&sid=' + Math.random();
    $.ajax({
        url: url,
        success: function(data, textStatus) {
            $("#BasicProductSearchResults").html(data);
            oButton.disabled = false;
            $.scrollTo("#BasicProductSearchResults", 700);
        }
    });
}

function SearchProducts(oButton){
    oButton.disabled = true;
    url = "/SATCMS/includes/ajax.asp?object=Product&action=SearchProducts";
    $("#ProductSearchFields select,#ProductSearchFields input").each(function(i){
        if(this.id.search('__') == -1 && this.value > '' && $('#'+this.id.replace('~','\\~')+'__operator')[0]){
            url += '&'+this.id+'='+encodeURIComponent($('#'+this.id.replace('~','\\~')+'__operator').val()+'_'+this.value);
        }
    });
    var states = '';
    $("#tabareaProductSearch_States :checkbox").each(function(i){
        if(this.checked) states += this.id.substr(6)+',';
    });
    url += '&states='+states.substring(0,states.length - 1);
    url += '&sid='+Math.random();
    $.ajax({
        url: url,
        success: function(data, textStatus){
   	        $("#ProductSearchResults").html(data);
	        oButton.disabled = false;
	        $.scrollTo("#ProductSearchResults", 700);
	    }
    });
}