/*
 * *** IMPORTANT!!! ***
 *
 *  1. Add any new code to the appropriate section.
 *  2. Use existing code if possible.
 *  3. Fix buggy code rather than creating your own new function.
 *  4. Use the same 
 */

/**************************************************** String Manipulation **/
function escapeVals(textarea,replaceCarrReturnWith, replaceSingleQuoteWith, replaceAmpersandWith)
{
	textarea=escape(textarea);
	while(textarea.indexOf("%0D%0A") > -1 || textarea.indexOf("%27") > -1 || textarea.indexOf("%26") > -1)
	{
		textarea=textarea.replace("%0D%0A",replaceCarrReturnWith);
		textarea=textarea.replace("%27",replaceSingleQuoteWith);
		textarea=textarea.replace("%26",replaceAmpersandWith);
	}
	textarea=unescape(textarea);
	return textarea;
}
function unescapeVals(textarea, replaceCarrReturnVal, replaceSingleQuoteVal, replaceAmpersandVal)
{
	textarea=escape(textarea);
	while(textarea.indexOf(replaceCarrReturnVal) > -1 || textarea.indexOf(replaceSingleQuoteVal) > -1 || textarea.indexOf(replaceAmpersandVal) > -1)
	{
		textarea=textarea.replace(replaceCarrReturnVal,"%0D%0A");
		textarea=textarea.replace(replaceSingleQuoteVal,"%27");
		textarea=textarea.replace(replaceAmpersandVal,"%26");
	}
	textarea=unescape(textarea);
	return textarea;
}


String.prototype.trim = function()
{
	var str = this;
	var iStart = 0, iEnd = str.length - 1;

	while (iStart <= iEnd && str.charAt(iStart) == ' ')
		iStart++;

	while (iEnd >= iStart && str.charAt(iEnd) == ' ')
		iEnd--;

	return str.substring(iStart, iEnd + 1);
}

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function DisableButtonAfterClick(button)
{
	button.disabled = true;
}
function EnableButtonAfterClick(button)
{
	button.disabled = false;
}

/**
 * Trims any leading or trailing spaces from the string.
 *
 * @param  str  String to trim
 * @returns     Trimmed string.
 */
function TrimString(str)
{
	return str.trim();
}

/****************************************************** String Validation **/

function IsLetter(c)
{
	return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}

function IsDigit(c)
{
   return c >= '0' && c <= '9';
}

function IsPunctuation(c)
{
	var punctuation = ' \'\",:;-?()&!£$%=+#';

	c = c.charAt(0); // just in case a string is passed in.
	return punctuation.indexOf(c, 0) != -1;
}

function IsNumber(qty)
{
	return /^-?\d+$/.test(qty.trim());
	
}

function IsRealNumber(qty)
{
	return /^[-+]?\d+(\.\d+)?$/.test(qty.trim());
}
function IsPositiveRealNumber(qty)
{
	return /^[+]?\d+(\.\d+)?$/.test(qty.trim());
}
function IsMoney(qty)
{
	return(parseFloat(qty,10)==(qty*1));
}
function IsPositiveWholeNumber(qty)
{
	var return_value = /^\d+$/.test(qty.trim());
	if (!(return_value))
		return false;
		
	if (parseInt(qty) < 1)
		return false;
	
	return true;
}
function IsPositiveWholeNumberOrZero(qty)
{
	var return_value = /^\d+$/.test(qty.trim());
	if (!(return_value))
		return false;
		
	if (parseInt(qty) < 0)
		return false;
	
	return true;
}

/**
 * Verifys whether an email address is valid.
 */
function IsValidEmail(email)
{
	return email.indexOf("@") != -1 && email.lastIndexOf(".") >= email.indexOf("@");
}

function formatAsMoney(mnt)
{
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}
/************************************************************ UI Handling **/

/**
 * Sets the input focus to the given object if it exists.
 *
 * @param  elem  Object to give focus to.
 */
function FocusIfExists(elem)
{
	if (elem != null && elem.type != 'hidden')
		elem.focus();
}

/**
 * For query listings, causes the form to submit with an empty string.
 */
function ViewAll()
{
	document.frm.query.value = '';
	document.frm.submit();
}

/**
 * Sets the checkboxes with the given name on or off.
 *
 * @param  frm    Form object containing the checkboxes.
 * @param  group  Name of the checkbox group.
 * @param  val    True to set them on, otherwise false.
 */
function CheckGroup(frm, group, val)
{
	var elem;

	for (var i = 0; i < frm.elements.length; i++)
	{
		elem = frm.elements[i];

		if (elem.name == group && !elem.disabled)
			elem.checked = val;
	}
}

/**
 * Sets the checkboxes with the given prefix on or off.
 *
 * @param  frm    Form object containing the checkboxes.
 * @param  group  Prefix of the checkbox group.
 * @param  val    True to set them on, otherwise false.
 */
function CheckPrefixedGroup(frm, group, val)
{
	var elem;

	for (var i = 0; i < frm.elements.length; i++)
	{
		elem = frm.elements[i];

		if (elem.name.substr(0, group.length) == group && !elem.disabled)
			elem.checked = val;
	}
}

/**
 * Sets the value of a form object, if it exists.
 *
 * @param  elem  Object to set.
 * @param  val   Value to give to it.
 */
function SetIfExists(elem, val)
{
	if (elem != null)
		elem.value = val;
}

/**
 * Causes the page's form to submit.
 *
 * @param  action  Page to submit to (optional).
 */
function SubmitForm()
{
	// If an action is specified
	if (arguments.length == 1)
		document.frm.action = arguments[0];

	document.frm.submit();
}

/******************************************************** Form Validation **/

/**
 * Checks if the given form element has a value.
 *
 * @param  elem  Form element to check.
 * @return       True if has a value, otherwise false.
 *
 * @note  This function strips any spaces in the element's value before
 *        checking.
 */
function HasValue(elem)
{
	elem.value = elem.value.trim();
	return elem.value != "";
}

/**
 * Checks to see if any of the items in the given group are selected.
 *
 * @param  frm    Form object containing the group.
 * @param  group  Name of the checkbox group.
 * @return        True if has checked items, otherwise false.
 */
function HasCheckedItems(frm, group)
{
	var elem;

	for (var i = 0; i < frm.elements.length; i++)
	{
		elem = frm.elements[i];

		if (elem.name == group && elem.checked)
			return true;
	}

	return false;
}

/**
 * Checks to see if any of the items in the given group are selected.
 *
 * @param  frm    Form object containing the group.
 * @param  group  Name of the checkbox group.
 * @returns       True if has checked items, otherwise false.
 */
function HasPrefixedCheckedItems(frm, group)
{
	var elem;

	for (var i = 0; i < frm.elements.length; i++)
	{
		elem = frm.elements[i];

		if ((elem.name.substr(0, group.length) == group) && elem.checked)
			return true;
	}

	return false;
}



/******************************************************** Date Validation **/
function IsDate(_value)
{
	var _test = Date.parse(_value)
	if (isNaN(_test)) 
		return false;
	else 
		return true;
}
/**
 * Checks if the given field has a valid numeric value.
 *
 * @param     elem  Form element to check.
 * @optional  min   Minimum value the field can have.
 * @optional  max   Maximum value the field can have.
 * @return          True if successful, false otherwise.
 */
function IsValidNumericField()
{
	var val = arguments[0].value;
	var min = GetArgument(arguments, 1, null);
	var max = GetArgument(arguments, 2, null);

	if (!IsNumber(val))
		return false;

	if ((max == null && min == null) || (max == null && val >= min) || (min == null && val <= max))
		return true;

	return val >= min && val <= max;
}


function GetEra(year)
{
	return Math.floor(year / 100) * 100;
}

/**
 * Check's whether the given date is valid.
 */
function GetValidMonthLength(year, month)
{
	if (IsLeapYear(year) && month == 2)
		return 29;

	var MONTH_LENGTHS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

	return MONTH_LENGTHS[month - 1];
}

/**
 * Takes a 3 character string representation of a month and returns the
 * numeric equivalent of that month according to it's position in the
 * calendar.
 *
 * @param  month  Three letter representation of the month's name.
 * @returns       Numberic value of that month from 1-12
 *
 * @author ?
 */
function GetIntMonth(month)
{
	switch (month.toLowerCase())
	{
		case "jan": return 1;
		case "feb": return 2;
		case "mar": return 3;
		case "apr": return 4;
		case "may": return 5;
		case "jun": return 6;
		case "jul": return 7;
		case "aug": return 8;
		case "sep": return 9;
		case "oct": return 10;
		case "nov": return 11;
		case "dec": return 12;
		default:    return 0;
   }
}

function GetMonthName(month)
{
	var MONTHS_LIST = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

	return MONTHS_LIST[month - 1];
}

/**
 * Compares two dates, returning 1 if the first parameter is greater than
 * the second, -1 if the second is greater than the first, and 0 if they
 * are the same date.
 *
 * @param  d1  First date.
 * @param  d2  Second date.
 * @returns    1: (d1 > d2); -1: (d1 < d2); 0: (d1 = d2)
 *
 * @author  Keith Gaughan 
 * @version 0.0.1 (2002-03-05)
 */
function CompareDates(d1, d2)
{
	var date1, date2;

	date1 = new Date(d1);
	date2 = new Date(d2);

	if (date1.valueOf() > date2.valueOf())
		return 1;

	if (date1.valueOf() < date2.valueOf())
		return -1;

	return 0;
}

function DateIsBefore(d1, d2)
{
	return CompareDates(d1, d2) != -1;
}

/**
 * Checks to see if the year passed in is a leap year.
 *
 * @param  year  Year to check.
 * @returns      True if a leap year; otherwise false.
 *
 * @author  Keith Gaughan 
 * @version 0.0.1 (2002-03-05)
 */
function IsLeapYear(year)
{
	if (year % 100 == 0 && year % 400 == 0)
		return true;

	return year % 4 == 0;
}

/************************************************** Miscellaneous Rubbish **/

/**
 * 
 */

function GetArgument(args, i, def)
{
	return (args.length > i) ? args[i] : def;
}

/**
 * Cascading Dropdown menu validation routine
 *
 * @param  obj  HTML select object i.e. &gt;select&lt;
 *
 * @note Requires two global arrays:
 *       <dl>
 
 *       </dl>
 *
 * You must set up 2 global arrays [in the calling page], of dropdown names and error messages.
 * arrDropdownNames -	This array holds the names of all the potential dropdowns - Both the ORDER and CASE of the items in this array is CRUCIAL.
 *			Make sure the entries are ordered in the same manner that the DDs will appear on the calling page
 * arrErrorMessages -	This array holds the error messages to display if there are any problems with the related DD: the DD at the same ordinal position in the _arrDropdownNames_ array			
 * 
 */
function ProcessForm(obj)
{
	// Test to see that the current selection of the current dropdown is
	// not the _prompt_ i.e. -- Select XXX --
	// If it is, do nothing: this stops reloading of the page on
	// selection of the prompt option.
	if (obj.value == 0)
		return;

	var clearFollowing = false;	// Note the dropdown hasn't been found yet.
	var errors = '';			//

	for (var i = 0; i < arrDropdownNames.length; i++)
	{
		var elem = obj.form.elements[arrDropdownNames[i]];

		if (obj == elem)
		{
			// We've found the DD which was selected
			// so mark the rest after it for clearing.
			clearFollowing = true;
		}
		else if (clearFollowing)
		{
			// The dropdown is after the selected one, so must be cleared.
			SetIfExists(elem, 0);
		}
		else if (elem.value == 0)
		{
			// It's before the selected one and has no value, so record its error.
			errors += "\n" + arrErrorMessages[i];
		}
	}

	if (errors != '')
	{
		alert("The following error(s) have occurred:\n" + errors);
	}
	else		
	{
		SubmitForm();
	}
}

/*********************************************** Transactions validation **/

function PartNumberInputControl(idWarrantor, lineNumber, size)
{
	var control = '';
	control += '<input type="hidden" id="idPN'+lineNumber+'" name="idPN'+lineNumber+'" value="">';
	control += '<input type="hidden" id="PN'+lineNumber+'" name="PN'+lineNumber+'" value="">';
	control += '<input type="text" id="display_PN'+lineNumber+'" name="display_PN'+lineNumber+'" value="" maxlength="250" size="'+size+'">';
	control += '<button id="pnBtn'+lineNumber+'" name="pnBtn'+lineNumber+'" class="listing" onClick="PartSelector_Inventory(' + idWarrantor + ',' + lineNumber + ')">...</button>';
	return control;
}
function PartNumberInputControl_ActiveOnly(idWarrantor, lineNumber, size)
{
	var control = '';
	control += '<input type="hidden" id="idPN'+lineNumber+'" name="idPN'+lineNumber+'" value="">';
	control += '<input type="hidden" id="PN'+lineNumber+'" name="PN'+lineNumber+'" value="">';
	control += '<input type="text" id="display_PN'+lineNumber+'" name="display_PN'+lineNumber+'" value="" maxlength="250" size="'+size+'">';
	control += '<button id="pnBtn'+lineNumber+'" name="pnBtn'+lineNumber+'" class="listing" onClick="PartSelector_Inventory_ActiveOnly(' + idWarrantor + ',' + lineNumber + ')">...</button>';
	return control;
}
function PartSelector_Inventory_ActiveOnly(idWarrantor, lineNumber)
{
	var w_height	= 700;
	var w_width		= 600;
	var w_top		= (screen.height-w_height)/2;
	var w_left		= (screen.width-w_width)/2;
	var partNumber	= eval("document.frm.display_PN" + lineNumber + ".value");

	var url = "/InputControls/PartNumber_Selector.asp?mode=partNumberSelector_Inventory&idWarrantor=" + idWarrantor + "&section=transaction&partNumber=" + partNumber + "&openersLineNumber=" + lineNumber + "&showActivePartsOnly=true";
	window.open(url, "PartNumberSelector", "toolbar=no,menubar=no,status=no,resizable=yes,scrollbars=yes,height="+w_height+",width="+w_width+",left="+w_left+",top="+w_top );
}

function valTxnLines(element, check, fieldName, lineNum)
{
	if ((eval(element + lineNum) != null))
	{	
		var value = eval(element + lineNum + ".value");

		if (check == "requiredField" && value == "")
			return "\n" + fieldName + " for line " + lineNum + " is required";

		if (check == "requiredRMAField" && value == "0")
			return "\n" + fieldName + " for line " + lineNum + " is required";

		if (check == "isPositiveWholeNumber" && !IsPositiveWholeNumber(value))
			return "\n" + fieldName + " for line " + lineNum + " must be a positive whole number";

		if (check == "isPositiveNumber" && !(IsNumber(value) && value > 0))
			return "\n" + fieldName + " for line " + lineNum + " must be a positive number";

		if (check == "isMoney")
		{
			if (value == "")
				return "\n" + fieldName + " for line " + lineNum + " is required";
			else
			{
				if (!IsMoney(value))
					return "\n" + fieldName + " for line " + lineNum + " must be numeric";
			}
		}
	}
	return "";	
}


function IsEmptyLine(lineNum)
{
	//Service and Cost added 15.07.02; Sean Tynan
	//Quantity and idPartNumber added 31.07.02; Sean Tynan

	if (lineNum == 1)
		return false;

	var idRMA	= eval("frm.idRMA" + lineNum);
	var qty		= eval("frm.Qty" + lineNum);
	var idPN	= eval("frm.idPN" + lineNum);
	var desc	= eval("frm.Desc" + lineNum);
	var sn		= eval("frm.Sn" + lineNum);
	var status	= eval("frm.Status" + lineNum);
	var service	= eval("frm.Service" + lineNum);	
	var cost	= eval("frm.Cost" + lineNum);		

	if (idRMA != null && idRMA.value != 0) 
		return false;

	if (qty != null && qty.value != "") 
		return false;

	if (idPN != null && idPN.value != "") 
		return false;

	if (desc != null && desc.value != "") 
		return false;

	if (sn != null && sn.value != "") 
		return false;

	if (status != null && status.value != "") 
		return false;

	if (service != null && service.value != "") 
		return false;

	if (cost != null && cost.value != "") 
		return false;

	if (idRMA != null && idRMA.value != 0) 
		return false;

	return true;
}

function submitTxn(errors, total, button)
{
	if (errors != "")
	{
		alert("The following error(s) have occurred:\n" + errors);
		EnableButtonAfterClick(button);
		return false;
	}

	if (!confirm("Units total " + total + "\n Are you sure you want to continue?"))
	{
		EnableButtonAfterClick(button);
		return false;
	}

	window.document.frm.submit();
	return true;
}

// change button colors on mouse over/out
function doMouseEvent(obj, action)
{
	obj.style.backgroundColor = (action == "over") ? "#ffffff" : "#eeeeee";
}

// write window opener button
function WriteWindowOpenerButton(onClickCode)
{
	document.write("<button onclick=\"" + onClickCode.replace("\"", "&quot;") + "\">...</button>")
}

/*****************************
	SLIDING POPUP WINDOW
****************************/
	
	function fSlidingPopup(strURL, strOptions)
	{
		winheight=100
		winsize=100
		x=10
		if ((screen.availHeight <= 600) && (screen.availWidth <= 800))
			{
			hheight = 410
			wwidth = 640	
			movet = 0
			movel = 0
			}
		else if ((screen.availHeight <= 768) && (screen.availWidth <= 1024))
			{
			hheight = 410
			wwidth = 640
			movet = 150
			movel = 150
			}
		else
			{
			hheight= screen.availHeight / 3 + 40
			hheight= hheight * 1.5
			wwidth = screen.availWidth / 3 + 90
			wwidth = wwidth * 1.25
			movet = screen.availHeight / 3	
			movet = movet / 1.5
			movel = screen.availWidth / 3
			movel = movel / 1.25
			}
			
		if (!(window.resizeTo&&document.all)&&!(window.resizeTo&&document.getElementById))
			{
			window.open(strURL)
			return
			}
		win2=window.open("", "", "width=1,height=1,"+strOptions+ ", top=" + movet + ", left=" + movel)  
		win2.moveTo(movel,movet)
		fSlidingPopup_Slide(strURL)
	}

	function fSlidingPopup_Slide(strURL)
	{
		if (winheight>= hheight)
			x=0
		win2.resizeBy(10,x)
		winheight+=10
		winsize+=10
		if (winsize>= wwidth)
		{
			win2.location=strURL
			winheight=10
			winsize=10
			x=10
			return
		}
	setTimeout("fSlidingPopup_Slide('"+strURL+"')",1)
	}
	
	function ValidateLines(element, check, fieldName, lineNum)
	{
		if ((eval(element + lineNum) != null))
		{	
			var value = eval(element + lineNum + ".value");

			if (check == "requiredField" && value == "")
				return "\n" + fieldName + " for line " + lineNum + " is required";

			if (check == "isNumeric" && !IsPositiveRealNumber(value))
				return "\n" + fieldName + " for line " + lineNum + " must be numeric";
			
			if (check == "isWholeNumeric" && !IsPositiveWholeNumber(value))
				return "\n" + fieldName + " for line " + lineNum + " must be numeric";
			
		}
		return "";	
	}
	function launch(newURL, newName, newFeatures)
	{
		var remote = open(newURL, newName, newFeatures);
		if (remote.opener == null)
			remote.opener = window;
		return remote;
	}
	function launchWindow(url)
	{
		myWin = launch(url, "", "screenX=0,left=0,screenY=0,top=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0,height=650, width=800");
	}
	// return true if the enter key was hit
	function isEnterKey(event)
	{
		// If the enter button pressed, return true
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13)
		{ 
			return true;
		}
		else
			return false;
}