//------------------------------------------------------------------------------------
// $Workfile: incSupplierHelper.js $
// Purpose: This file contains javascript functions that support various validations 
//          in the Supplier Tool.
//------------------------------------------------------------------------------------
// $Revision: 1.3 $
// $Date: 2009/10/30 15:11:09EDT $
// $Author: DANIEL CARTER (CK3X) $
//------------------------------------------------------------------------------------

function isNumeric(sNumber) 
{
	//return true
	return sNumber == sNumber.replace(/[^\d]*/gi,"");
}

//-------------------------------------------------------------------------------------

function validateCharacters(sString)
{
	return sString.replace(/[^%]*/gi,"");
}

//-------------------------------------------------------------------------------------

function checkUnderScore(sString)
{
	return sString.replace(/[^_]*/gi,"");
}

//-------------------------------------------------------------------------------------

function validateZipCodeSearch(sLanguage)
{
	var sZipCode, sErrMsg;
	var sErrMsg_MissingZip, sErrMsg_InvalidZip;
	
	sErrMsg = "";

	switch (sLanguage.toUpperCase()) {
		case "SPANISH":
			sErrMsg_MissingZip = "Por favor escriba el código postal en el espacio\n"
			sErrMsg_InvalidZip = "Por favor, escriba un valor numérico de 5 dígitos en el espacio de Código de Área en.\n"
			break;

		default:
			sErrMsg_MissingZip = "Please enter a zip code in the Zip Code field.\n"
			sErrMsg_InvalidZip = "Please enter a 5-digit numeric value in the Zip Code field.\n"
			break;
	}

	sZipCode = document.frmZipCodeSearch.ZipCode;

	if (sZipCode != 'undefined') {
		if (sZipCode.value.length == 0)
			sErrMsg = sErrMsg_MissingZip;
		else if (isNaN(sZipCode.value))
			sErrMsg = sErrMsg_InvalidZip;
		else if (sZipCode.value.length < 5) 
			sErrMsg = sErrMsg_InvalidZip;
		else
		{
			sZipCode = document.frmZipCodeSearch.ZipCode.value;
			for (i=0; i < sZipCode.length; i++)
			{
				digit = sZipCode.substring(i, i+1);
				if (!((parseInt(digit) >= 0) && (parseInt(digit) <= 9)))  
				{
					sErrMsg = sErrMsg_InvalidZip;
				}
			}
		}
	}
	

	if (sErrMsg.length > 0)
	{
		alert(sErrMsg);
		return false;
	}
	else
	{
		return true;
	}		

}

//-------------------------------------------------------------------------------------

function ValidateCheckBoxes(sLanguage)
{
	var sErrMsg;
	var i;
	var sErrMsg_NoCheckBox, sErrMsg_TooManyCheckBox;
	var nProductSelected;
	
	sErrMsg = "";
	
	switch (sLanguage.toUpperCase()) {
		case "SPANISH":
			sErrMsg_NoCheckBox = "ESP_Please select at least one product category.\n"
			sErrMsg_TooManyCheckBox = "ESP_Please select up to 5 categories from the list.\n"
			break;
		default:
			sErrMsg_NoCheckBox = "Please select at least one product category.\n"
			sErrMsg_TooManyCheckBox = "Please select up to 5 categories from the list.\n"
			break;
	}
	
	nProductSelected = 0;
	for (i = 0;  i < document.frmChooseCategory.SupplyType.length;  i++)
	{
		if (document.frmChooseCategory.SupplyType[i].checked)
		{
			nProductSelected = nProductSelected + 1;
		}
	}

	if (nProductSelected == 0) {
		sErrMsg = sErrMsg_NoCheckBox;
	} else if (nProductSelected > 5) {
		sErrMsg = "You selected " + nProductSelected + " product categories.\n" + sErrMsg_TooManyCheckBox ;
	}
		
	
	if (sErrMsg.length > 0)
	{
		alert(sErrMsg);
		return false;
	}
	else
	{
		return true;
	}		
}

//-------------------------------------------------------------------------------------

//This method is called in Step 3 to create the Select All/Select None toggle link
function ResetCheckBoxes()
{
	var i;

	for(i=0;i<document.frmChooseCategory.SupplyType.length;i++) 
	{
		document.frmChooseCategory.SupplyType.item(i).checked = false;
	}
}

//-------------------------------------------------------------------------------------

function ValidateDistance(sLanguage)
{
	var sErrMsg;
	
	sErrMsg = "";

	switch (sLanguage.toUpperCase()) {
		case "SPANISH":
			sErrMsg_MissingDistance = "Por favor seleccione la distancia de la caja de abajo.\n"
			break;
		default:
			sErrMsg_MissingDistance = "Please select a distance from the Distance drop down box.\n"
			break;
	}
	
	if (document.frmProximitySearch.Distance.value.length <= 0)
	{
		sErrMsg = sErrMsg_MissingDistance;
	}

	if (sErrMsg.length > 0)
	{
		alert(sErrMsg);
		return false;
	}
	else
	{
		return true;
	}		
}

//-------------------------------------------------------------------------------------

function ValidateSortDropDown(sLanguage, sSelectedValue) {

	var sErrMsg_MissingSelection;
		
	switch (sLanguage.toUpperCase())
	{
		case 'SPANISH':
			sErrMsg_MissingSelection = 'ESP Please select an option in the dropdown to sort the search results.';
			break;
		default:
			sErrMsg_MissingSelection = 'Please select an option in the dropdown to sort the search results.';
			break;
	}
		
	if (sSelectedValue == 0)
	{
		alert(sErrMsg_MissingSelection); 
		return false;
			
	} else {
		return true;
	} 
}

//-------------------------------------------------------------------------------------

function submitViewAllSuppliers(vsSearchType, vsSearchValue) {

	document.frmViewSuppliers.SearchType.value = vsSearchType;
	document.frmViewSuppliers.SearchValue.value = vsSearchValue;
	document.frmViewSuppliers.submit();
}

//-------------------------------------------------------------------------------------

// This function changes the values of the div state (expanded/collapsed) params in a form that is passed in.
function changeDivParams(vsForm, vsCurrSupplyType)
{
	var objForm = document.getElementById(vsForm);
	var i;	
	var nProductID
	var sExpandProductList = "";
		
	for (i=0; i<objForm.SupplyType.length; i++) {
			
		nProductID = (objForm.SupplyType[i].value.split("|", 1))[0];
	

		if (document.getElementById("Prod_" + nProductID + "_divId") != null) {
			if (document.getElementById("Prod_" + nProductID + "_divId").className == 'opened') {
				sExpandProductList = sExpandProductList + nProductID + "|";
			} 
		}		
	}

	//remove the last |
	if (sExpandProductList.length > 0) {
		sExpandProductList = sExpandProductList.substr(0,sExpandProductList.length - 1);
	}
	
	if (objForm.SupplierExpandList != null) {
		objForm.SupplierExpandList.value = sExpandProductList;
	}

}

//-------------------------------------------------------------------------------------

