var url_customerLookup 		= "customerLookup.aspx";
var url_newCustomer    		= "addCustomer.aspx";
var url_ajaxInterface       = "ajaxInterface.aspx";
var url_idManagement        = "customerIDManagement.aspx";

/**
 *
 * States Lookup Component
 *
 **/
function populateStates(country, formItemName)
{
    var formItem = getElem(formItemName);
    
    var obj = new AjaxObject(url_ajaxInterface);
    obj.WebURL.AddParam("request", "statesLookup");
    obj.WebURL.AddParam("country", country);
    
    RemoveOptions(formItem);
	AddBlankToList(formItem);
	
	obj.Open();
	if (obj.HasResults())
	{
	    var state = null;
	    state = obj.ResultSet.NextItem();
	    while (null != (state = obj.ResultSet.NextItem()))
		    AppendOption(formItem, state.state, state.code);
	}
	
	obj.Close();
}

/**
 *
 * Customer Lookup Component
 *
 **/
function customerLookup(formName)
{
	if (formName == null || formName.length == 0)
		return;		

	var ajax = new AjaxObject(url_ajaxInterface);
	
    ajax.WebURL.AddParam("request", "customerLookup");
	ajax.WebURL.AddParam("firstName", getElemValue(formName+"_FirstName"));
	ajax.WebURL.AddParam("lastName1", getElemValue(formName+"_LastName1"));
	ajax.WebURL.AddParam("lastName2", getElemValue(formName+"_LastName2"));
	ajax.WebURL.AddParam("phone", getElemValue(formName+"_Phone"));
	ajax.WebURL.AddParam("country", getElemValue(formName+"_Country"));

	ajax.Callback = function()
	{
		hideElem("customerLookup_waitSection_"+formName);
		if (ajax.HasResults())
		{
			hideElem("customerLookup_noCustomers_"+formName);
			
			var parentItem = getElem('customerLookup_searchSection_'+formName);
			var grid = showDataGrid(formName+'_dataGrid', parentItem, ajax, new Array('firstName', 'lastName1', 'lastName2', 'address', 'telephoneNumber'), new Array('First Name', 'Last Name', 'Mothers Maiden Name', 'Address', 'Telephone'));
			grid.OnItemSelected = function(gridObj, data, row)
			{
				setCustomerInfo(formName, data, false);
				hideElem("customerLookup_noCustomers_"+formName);
				gridObj.Close();
			}
			grid.OnGridClosed = function(gridObj, canceled)
			{
				if (canceled)
				{
					showElem("customerLookup_searchSection_"+formName);
					hideElem("customerLookup_noCustomers_"+formName);
				}
			}
		}
		else
		{
			showElem("customerLookup_noCustomers_"+formName);
			showElem("customerLookup_searchSection_"+formName);
		}
		ajax.Close();
	}
	
	showElem("customerLookup_waitSection_"+formName);
	hideElem("customerLookup_searchSection_"+formName);
	hideElem("customerLookup_noCustomers_"+formName);
	ajax.Open();
}

function clearCustomerInfo(formName)
{
    setElemValue(formName, '');
    hideElem("customerLookup_customerSection_"+formName);
    showElem("customerLookup_searchSection_"+formName);
    
    setElemText("customerLookup_customerSection_name_"+formName, "");
    setElemText("customerLookup_customerSection_address1_"+formName, "");
    setElemText("customerLookup_customerSection_address2_"+formName, "");
    
	if (null != window.onCustomerCleared)
	    window.onCustomerCleared(formName);
}

function getCustomerInfo(customerId)
{
    if (IsStringEmpty(customerId))
        return null;

	var ajax = new AjaxObject(url_ajaxInterface);
	
    ajax.WebURL.AddParam("request", "customerDetails");
	ajax.WebURL.AddParam("customerId", customerId);
	
    var customerInfo   = null;
    
    ajax.Open();
	
	if (ajax.HasResults())
	{
        customerInfo = ajax.ResultSet.GetFirstResult();
    }

    ajax.Close();
    
    return customerInfo;
}

function initializeCustomerInfo(formName)
{ 
    var cid = getElemValue(formName);
    if (IsStringEmpty(cid))
        return;

    setCustomerInfo(formName, getCustomerInfo(cid), false);
}

function setCustomerInfo(formName, customerInfo)
{
	if (null == customerInfo)
		return;
		
	setElemValue(formName, customerInfo.id);
	
	/* Set the display values
	 */
    showElem("customerLookup_customerSection_"+formName);
    hideElem("customerLookup_searchSection_"+formName);
    setElemText("customerLookup_customerSection_name_"+formName, customerInfo.name);
    setElemText("customerLookup_customerSection_address1_"+formName, customerInfo.address);
    setElemText("customerLookup_customerSection_address2_"+formName, customerInfo.city + " " + customerInfo.state + " " + customerInfo.country);
    setElemText("customerLookup_customerSection_phone_"+formName, formatPhoneNumber(customerInfo.telephoneNumber,customerInfo.country) );
}

function addNewCustomer(formItem)
{
	if (formItem == null || formItem.length == 0)
		return;		

	var url = new AjaxURL(url_newCustomer);
	
	url.AddParam("parentFormItemName", formItem);
	url.AddRandom();
					
	hideElem("customerLookup_noCustomers_"+formItem);
		
	showPopup(url.FullURL, "NewCustomer", 670);
}

function editCustomer(formItem)
{
	if (formItem == null || formItem.length == 0)
		return;		

	var url = new AjaxURL(url_newCustomer);
	
	url.AddParam("parentFormItemName", formItem);
	url.AddParam("customerId", getElemValue(formItem));
	url.AddRandom();
		
	showPopup(url.FullURL, "NewCustomer", 670);
}

function idManagement(formItem)
{
	if (formItem == null || formItem.length == 0)
		return;		

	var url = new AjaxURL(url_idManagement);
	
	url.AddParam("parentFormItemName", formItem);
	url.AddParam("customerId", getElemValue(formItem));
	
    showPopup(url.FullURL, "IDManagement", 670);
}

function clearIdInfo(formItem, isPopup)
{
    setElemValue(formItem+"_idSelected", "", isPopup);
	setElemText("customerLookup_customerSection_idType", "", isPopup);
    setElemText("customerLookup_customerSection_idNumber", "", isPopup);
}

function setIdInfo(formItem, idInfo, isPopup)
{
    setElemValue(formItem+"_idSelected", idInfo.id, isPopup);
	if (isPopup)
	{
	    closePopup();
	    this.close();
	}
	
	/* Set the display values
	 */
    setElemText("customerLookup_customerSection_idType", idInfo.idType, isPopup);
    setElemText("customerLookup_customerSection_idNumber", idInfo.idNumber, isPopup);
    
	if (true == isPopup && null != window.opener.onCustomerIdSelected)
	    window.opener.onCustomerIdSelected(formItem, idInfo);
    else if (false == isPopup && null != window.onCustomerIdSelected)
	    window.onCustomerIdSelected(formItem, idInfo);
}
