$(document).ready(function(){
    $('.add_order_item').bind('click', function(){
        $(this).attr('disabled', 'disabled');
    })
})

//display eligible SKUs
function displayUpgrade(product_id){
    if($('#upgrade_container_'+product_id).html() != ''){
        if($('#upgrade_row_container_'+product_id).css('display') != 'none'){
            //$('#upgrade_row_container_'+product_id).slideUp('slow');
            $('#upgrade_row_container_'+product_id).fadeOut('slow');
        } else {
            //$('#upgrade_row_container_'+product_id).slideDown('slow');
            $('#upgrade_row_container_'+product_id).fadeIn('slow');
        }
    } else {
        var is_buy_now = 1;
        if($('#is_buy_now').length > 0){
            is_buy_now = $('#is_buy_now').val();
        }

        loadPiece(root_url+'/vendor_products/eligible', '#upgrade_container_'+product_id, {'product_id': product_id, 'is_buy_now': is_buy_now});
        /*$('#upgrade_container_'+product_id).html("<center><img src='"+root_url+"/img/loadingAnimation.gif' /></center>");
        //$('#upgrade_row_container_'+product_id).slideDown('slow');
        $('#upgrade_row_container_'+product_id).fadeIn('slow');
        $.post(root_url+'/vendor_products/eligible', {'product_id': product_id}, function(data){
            $('#upgrade_container_'+product_id).html(data);
        })*/
    }
}

function submitform(frm){
    if(frm == 'AuctionCheckoutForm'){
	if($("#AuctionTerms").attr('checked')){
            $("#"+frm).submit();
            return false;
	}else{
            alert("Please Accept Terms and Conditons");
            return false;
	}
    }
}

function counter_auction(expiry,pos){	
    count_auction = document.getElementById("count_auction").value;
    expiry = Date.parse(expiry);
    timeleft = new Date(expiry);
    $('#defaultCountdown_customer'+pos).countdown({until: timeleft,format: 'yOdHMS',compact: false,
        layout: 'just <b>{d<}{dn} {dl} {d>}{hnn}{sep}{mnn}{sep}{snn}</b> {desc}',
        description: ' left'});
}

function getList(root, controller, calling_obj_type, calling_obj_id, row_container, col_container, select_box, current_model, display_no_value, is_multiple){
    if(row_container != ''){
        $('#'+row_container).hide('fast');
        $('#'+col_container).html("");

        if(calling_obj_id > 0){
            $('#'+col_container).html("<img src='"+root+"/img/loadingAnimation.gif' />");
            $('#'+row_container).fadeIn('slow');
            $.get(root+'/'+controller+'/getList/'+calling_obj_id+'/'+calling_obj_type+'/'+select_box+'/'+current_model+'/'+display_no_value+'/'+is_multiple, function(data){
                $('#'+col_container).html(data);
            });
        }
    } else {
        
        $('#'+col_container).hide('fast');
        $('#'+col_container).html("");
        if(calling_obj_id > 0){            
            $('#'+col_container).html("<img src='"+root+"/img/loadingAnimation.gif' />");            
            $('#'+col_container).fadeIn('slow');
            $.get(root+'/'+controller+'/getList/'+calling_obj_id+'/'+calling_obj_type+'/'+select_box+'/'+current_model+'/'+display_no_value, function(data){
                $('#'+col_container).html(data);
            });
        }
    }
}

// Created by Thu Ra. 2011-01-03 03:40 PM
function getClear(root, controller, calling_obj_id, row_container, col_container){
    if(row_container != ''){
        $('#'+row_container).hide('fast');
        $('#'+col_container).html("");

        if(calling_obj_id > 0){
            $('#'+col_container).html("<img src='"+root_url+"/img/loadingAnimation.gif' />");
            $('#'+row_container).fadeIn('slow');
            $.get(root_url+'/'+controller+'/EmsisoftOrderForm/'+calling_obj_id, function(data){
                $('#'+col_container).html(data);
            });
            getLicense(root_url, controller, '7','', "product_list_col");
        }
    } else {
        $('#'+col_container).hide('fast');
        $('#'+col_container).html("");
        if(calling_obj_id != ''){
            $('#'+col_container).html("<img src='"+root_url+"/img/loadingAnimation.gif' />");
            $('#'+col_container).fadeIn('slow');
            //alert(root+'/'+controller+'/getClear/'+calling_obj_id);

            $.get(root_url+'/'+controller+'/getClear/'+calling_obj_id, function(data){
                $('#'+col_container).html(data);
            });
            getLicense(root_url, controller, '7','', "product_list_col");
        }
    }
}
function getLicense(root, controller, calling_obj_id, row_container, col_container){  
    if(row_container != ''){
        $('#'+row_container).hide('fast');
        $('#'+col_container).html("");

        if(calling_obj_id > 0){
            $('#'+col_container).html("<img src='"+root_url+"/img/loadingAnimation.gif' />");
            $('#'+row_container).fadeIn('slow');
            $.get(root_url+'/'+controller+'/getLicense/'+calling_obj_id, function(data){
                $('#'+col_container).html(data);
            });
            $('#unit_price').html("SGD");
            $('#subtotal').html("");
            $('#div_qty').html("<select><option>0</option></select>");
            $('#GST').html("SGD");
            $('#grandtotal').html("SGD");
        }
    } else {
        $('#'+col_container).hide('fast');
        $('#'+col_container).html("");
        if(calling_obj_id != ''){
            $('#'+col_container).html("<img src='"+root_url+"/img/loadingAnimation.gif' />");
            $('#'+col_container).fadeIn('slow');
            //alert(root+'/'+controller+'/getLicense/'+calling_obj_id);
            $.get(root_url+'/'+controller+'/getLicense/'+calling_obj_id, function(data){
                $('#'+col_container).html(data);
            });
            $('#unit_price').html("SGD");
            $('#subtotal').html("");
            $('#div_qty').html("<select><option>0</option></select>");
            $('#GST').html("SGD");
            $('#grandtotal').html("SGD");
        }
    }
}

/* Start: Added by Surya {Function to get all prices for 1 product} */
function calPriceNew(product_id){
	$.post(root_url+'/vendor_products/get_price/'+product_id, {}, function(data){
		$('#price_container_'+product_id).html(data);
	});
}

function loadUpgradeEligible(product_id){
	var is_buy_now = 1;
	if($('#is_buy_now').length > 0){
		is_buy_now = $('#is_buy_now').val();
	}
	
	loadPiece(root_url+'/vendor_products/eligible', '#upgrade_pack_container_'+product_id, {'product_id': product_id, 'is_buy_now': is_buy_now});
}
/* End: Added by Surya */


//get price to display in the product list
function calPrice(product_id){
    if($('#price_container_'+product_id).html() != ''){
        if($('#price_container_'+product_id).css('display') == 'none'){
            $('#price_container_'+product_id).slideDown('slow');
        } else {
            $('#price_container_'+product_id).slideUp('slow');
        }
    } else {
        $('#price_container_'+product_id).html("<center><img src='"+root_url+"/img/loadingAnimation.gif' /></center>").slideDown('slow');

        //get price
        $.post(root_url+'/vendor_products/get_price/'+product_id, {}, function(data){
            $('#price_container_'+product_id).html(data).slideDown('slow');
        });
    }
}

function getPrice(root, controller, calling_obj_id, row_container, col_container){  
    if(row_container != ''){
        $('#'+row_container).hide('fast');
        $('#'+col_container).html("");

        if(calling_obj_id > 0){
            $('#'+col_container).html("<img src='"+root_url+"/img/loadingAnimation.gif' />");
            $('#'+row_container).fadeIn('slow');
            //alert(root+'/'+controller+'/getPrice/'+calling_obj_id);
            $.get(root_url+'/'+controller+'/getPrice/'+calling_obj_id, function(data){
                $('#'+col_container).html(data);
            });
            setTimeout("qty(root_url, '" + controller + "', '" + calling_obj_id + "', '" + row_container + "', 'div_qty')",1000);
        }
    } else {
        $('#'+col_container).hide('fast');
        $('#'+col_container).html("");
        if(calling_obj_id != ''){
            $('#'+col_container).html("<img src='"+root_url+"/img/loadingAnimation.gif' />");
            $('#'+col_container).fadeIn('slow');
            //alert(root+'/'+controller+'/getPrice/'+calling_obj_id);
            $.get(root_url+'/'+controller+'/getPrice/'+calling_obj_id, function(data){
                $('#'+col_container).html(data);
            });
            qty(root_url, controller, calling_obj_id, row_container, 'div_qty');
        }
    }
} 

function qty(root, controller, calling_obj_id, row_container, col_container)
{
	if(row_container != ''){
        $('#'+row_container).hide('fast');
        $('#'+col_container).html("");

        if(calling_obj_id > 0){
            $('#'+col_container).html("<img src='"+root_url+"/img/loadingAnimation.gif' />");
            $('#'+row_container).fadeIn('slow');
            $.get(root_url+'/'+controller+'/getQty/'+calling_obj_id, function(data){
                $('#'+col_container).html(data);
            });
            sleep(1000);
            setTimeout("getSubtotal(root_url, controller, 1, '" + row_container + "', 'sub_total')",1000);
        }
    } else {
        $('#'+col_container).hide('fast');
        $('#'+col_container).html("");
        if(calling_obj_id != ''){
            $('#'+col_container).html("<img src='"+root_url+"/img/loadingAnimation.gif' />");
            $('#'+col_container).fadeIn('slow');
            //alert(root+'/'+controller+'/getPrice/'+calling_obj_id);
            $.get(root_url+'/'+controller+'/getQty/'+calling_obj_id, function(data){
                $('#'+col_container).html(data);
            });
            sleep(1000);
            setTimeout("getSubtotal(root_url, '" + controller + "', 1, '" + row_container + "', 'subtotal')",1000);
        }
    }
}

function getSubtotal(root, controller, calling_obj_id, row_container, col_container){
    if(row_container != ''){
        $('#'+row_container).hide('fast');
        $('#'+col_container).html("");

        if(calling_obj_id > 0){
            $('#'+col_container).html("<img src='"+root_url+"/img/loadingAnimation.gif' />");
            $('#'+row_container).fadeIn('slow');

            $.get(root_url+'/'+controller+'/getSubtotal/'+calling_obj_id, function(data){
                $('#'+col_container).html(data);
            });
            sleep(1000);
            setTimeout("getGst(root_url, '" + controller + "', '" + calling_obj_id + "', '" + row_container + "', 'GST')", 1000);
        }
    } else {
        if(calling_obj_id != ''){
			$('#loadingidNew').html("Loading...");
            //$('#'+col_container).html("<img src='"+root_url+"/img/loadingAnimation.gif' />");
            $('#'+col_container).fadeIn('slow');
            $.get(root_url+'/'+controller+'/getSubtotal/'+calling_obj_id, function(data){
				$('#loadingidNew').html("");
                $('#'+col_container).html(data);
            });
            
           // setTimeout("getGst(root_url, '" + controller + "', '" + calling_obj_id + "', '" + row_container + "', 'GST')", 1000);
        }
    }
}

function getGst(root, controller, calling_obj_id, row_container, col_container){
    if(row_container != ''){
        $('#'+row_container).hide('fast');
        $('#'+col_container).html("");

        if(calling_obj_id > 0){
            $('#'+col_container).html("<img src='"+root_url+"/img/loadingAnimation.gif' />");
            $('#'+row_container).fadeIn('slow');
             $.get(root_url+'/'+controller+'/getGst/'+calling_obj_id, function(data){
                $('#'+col_container).html(data);
            });
            setTimeout("getGrand(root_url, '" + controller + "', '" + calling_obj_id + "', '" + row_container + "', 'grandtotal')", 2000);
        }
    } else {
        $('#'+col_container).hide('fast');
        $('#'+col_container).html("");
        if(calling_obj_id != ''){
            $('#'+col_container).html("<img src='"+root_url+"/img/loadingAnimation.gif' />");
            $('#'+col_container).fadeIn('slow');
            $.get(root_url+'/'+controller+'/getGst/'+calling_obj_id, function(data){
                $('#'+col_container).html(data);
            });
            setTimeout("getGrand(root_url, '" + controller + "', '" + calling_obj_id + "', '" + row_container + "', 'grandtotal')", 2000);
        }
    }
}

function getGrand(root, controller, calling_obj_id, row_container, col_container){
    if(row_container != ''){
        $('#'+row_container).hide('fast');
        $('#'+col_container).html("");

        if(calling_obj_id > 0){
            $('#'+col_container).html("<img src='"+root_url+"/img/loadingAnimation.gif' />");
            $('#'+row_container).fadeIn('slow');
            $.get(root_url+'/'+controller+'/getGrand/'+calling_obj_id, function(data){
                $('#'+col_container).html(data);
            });
        }
    } else {
        $('#'+col_container).hide('fast');
        $('#'+col_container).html("");
        if(calling_obj_id != ''){
            $('#'+col_container).html("<img src='"+root_url+"/img/loadingAnimation.gif' />");
            $('#'+col_container).fadeIn('slow');
            $.get(root_url+'/'+controller+'/getGrand/'+calling_obj_id, function(data){
                $('#'+col_container).html(data);
            });
        }
    }
}

function sleep(milliSeconds){
    var startTime = new Date().getTime(); // get the current time
    while (new Date().getTime() < startTime + milliSeconds); // hog cpu
}

function validate_required(field,alerttxt)
{
    with (field)
    {
        if (value==null||value=="")
        {
            alert(alerttxt);
            return false;
        }
        else
        {
            reWhiteSpace = new RegExp(/^\s+$/);
            if (reWhiteSpace.test(value)) {
                alert("Please Check Your Fields For Spaces");
                return false;
            }
        }
        return true;
    }
}

function validate_form(thisform)
{
    with (thisform)
    {
        if(validate_required(company,"Company name must be filled out!")==false)
        {
            company.focus();
            return false;
        }
        if(validate_required(fname,"First name must be filled out!")==false)
        {
            fname.focus();
            return false;
        }
        if(validate_required(lname,"Last name must be filled out!")==false)
        {
            lname.focus();
            return false;
        }
        if (validate_required(email,"Email must be filled out!")==false)
        {
            email.focus();
            return false;
        }else if(validate_email(email) == false){
            email.focus();
            return false;
        }
        if(validate_required(ph,"Telephone No. must be filled out!")==false)
        {
            ph.focus();
            return false;
        }else if(IsNumeric(ph.value)== false)
        {
            alert('Telephone No. must be \"0123456789-+\".');
            ph.focus();
            return false;
        }
        if(validate_required(address,"Mailing address must be filled out!")==false)
        {
            address.focus();
            return false;
        }

        if(validate_required(zip,"ZIP/POSTAL code must be filled out!")==false)
        {
            zip.focus();
            return false;
        }else if(IsNumeric(zip.value)== false)
        {
            alert('Zip code must be \"0123456789\".');
            zip.focus();
            return false;
        }

        if(validate_required(cty,"City name must be filled out!")==false)
        {
            cty.focus();
            return false;
        }

        if(validate_required(country,"Country name must be filled out!")==false)
        {
            country.focus();
            return false;
        }

        if(product_search.selectedIndex == 0)
        {
            if(product_search_model.selectedIndex == 0)
            {
                alert ( "Please select a Emsisoft product or. \n Please select a license of Emsisoft product." );
                product_search.focus();
                return false;
            }
        }else if(product_search_model.selectedIndex == 0)
        {
            alert ( "Please select a license." );
            product_search_model.focus();
            return false;
        }
        return true;
    }
}
function validate_form_livevault(thisform)
{
    with (thisform)
    {
        if(validate_required(company,"Company name must be filled out!")==false)
        {
            company.focus();
            return false;
        }
        if(validate_required(fname,"First name must be filled out!")==false)
        {
            fname.focus();
            return false;
        }
        if(validate_required(lname,"Last name must be filled out!")==false)
        {
            lname.focus();
            return false;
        }
        if (validate_required(email,"Email must be filled out!")==false)
        {
            email.focus();
            return false;
        }else if(validate_email(email) == false){
            email.focus();
            return false;
        }
        if(validate_required(ph,"Telephone No. must be filled out!")==false)
        {
            ph.focus();
            return false;
        }else if(IsNumeric(ph.value)== false)
        {
            alert('Telephone No. must be \"0123456789-+\".');
            ph.focus();
            return false;
        }
        if(validate_required(address,"Mailing address must be filled out!")==false)
        {
            address.focus();
            return false;
        }

        if(validate_required(zip,"ZIP/POSTAL code must be filled out!")==false)
        {
            zip.focus();
            return false;
        }else if(IsNumeric(zip.value)== false)
        {
            alert('Zip code must be \"0123456789\".');
            zip.focus();
            return false;
        }

        if(validate_required(cty,"City name must be filled out!")==false)
        {
            cty.focus();
            return false;
        }

        if(validate_required(country,"Country name must be filled out!")==false)
        {
            country.focus();
            return false;
        }
         if(validate_required(grandtotal,"Grandtotal cannot be empty")==false)
        {
            grandtotal.focus();
            return false;
        }
       return true;
    }
}
function IsNumeric(strString)   
{
    //  check for valid numeric strings
    var strValidChars = "0123456789";
    var strValidChars2 = "0123456789+-";
    var strChar;
    var blnResult = true;

    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length && blnResult == true; i++)
    {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1)
        {
            blnResult = false;
        }
    }

    if(blnResult == false)
    {
        var blnResult = true;
        //  test strString consists of valid characters listed above
        for (i = 0; i < strString.length && blnResult == true; i++)
        {
            strChar = strString.charAt(i);
            if (strValidChars2.indexOf(strChar) == -1)
            {
                blnResult = false;
            }
        }
    }
    return blnResult;
}



function validate_email(email) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    with(email)
    {
        var address = value;
        if(reg.test(address) == false) {
            alert('Invalid Email Address');
            return false;
        }
    }
   return true;
}
// End of Thu Ra's Creation. 2011-01-03 4:00PM
//for customer order and RMA
function view_details(pageurl, id, type) {
    //if details has already been loaded
    if($('#'+type+'_history_details_'+id).length > 0){
        //if it is displayed
        if($('#'+type+'_history_details_'+id).css('display') != 'none'){
            //hide it
            $('#'+type+'_history_details_'+id).slideUp('slow');
        } else {
            //hide other details of other order
            $('.'+type+'_history_details_row').slideUp('slow');

            //show content
            $('#'+type+'_history_details_'+id).slideDown('slow');
        }
    } else {
        //hide other details of other order
        $('.'+type+'_history_details_row').slideUp('slow');

        //create the content container first
        $('#'+type+'_history_'+id).after("\n\
        <tr class='"+type+"_history_details_row' style='background-color: #FFFFFF;' id='"+type+"_history_details_"+id+"'>\n\
            <td colspan='"+(type == 'order' ? '7':'9')+"'>\n\
                <div id='"+type+"_details_container_"+id+"'></div>\n\
            </td>\n\
        </tr>");

        //load content
        var params = {};
        if($('#'+type+'_details_container_'+id).length > 0){
            loadPiece(root_url+'/customer_orders/details/'+id+'/1/'+type, '#'+type+'_details_container_'+id, params);
        }
    }
}

//view shipment details of vendor
function view_shipment_details(id, type){
    //if details has already been loaded
    if($('#vendor_'+type+'_history_details_'+id).length > 0){
        //if it is displayed
        if($('#vendor_'+type+'_history_details_'+id).css('display') != 'none'){
            //hide it
            $('#vendor_'+type+'_history_details_'+id).slideUp('slow');
        } else {
            //hide other details of other order
            $('.vendor_'+type+'_history_details_row').slideUp('slow');

            //show content
            $('#vendor_'+type+'_history_details_'+id).slideDown('slow');
        }
    } else {
        //hide other details of other order
        $('.vendor_'+type+'_history_details_row').slideUp('slow');

        //create the content container first
        $('#vendor_'+type+'_history_'+id).after("\n\
        <tr class='vendor_"+type+"_history_details_row' style='background-color: #FFFFFF;' id='vendor_"+type+"_history_details_"+id+"'>\n\
            <td colspan='"+(type == 'order' ? '6':'9')+"'>\n\
                <div id='vendor_"+type+"_details_container_"+id+"'></div>\n\
            </td>\n\
        </tr>");

        //load content
        var params = {};
        if($('#vendor_'+type+'_details_container_'+id).length > 0){
            if(type == 'order'){
                loadPiece(root_url+'/shipment_tracking_orders/'+type+'_shipment_display/'+id+'/0', '#vendor_'+type+'_details_container_'+id, params);
            } else if(type == 'rma') {
                loadPiece(root_url+'/shipment_tracking_orders/'+type+'_shipment_display/'+id+'/vendor', '#vendor_'+type+'_details_container_'+id, params);
            }
        }
    }
}

function formSubmit(root, form_id, controller, act, selected, submited, selected_id, is_ajax, return_obj, return_obj_type){
   //if the page required to be submitted
   //alert(form_id);
   if(submited){
           if(is_ajax){
               //show loading
               tb_show_content();

               //get all data
               var inputs = $('#'+form_id+' :input');
				
               var values = {};
               inputs.each(function(){
                   //if input is either radio button or checkbox
                   if($(this).attr('type') == 'radio' || $(this).attr('type') == 'checkbox'){
                       //only get the selected one
                       if($(this).attr('checked')){
                           values[this.name] = $(this).val();
                       }
                   } else {
                       values[this.name] = $(this).val();
                   }
				   //alert('name: '+this.name+' '+'value: '+$(this).val());
               });
			   //alert(values);
               var url = '';
               if(selected_id > 0){
                   url = root+'/'+controller+'/'+act+'/'+selected_id;
               } else {
                   url = root+'/'+controller+'/'+act;
               }
				
               //post data
               $.post(url, values, function(data){
                   if(data.indexOf('class="error-message"') >= 0){
                       tb_show_content(data);
                   } else {
                       switch(controller){
                           case 'customer_orders':
                               loadPiece(root+"/"+controller+"/rma_index","#customer_rmas_container", {});
                               break;

                           default:		   		//alert(values);
                               //loadPiece(root+"/"+controller+"/display/"+return_obj+'/'+return_obj_type+'/'+controller+'_container',"#"+controller+"_container",values);
                               break;
                       }

                       tb_remove(); //tb_show_content(data);
                   }
               });
           } else {
               if(selected_id > 0){
                   $('#'+form_id).attr('action', root+'/'+controller+'/'+act+'/'+selected_id);
               } else {
                   $('#'+form_id).attr('action', root+'/'+controller+'/'+act);
               }
               $('#'+form_id).submit();
           }
   } else {
           $(window.location).attr('href', root+'/'+controller+'/'+act);		   
   }
}

function loadPiece(href,divName, params) {
    $(divName).html("<center style='margin: 2em;'><img src='"+root_url+"/img/loadingAnimation.gif' /></center>");

    if(params == undefined) var params = {};
    $.post(href, params, function(data){
        $(divName).html(data);

        //pagination links
        var divPaginationLinks = divName+" .paginator_contain a";
        $(divPaginationLinks).click(function() {
            var thisHref = $(this).attr("href");
            loadPiece(thisHref,divName, params);
            return false;
        });

        //pagination sorting links
        var divSortLinks = divName + " a.ajax_sort_header";
        $(divSortLinks).click(function() {
            var thisHref = $(this).attr("href");
            loadPiece(thisHref,divName, params);
            return false;
        });
    });
}
function RmaSubmitOrder() {
	$.ajax({
	  url: MAIN_URL+'customer_orders/rma_index/',
	  success: function(data) {
		$('#rma_details').html(data);
	  }
	});
}

function display_finder(){
    $('#product_finder_container').toggle('slow');

    if($('#bd_none').attr('class') == 'active'){
        $('#bd_none').attr('class', '');
    } else {
        $('#bd_none').attr('class', 'active');
    }
}

function product_sku_search(){
    $('#product_search_brand').val(0);
    $('#product_search_model').val(0);
    $('#product_search_category').val(0);
	$('#ProductIndexForm').attr("target","_self");
    $('#product_search').closest('form').submit();
}
function product_sku_search1(searchval){
    $('#product_search_brand').val(0);
    $('#product_search_model').val(0);
    $('#product_search_category').val(0);
	$('#product_search').val(searchval);
	$('#ProductIndexForm').attr("target","_blank");
    $('#product_search').closest('form').submit();
}

function search_product(id, type){
    if(id > 0 && type !=''){
        if($('#product_search_'+type).length > 0){
            $('#product_search_'+type).val(id);

            if(type == 'brand'){
                $('#product_search_model').val(0);
                $('#product_search_category').val(0);
            } else if(type == 'model'){
                $('#product_search_category').val(0);
            }

            $('#product_search').val('');

            $('#product_search_'+type).closest('form').submit();
        }
    }
}

function search_product_tag(searchval) {
            $('#product_search').val(searchval);
			$('#product_search').closest('form').submit();
}

function get_sub(id, parent_type, target_controller){
    if($('#'+parent_type+'_search_'+id+'_container').length > 0){
        if($('#'+parent_type+'_search_'+id+'_container').css('display') == 'none'){
            $('.'+parent_type+'_search_class').slideUp('slow');
            $('#'+parent_type+'_search_'+id+'_container').toggle('slow');

            $('#product_search_'+parent_type).val(id);
            if(parent_type == 'brand'){
                $('#product_search_model').val(0);
                $('#product_search_category').val(0);
            } else if(parent_type == 'category'){
                $('#product_search_category').val(0);
            }
        } else {
            $('#'+parent_type+'_search_'+id+'_container').slideUp('slow');

            $('#product_search_'+parent_type).val(0);
            if(parent_type == 'brand'){
                $('#product_search_model').val(0);
                $('#product_search_category').val(0);
            } else if(parent_type == 'category'){
                $('#product_search_category').val(0);
            }
        }

        if($('#'+parent_type+'_sub_'+id).attr('class') == 'sub_closer'){
            $('#'+parent_type+'_sub_'+id).attr('class', 'sub_opener');
        } else {
            $('#'+parent_type+'_sub_'+id).attr('class', 'sub_closer');
        }
    } else {
        $('.'+parent_type+'_search_class').slideUp('slow');

        $('#'+parent_type+'_sub_'+id).attr('class', 'sub_closer');

        $('#'+parent_type+'_sub_'+id).after("\n\
        <ul id='"+parent_type+"_search_"+id+"_container' style='display: none; text-align: center;'>\n\
            <li style='height: 30px;'><img src='"+root_url+"/img/loading.gif' style='margin-top: 8px;' /></li>\n\
        </ul>\n\
        ");

        $('#product_search_'+parent_type).val(id);
        if(parent_type == 'brand'){
            $('#product_search_model').val(0);
            $('#product_search_category').val(0);
        } else if(parent_type == 'category'){
            $('#product_search_category').val(0);
        }

        $("#"+parent_type+"_search_"+id+"_container").slideDown('slow');

        $.post(root_url+'/'+target_controller+'/get_search', {'id': id, 'type': parent_type}, function(data){
            $("#"+parent_type+"_search_"+id+"_container").remove();
            $('#'+parent_type+'_sub_'+id).after(data);
            $('#'+parent_type+'_search_'+id+'_container').slideDown('fast');
        });
    }
}
function getPriceVault(value,col_container){  
        //$('#'+col_container).hide('fast');
        $('#loadingid').html("Loading...");
        $('#'+col_container).fadeIn('slow');
		$.get(root_url+'/products/get_vault_price/'+value, function(data){
			$('#'+col_container).html(data);
		});
} 
