// variables for use 
var ReturnTo;

function formatHref(href)
{
	if(href != null && href != "")
	{
		var lastPos = href.indexOf("?");
		if(lastPos > 0)
		{
			href = href.substr(0,lastPos);
		}
	}
	return href;
}

//Popup a picture of the item.
function openImageWin(imgPath)
{
	var winOptions = "location=no, width=520, height=420, toolbars=no, status=no,resizable=yes,menubar=no, scrollbars=yes";
	var winName = "imageWindow";
	var winLocation = imgPath;
	
	window.open(winLocation, winName, winOptions);
}

function addVal(curVal, newVal)
{
	var retValue;
	
	if(curVal == '')
	{
		retValue = newVal;
	}
	else
	{
		retValue += ',' + newVal;
	}
	
	return retValue;
}

function request()
{
	document.forms[0].submit();
}

function freshenPage()
{
    var loc = document.location.href;


    if(loc.indexOf("retValue=1") < 0)
    {
            document.location.href = document.location.href + "?retValue=1";
    }
}
function AcceptCalendarDate(CalendarDate)
{
	document.forms[0].elements[ReturnDateTo].value = CalendarDate;
}
function PositionCalendar()
{
	if (document.layers)
	{
		openTop = window.innerHeight / 2;
		openLeft = window.innerWidth / 2;
	}
	else
	{
		openTop = window.screen.availHeight / 2;
		openLeft = window.screen.availWidth / 2;
	}
}
function openPopUpCalendar(sReturnDateTo)
{
	var sProp = "";

	PositionCalendar();
	ReturnDateTo = sReturnDateTo;
	sProp = 'height=240,width=268,status=no,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,maximize=no,top=' + openTop + ',left=' + openLeft ;
	popUpWindow = window.open("Calendar.aspx", "Calendar", sProp );	

}

function handleEnterCheckOrder (field, event) 
{
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) 
	{
		checkStatus();
	} 
	else
	return true;
}

//This is for the canceling of the form submit
//	if the search button and the 'search by part number' controls are 
//	on the same page.
var searchSubmit = false;

function handleEnter (field, event) 
{
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) 
	{
		searchSubmit = true;
		searchProducts();
	} 
	else
	{
		return true;
	}
}  

//If a form contains both the left nav (with the search button)
//	and the 'Add part number' control, this method needs to be
//	attached to the form.onSubmit method.
//	Example:
//	<form id="Form1" method="post" onsubmit="return handleSubmit(this, event);" runat="server">
//	Also note: 
//	It has to say 'return handleSubmit...' to work properly.
function handleSubmit(frm, event)
{
	if(searchSubmit == true)
	{
		searchSubmit = false;
		return false;
	}
	else
	{
		return true;
	}
}

function handleEnterAgain (field, event) 
{
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) 
	{
		searchAgain();
	} 
	else
	return true;
}
function processFormElements(formId, elementNames, requiredElementNames)
{
	//debugger;
	var curElement;
	var post = true;
	
	for(i = 0; i < document.forms[0].elements.length; i++)
	{
		curElement = document.forms[0].elements[i];
		if(curElement.id == null || curElement.id === undefined || curElement.id == "")
		{
			//Do not process
		}
		else
		{
			if(elementNames.indexOf(curElement.id) >= 0)
			{
				elementNames = elementNames.replace(curElement.id, curElement.id + '=' + curElement.value);
				if(requiredElementNames.indexOf(curElement.id)>=0)
				{
					if(curElement.options != null)//Is a select box
					{
						if(curElement.options.length > 1)
						{
							if(curElement.options[curElement.selectedIndex].value == "")
							{
								post = false;
								break;
							}
							else
							{
								curElement.value = replaceInvalidInput(curElement.options[curElement.selectedIndex].value);
							}
						}
					}
					else	//Is NOT a select box
					{
						if(curElement.value == "")
						{
							post = false;
							break;
						}
						else
						{
							curElement.value = replaceInvalidInput(curElement.value);
						}
					}
				}
			}
		}
	}
	//debugger;
	if(post == true)
	{
		/*
		var curPage = document.location.href;
		if(curPage.indexOf("formData") >= 0)
		{
			curPage = curPage.substring(0, curPage.indexOf("formData") - 1);
		}
		document.location.href = "./processForm.aspx?formId=" + formId + "&formData=" + elementNames + "&curPage=" + curPage;
		*/
		document.forms[0].hdnFormId.value = formId;
		document.forms[0].hdnFormData.value = elementNames;
	}
	else
	{
		//alert("The '" + curElement.id + "' field is required and has no value.  Please fill this field in to continue.");
		var lblErrMsg = document.getElementById('lblError');
        if (lblErrMsg != null)
        {
            var hdnField = document.forms[0].elements['hdn' + curElement.id];
			var text = "";
			if(hdnField != null)
			{
				text = hdnField.value;
			}
			else
			{
				text = curElement.id;
			}
			lblErrMsg.innerHTML = "Errors found:<br />"
				+ "The '" + text + "' field is required and has no value.  "
				+ "Please fill this field in to continue.";
			return false;
        }
	}
}

function processFormElementsM(formId, elementNames, requiredElementNames, pageName)
{
	var curElement;
	var post = true;
	
	for(i = 0; i < document.forms[0].elements.length; i++)
	{
		curElement = document.forms[0].elements[i];
		if(curElement.id == null || curElement.id === undefined || curElement.id == "")
		{
			//Do not process
		}
		else
		{
			if(elementNames.indexOf(curElement.id) >= 0)
			{
				elementNames = elementNames.replace(curElement.id, curElement.id + '=' + curElement.value);
				if(requiredElementNames.indexOf(curElement.id)>=0)
				{
					if(curElement.items != null)//Is a select box
					{
						if(curElement.items.length > 1)
						{
							if(curElement.items[curElement.selectedIndex].value == "")
							{
								post = false;
								break;
							}
							else
							{
								curElement.value = replaceInvalidInput(curElement.items[curElement.selectedIndex].value);
							}
						}
					}
					else	//Is NOT a select box
					{
						if(curElement.value == "")
						{
							post = false;
							break;
						}
						else
						{
							curElement.value = replaceInvalidInput(curElement.value);
						}
					}
				}
			}
		}
	}
	if(post == true)
	{
		//document.location.href = "./processForm.aspx?formId=" + formId + "&formData=" + elementNames + "&pageName=" + pageName;
		document.forms[0].hdnFormId.value = formId;
		document.forms[0].hdnFormData.value = elementNames;
	}
	else
	{
		//alert("The '" + curElement.id + "' field is required and has no value.  Please fill this field in to continue.");
		var lblErrMsg = document.getElementById('lblError');
        if (lblErrMsg != null)
        {
            var hdnField = document.forms[0].elements['hdn' + curElement.id];
			var text = "";
			if(hdnField != null)
			{
				text = hdnField.value;
			}
			else
			{
				text = curElement.id;
			}
			lblErrMsg.innerHTML = "Errors found:<br />"
				+ "The '" + text + "' field is required and has no value.  "
				+ "Please fill this field in to continue.";
        }
	}
}

function goback()
{
	//alert("Pause...");

	if (window.opener != null) 
	{ 
		window.opener.acceptEditWin(document.getElementById(actEditor).innerHTML);
	}
	window.close();
}

function returnVal(sValue)
{
	//alert("Pause...");

	if (window.opener != null) 
	{ 
		window.opener.acceptEditWin(sValue);
	}
	window.close();
}

function acceptEditWin(sValue)
{
	for(i = 0; i < document.forms[0].elements.length; i++)
	{
		var curElement = document.forms[0].elements[i];
		if(curElement.id == null || curElement.id === undefined || curElement.id == "")
		{
			//Do nothing
		}
		else
		{
			if(curElement.name.indexOf(ReturnTo) >= 0)
			{
				document.forms[0].elements[curElement.id].value = sValue;
			}
		}
	}	
}
function replaceInvalidInput(input)
{
	var expr = new RegExp(/[^0-9a-zA-Z &@\.\(\)\[\]]/);
	input = input.replace(expr, "");
	
	return input;
}

function changeSelect()
{
	var fields = document.forms[0].elements;
	var postData = "";
	for(i = 0; i < fields.length; i++)
	{
		var name = fields[i].id;
		
		if(name == "")
		{
			name = fields[i].name;
		}
		
		var val = fields[i].value;
		
		
		val = escape(val);		
		
		if(name != null && name != "" && name != "__VIEWSTATE" && name.indexOf("hdn" < 0))
		{
			if(fields[i].type == "checkbox" || fields[i].type == "radio")
			{
				if(fields[i].checked)
				{
					if(postData.indexOf(name) >= 0)
					{
						postData += "," + val;
					}	
					else
					{				
						postData += (postData.length > 0 ? "|" : "") + name + "=" + val;			
					}
				}
			}
			else
			{
				postData += (postData.length > 0 ? "|" : "") + name + "=" + val;			
			}
		}
	}
	
	var curLocation = document.location.href;
	if(curLocation.indexOf("?") >= 0)
	{
		var pos = curLocation.indexOf("?");
		curLocation = curLocation.substring(0, curLocation);
	}
	
	var action = document.forms[0].action;
	if(action.indexOf("formData") >= 0)
	{
		action = action.substring(0, action.indexOf("formData"));
	}
	if(action != null && action != "")
	{
		var start = action.indexOf("?");
		if(start > 0)
		{
			//We have a querystring
			action = action.substring(start, action.length)
		}
		else
		{
			action = "";
		}
	}
	curLocation += action + (action != null && action != "" && action.indexOf("?") >= 0 ? "&" : "?") + "formData=" + postData;
	
	document.location.href = curLocation;
}

function trim(strText)
{ 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1, strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
}

function toggleCheckboxes(checkedState, idPattern)
{
	// Check or uncheck a series of checkboxes having IDs matching a specified pattern
	for(i = 0; i < document.forms[0].elements.length; i++)
	{
		var curElement = document.forms[0].elements[i];
		if(curElement.id.indexOf(idPattern) >= 0)
		{
			// This is one of the checkboxes to toggle
			curElement.checked = checkedState;
		}
	}
}

