var browserName = navigator.appName;
var browserVersion = navigator.appVersion;
var ie	= (browserName.indexOf('Microsoft')!=-1);
var ns4	= (browserName.indexOf('Netscape')!=-1);
var dom	= (document.getElementById) ? 0 : 1;

var ExpUsername		= /^[a-zA-Z][a-zA-Z_0-9]{2,11}$/i
var ExpPassword		= /^[a-zA-Z][a-zA-Z0-9]{3,14}$/i
var ExpSSN			= /^[0-9]{3}[(\-)|(\\)]?[0-9]{2}[(\-)|(\\)]?[0-9]{4}$/i
var ExpPin			= /^[0-9]{4}$/i
var ExpEmail		= /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/i
var ExpSMS			= /^[a-zA-Z_0-9\.\-]*@[a-zA-Z_0-9\.\-]+\.[a-z]{2,3}$/i
var ExpDate			= /^(0?[1-9]|1[0-2])\/(0?[1-9]|[1-2][0-9]|3[0-1])\/[1,2][0-9]{3}$/i
var ExpDobCAN		= /^(0?[1-9]|1[0-2])\/(0?[1-9]|[1-2][0-9]|3[0-1])$/i
var ExpPhone		= /^[\(]?[1-9][0-9]{2}([\/\)]\s?|-|\s)[0-9]{3}-?[0-9]{4}$/i
var ExpZip			= /^([0-9]{5}$|[0-9]{5}-[0-9]{4})$/i
var ExpZipCan		= /^([a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][0-9]|[a-zA-Z][0-9][a-zA-Z][\s][0-9][a-zA-Z][0-9])$/i
var ExpUrl			= /^(http:\/\/[a-zA-Z_0-9\.\-]*\.[a-z]{2,3}|https:\/\/[a-zA-Z_0-9\.\-]*\.[a-z]{2,3}|mailto:[a-zA-Z][a-zA-Z_0-9\.\-]*@[a-zA-Z_0-9\.\-]+\.[a-z]{2,3})$/i
var ExpUrlWithPath	= /^(https?:\/\/[a-zA-z_0-9\.\-]*\.[a-zA-Z]{2,4}([\/a-zA-Z_0-9\,\-]*\.[a-zA-Z]{2,4})?)$/i
var ExpCurrency		= /^(\$)?[0-9,]*(\.[0-9]{2}){0,1}$/
var ExpCurrency2	= /^(\$)?([0-9]*|(([0-9]{1,3}(\,)?)*[0-9]{3}))(\.[0-9]{2}){0,1}$/
var ExpNumeric		= /^[0-9]+$/
var ExpLetters		= /^[a-zA-Z]*$/i
var ExpBombayStore	= /^[0]{1}[0-1]{1}[0-9]{3}$/i
var ExpBombaySKU	= /^([0-9]{9}|[0-9]{8}|[0-9]{7})$/i

/* CAUTION: if you are going to use ExpRealNum, this will validate that it's a real number,
			however it will also allow blanks.  We will address this when time permits.
			Until then, use the isWhitespace() function, or trim and validate the length to make sure
			that something was entered.
*/
var ExpRealNum		= /^[0-9]*(\.[0-9]{1,2}){0,1}$/
var space			= /\s/
var quote			= /"/
var startspaces		= /^\s+/

var userBrowser = '';
var openXPos = 50;
var openYPos = 50;
var message			= "Unknown error.";

String.prototype.right = extract_right;
String.prototype.left = extract_left;
String.prototype.trim = trim_spaces;

function openPopUpWindow(linkURL, openWidth, openHeight, XPos, YPos) {
	var newWind=window.open(linkURL,'display','resizable,scrollbars=yes,width='+openWidth+',height='+openHeight+',screenX='+XPos+',screenY='+YPos);
	if (newWind.opener == null){
		newWind.opener = window;
	}
	newWind.focus();
	return false;
}

function isValid(pattern, str) {
	return pattern.test(str);
}

function stripChars(pattern, str) {
	return str.replace(pattern, "");
}

function replaceChars(pattern, str, replace) {
	return str.replace(pattern, replace);
}

function validateSearch() {
 	var f = document.BasicSearch;
 
 	f.Search.value = stripChars(startspaces, f.Search.value);

 	if (f.Search.value.length < 1) {
 		alert('Please enter something to search for.');
 		f.Search.focus();
 		return false;
 	}
}

function ResizeWindow() {
	//The height and width properties of the table cannot 
	//be retrieved by Netscape, so we ignore Netscape.
	IE = document.all;
	if (IE) {
		width = IE.MainTable.offsetWidth + 30;
		height = IE.MainTable.offsetHeight + 160;
		if (height >= 400) {
			height = 400;
		}
		window.resizeTo(width, height);
	}
}


function printerFriendly(strContent){
    var strHtml = "";
    strHtml += "<html>"
    strHtml += "<head>"
    strHtml += "<title>" + document.title + "</title>"
    strHtml += "</head>"
    strHtml += "<body leftMargin=\"0\" topMargin=\"0\" marginheight=\"0\" marginwidth=\"0\">"
	if (!strContent || strContent==""){
	    strHtml += "<table width=\"100%\" border=\"0\">"
	    strHtml += "    <tr class=\"mainnavbar\">"
	    strHtml += "        <td align=center class=\"mainnavlink\" style=color:#ffffff>"
	    strHtml += "            <a href=\"javascript:print();\" class=\"mainnavlink\">Print</a>"
	    strHtml += "            &nbsp;|&nbsp;"
	    strHtml += "            <a href=\"#\" onclick=\"self.close();\" class=\"mainnavlink\">Close Window</a>"
	    strHtml += "        </td>"
	    strHtml += "    </tr>"
	    strHtml += "    <tr>"
	    strHtml += "        <td align=center>"
	    strHtml += document.body.innerHTML;
	    strHtml += "        </td>"
	    strHtml += "    </tr>"
	    strHtml += "</table>"
	} 
    else
        strHtml += strContent;

    strHtml += "</body>"
    strHtml += "</html>"
	var printWindow = window.open("","PrinterFriendly","location=no,personalbar=no,toolbar=no,menubar=no,scrollbars=yes,status=no,resizable=yes,width=640,height=480");
	printWindow.document.open();
	printWindow.document.write(strHtml);
	printWindow.document.close();
	printWindow.print();
}

//Preload "swap" images for fast veiwing
//	The len is the length of the comma delimited string (strImages) with a trailing comma
//	The path is the path after '../images/'
function preload(strImages,len,path) {
	var imgarr = new Array();
	var EndPos,BegPos;
	BegPos = 0;
	for (var i = 0; i < len; i++) {
		imgarr[i] = new Image();
		EndPos = strImages.indexOf(",",BegPos);
		imgarr[i].src = '../../images/' + path + strImages.substring(BegPos,EndPos);
		BegPos = EndPos + 1;
		//alert(imgarr[i].src);
	}
}
function swap(image, newsrc){
    image.src = newsrc;
}
function isEmpty(s)
{   
	return ((s == null) || (s.length == 0));
}
					
function isWhitespace (s)
{   var i;
	var whitespace = " \t\n\r";
    // Is s empty?
    if (isEmpty(s)) return true;
    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    // All characters are whitespace.
    return true;
}

function isDigit (c)
{   
	return ((c >= "0") && (c <= "9"));
}

function Paging(X_intCurrentPage) {
	if(document.forms[0].CurrentPage) {
		document.forms[0].CurrentPage.value = X_intCurrentPage;
	}
	document.forms[0].submit();
}

function trim_spaces(from_where) {
    // Store the string in a temporary variable    
    var temp_string = this;

    // If no argument, then trim from both sides
    if (arguments.length == 0) {
        from_where = "BOTH";
    }
    
    // Trim spaces from the left
    if (from_where.toUpperCase() == "LEFT" || from_where == "BOTH") {
        while (temp_string.left(1) == " ") {
            temp_string = temp_string.substring(1);
        }
    }
    
    // Trim spaces from the right
    if (from_where.toUpperCase() == "RIGHT" || from_where == "BOTH") {
        while (temp_string.right(1) == " ") {
            temp_string = temp_string.substring(0, temp_string.length - 1);
        }
    }
    return temp_string;
}

function extract_left(total_chars) {
    return this.substring(0, total_chars);
}

function extract_right(total_chars) {
    return this.substring(this.length - total_chars);
}

function isspaces(fld){
	for (var i = 0; i <= fld.length; i++ ){
		if (fld.substr != ' '){
			return false;
		}
	}
	return true;
}	

/* This group of functions are for date manipulation and comparison */
function isValidDate(x_dteDate) {

	var dteMonth = GetInputMonth(x_dteDate);
	var dteDay = GetInputDay(x_dteDate);
	var dteYear = GetInputYear(x_dteDate);

	// test to make sure the date is formatted correctly
	if(!isValid(ExpDate, x_dteDate)) {
		alert('The date entered is not a valid format, use mm/dd/yyyy.');
		return false;
	}

	// test for leap year - IsLeapYear has its own alert boxes
	if(!IsLeapYear(dteMonth, dteDay, dteYear)) {
		return false;
	}
	
	// only 30 days in April, June, September, and November - IsThirtyDay has its own alert boxes
	if(!IsThirtyDay(dteMonth, dteDay)) {
		return false;
	}
	
	return true;
}
function GetInputMonth(x_strDate)
{
	var strInputMonth = new String(x_strDate);
	// return only the first part of the date (MM) of a date that's formatted MM/DD/YYYY
	return	strInputMonth.substr(0,strInputMonth.indexOf("/",0));
}
function GetInputDay(x_strDate)
{
	var strInputDay = new String(x_strDate);
	var intStartPosition = (strInputDay.indexOf("/",0) + 1);
			
	// return only the day (DD) of a date that's formatted MM/DD/YYYY
	return	strInputDay.substr(intStartPosition,(strInputDay.indexOf("/",intStartPosition) - intStartPosition));
}
function GetInputYear(x_strDate)
{
	var strInputYear = new String(x_strDate);
	var intStartPosition = (strInputYear.lastIndexOf("/",strInputYear.length) + 1);
			
	// return only the last part of the date (YYYY) of a date that's formatted MM/DD/YYYY
	return strInputYear.substr(intStartPosition,(strInputYear.length - 1));
}
function IsLeapYear(x_intMonth, x_intDay, x_intYear)
{
	//test for leap year for the starting date
	if(x_intYear % 4 == 0)
	{
		//Is it a century.
		if(x_intYear % 100 == 0)
		{
			//If a century, must be evenly divisible by 400.
			if(x_intYear % 400 == 0 )
			{
				//it's a leap year
				if((x_intMonth == 2) && (x_intDay > 29))
				{
					alert("There are only 29 days in February.");
					return false;
				}				
			}
			else
			{
				//it's not a leap year
				if((x_intMonth == 2) && (x_intDay > 28))
				{
					alert("There are only 28 days in February.");
					return false;
				}
			}
		}
		else
		{
			//it's a leap year
			if((x_intMonth == 2) && (x_intDay > 29))
			{
				alert("There are only 29 days in February.");
				return false;
			}
		}
	}
	else
	{
		//it's not a leap year, test for the number of days in february
		if((x_intMonth == 2) && (x_intDay > 28))
		{
			alert("There are only 28 days in February.");
			return false;
		}
	}
	return true;
}
		
function IsThirtyDay(x_intMonth, x_intDay)
{
	//there are only 30 days in April, June, September, and November
	if(((x_intMonth == 4) || (x_intMonth == 6) || (x_intMonth == 9) || (x_intMonth == 11)) && (x_intDay > 30))
	{
		alert("There are only 30 days in Month you entered.");
		return false;
	}
	return true;
}
function MakeDate(x_dteDate)
{
	var dteDate = new Date(parseInt(GetInputYear(x_dteDate)), parseInt(GetInputMonth(x_dteDate)-1), parseInt(GetInputDay(x_dteDate)), 0, 0, 0);
	// return the new date object
	return dteDate;
}
function CompareDates(x_dteStart, x_dteEnd)
{
	//Test that the Start Date hasen't already past the End Date
	if(MakeDate(x_dteStart)  > MakeDate(x_dteEnd))
	{
		return false;
	}
	return true;
}
function CheckRadioSelection(x_objRadioButton)
{
	//Validate the radio button is selected
	//Assumes at least 2 buttons in the group
	var intReturn = -1;	
	if(x_objRadioButton.length)	
	{
		for (var i = 0; i < x_objRadioButton.length; i++)
		{
			//They have selected Manager Rollup
			if(x_objRadioButton[i].checked)
			{
				intReturn =  x_objRadioButton[i].value;
			}
		}
	}
	else
	{
		if (x_objRadioButton.checked) 
		{
			intReturn =  x_objRadioButton.value;
		}
	}
	return intReturn;
}

// AutoTab start
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) 
{
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) 
	{
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}
	function containsElement(arr, ele) 
	{
		var found = false, index = 0;
		while(!found && index < arr.length)
			if(arr[index] == ele)
				found = true;
			else
				index++;
		return found;
	}
	function getIndex(input) 
	{
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input)index = i;
			else i++;
			return index;
	}
	return true;
}
//  AutoTab End -->	

//  Checkdate Begin <!-- Begin
function checkdate(objName) 
{
	var datefield = objName;
	if (chkdate(objName) == false) 
	{
		datefield.select();
		alert("That date is invalid.  Please try again.");
		datefield.focus();
		return false;
	}
	else {return true;}
}
function chkdate(objName) 
{
	var strDatestyle = "US"; //United States date style
	//var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value;
	//if (strDate.length < 1) {
	//return true;
	//}
	if (strDate.length < 6) 
	{
		return false;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) 
	{
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) 
		{
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) 
			{
				err = 1;
				return false;
			}
			else 
			{
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
		}
	}
	if (booFound == false) 
	{
		if (strDate.length>5) 
		{
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
	}
	if (strYear.length == 2) 
	{
		intYear = parseInt(strYear, 10);
		if ( intYear < 10)
		{
		strYear = '20' + strYear;
		}
		else
		{
		strYear = '19' + strYear;
		}
	}
	// US style
	if (strDatestyle == "US") 
	{
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) 
	{
		err = 2;
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) 
	{
		for (i = 0;i<12;i++) 
		{
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) 
			{
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
			}
		}
		if (isNaN(intMonth)) 
		{
			err = 3;
			return false;
		}
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) 
	{
		err = 4;
		return false;
	}
	if (intMonth>12 || intMonth<1) 
	{
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) 
	{
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) 
	{
		err = 7;
		return false;
	}
	if (intMonth == 2) 
	{
		if (intday < 1) 
		{
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) 
		{
			if (intday > 29) 
			{
			err = 9;
			return false;
			}
		}
		else 
		{
			if (intday > 28) 
			{
				err = 10;
				return false;
			}
		}
	}
	if (strDatestyle == "US") 
	{
		//datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
		datefield.value = intMonth + "/" + intday+"/" + strYear;
	}
	else 
	{
		datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
	}
	return true;
}

	function LeapYear(intYear) 
	{
		if (intYear % 100 == 0) 
		{
			if (intYear % 400 == 0) 
				{ return true; }
		}
		else 
		{
			if ((intYear % 4) == 0) 
				{ return true; }
		}
		return false;
	}
	
	function doDateCheck(from, to) 
	{
		if (Date.parse(from.value) <= Date.parse(to.value)) 
		{
			alert("The dates are valid.");
		}
		else 
		{
			if (from.value == "" || to.value == "") 
				alert("Both dates must be entered.");
			else 
				alert("To date must occur after the from date.");
		}
	}
//  CheckDate End -->


function CloseWindow() 
{
	this.close();
}
function PrintWindow()
{
	this.print();
}
function PopWindow(intEpanID) 
{ 
	var newWind= window.open('epPrintEPan.asp?EpanID=' + intEpanID  ,'PrintEpan','fullscreen=yes');
	if (newWind.opener == null)
	{
		newWind.opener = window;
	}
	newWind.focus();
	return false;
}

function TrimFormElements(myForm)
{
	var i;
	for(i = 0; i < myForm.elements.length; i++)
	{
		myForm.elements[i].value = myForm.elements[i].value.trim();
	}
}

function MaxLength(objFormFieldToValidate, intMaxLength, objFormFieldToJumpTo){
     if(objFormFieldToValidate.value.length>intMaxLength){
        alert("Maximum length is " + intMaxLength + " characters.")
        objFormFieldToValidate.value = objFormFieldToValidate.value.substring(0,intMaxLength);
        objFormFieldToJumpTo.focus();
     }
}
function ReadCookie(name) {
	var namearg = name + "=";
	var nlen = namearg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
	  var j = i + nlen;
	  if (document.cookie.substring(i, j) == namearg) {
	     var endpos = document.cookie.indexOf (";", j);
	     if (endpos == -1) endpos = document.cookie.length;
	     return unescape(document.cookie.substring(j, endpos));
	}
	  i = document.cookie.indexOf(" ", i) + 1;
	  if (i == 0) break;
	}
	return null;
}
function WriteCookie(name, value) {
	var expdate=new Date();
	expdate.setTime(expdate.getTime()+24*60*60*1000);	// expire cookie in 24 hours
	document.cookie = name + "=" + escape (value) + "; expires=" + expdate.toGMTString();
}
function DeleteCookie(name) {
	var expdate = new Date();
	expdate.setTime (expdate.getTime() - 1);  // Already gone!
	var cval = ReadCookie(name);
	document.cookie = name + "=" + cval + "; expires=" + expdate.toGMTString();
}

