$file = "code/rma.aspx";
initPage();

function loadPage()
{	
	$(".contentmargin").css("margin","10px 10px 40px 20px");
	
	$customermode = true;

	$("#btnSearch").click(function(){
		search(userID);
	});

	if($customermode)
	{
		$("#rowCustomerLine").hide();
		$("#btnSearch").removeAttr("disabled");
		search(userID);
	}
	else
	{
		loadCustomers();
	}
}

function loadCustomers()
{
    $("#cboCustID").html("<option>Loading customers. Please wait...</option>");
    loadProgress("Initialising application please wait...");
	$("#btnSearch").attr("disabled","1");
	
	$.post($file, "cmd=GET_CUSTOMERS", function(data) {
	    if (data.search(/\<option/) >= 0) {
	        $("#cboCustID").html("<option value=''>All</option>");
	        $("#cboCustID").append(data);
	        $("#gridcontainer").html("Select customer and SROID to begin your search...");
	        $("#btnSearch").removeAttr("disabled");
	        //console.log(data);
	    }
	    else {
	        $("#gridcontainer").html(data);
	        //console.log("hey");
	    }
	});
}

function search(userID,custID,sroID,serialNo)
{
	loadProgress("Searching your RMA entries. Please wait...");

	custID = custID ? custID : $("#cboCustID").val();
	custID = custID == null ? custID = "" : custID;
	sroID = sroID ? sroID : $("#txtSROID").val();
	sroID = sroID == null ? sroID = "" : sroID;
	serialNo = serialNo ? serialNo : $("#txtSerialNo").val();
	serialNo = serialNo == null ? serialNo = "" : serialNo;
	postdata = "cmd=SEARCH&custid=" + custID + "&sroid=" + sroID + "&sn=" + serialNo + uidQS;
	
	if(userID || custID || sroID || serialNo)
	{
		//console.log(postdata);
		$.post($file,postdata,function(data){
			if(data.search(/\<table/)>=0)
			{
				loadContent(data);
			}
			else
			{
				loadContent(data);
			}
		});
	}
	else
	{
		loadProgress("Something went wrong...");
	}
}

function loadProgress(msg)
{
    html = "<img src=\"media/images/ajax/ajax-loader-red.gif\" align=\"absmiddle\" style=\"margin-right:4px\">" + msg;
    $("#gridcontainer").html(html);
}

function loadContent(content) {
    $("#gridcontainer").html(content);
}