

function addOption(selectObject,optionText,optionValue) {
	var optionObject = new Option(optionText,optionValue)
	var optionRank = selectObject.options.length
	//search for duplicates first...
	var foundit =0;

	for (i=0; i<selectObject.options.length; i++)
	{
		if ( selectObject.options[i].value == optionValue ) { foundit = 1; i=selectObject.options.length; }
	}
	if (foundit == 0) { selectObject.options[optionRank]=optionObject}
}

function clearOptions(selectObject) {
	var tempLength = selectObject.options.length;
	for (i=0; i<tempLength; i++)
	{
		selectObject.options[0]=null 
	}
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function Replace( sExpression, sFind, sReplaceWith, iStart, iCount ) {
	var iLengthExp 		= 0;
	var iRemoveLength 	= 0;
 	var iStartPosition 	= 0;
	var iSubstringLength 	= 0;
	var iLoop		= 0;
	var sFirstPart 		= "";
	var sThirdPart 		= "";
	var sStartSubstring 	= "";
	var sReturnValue 	= "";
	
	if (( sExpression == null ) || ( sFind == null ) || ( sReplaceWith == null )) {
		return null;	};
	if ( sFind == sReplaceWith ) { return sExpression; }		// prevent nasty recursion later
	if (( iStart == null ) || ( iStart < 0 )) { iStart = 0; };	// parameter check, default to 0
	if (( iCount == null ) || ( iCount < 1 )) { iCount = -1; }; 	// parameter check, default to -1
	iLengthExp 	= sExpression.length;	// check length of expression
	iRemoveLength 	= sFind.length;		// check length of sFind
	if (( iLengthExp < 1 ) || ( iRemoveLength < 1 )) {	
		return sExpression;			
	};
	if ( iStart > iLengthExp ) { return sExpression; };	// sanity check start vs. expression length
	if ( iStart > 0 ) {	// do this if the user specified a place to start the replacement(s)
		sLeftOfStart 	= sExpression.substring( 0, iStart );
		sRightOfStart	= sExpression.substring( iStart, iLengthExp);	
		sExpression	= sRightOfStart;	
	};
	iStartPosition 	= sExpression.indexOf( sFind );	

	while( iStartPosition != -1 ) {
		iLengthExp = sExpression.length;		
		iSubstringLength = iStartPosition + iRemoveLength;
		sFirstPart = sExpression.substring( 0, iStartPosition );
		sThirdPart = sExpression.substring( iSubstringLength, iLengthExp );
		sExpression = sFirstPart + sReplaceWith + sThirdPart;
		iStartPosition = sThirdPart.indexOf( sFind );
		if ( iStartPosition > -1 ) {
			iStartPosition = sFirstPart.length + sReplaceWith.length + iStartPosition;
		};
		iLoop++;	
		if (( iCount > -1 ) && ( iLoop == iCount )) { iStartPosition = -1; };
	};
	if ( iStart > 0 ) { 					
		sExpression = sLeftOfStart + sExpression;
	};
	sReturnValue = sExpression;

	return sReturnValue;
}



function stripStr(str)
{	var temp=""
	var strTemp = str
	var j;
	for (j=0; j < strTemp.length; j++)
	{
		//if (strTemp.charCodeAt(j) >= 65 &&  strTemp.charCodeAt(j) <= 122)  //charcters
		if (strTemp.charCodeAt(j) >=47 &&  strTemp.charCodeAt(j) <= 57) //numbers and /'s
		{
			temp = temp + strTemp.substr(j,1)
		}
	}	
	return temp; //.toLowerCase();
}

function Trim2(Untrimmed) 
{
	var Trimmed = '';
	for (var i = 0; i < Untrimmed.length; i++) 
	{
		if ( Untrimmed.charCodeAt(i)!=32 )  //not a space
		{ 
			Trimmed = Trimmed + Untrimmed.substr(i,1);	// just add it
		} else { 	// is a space
			if (Trimmed.charCodeAt(Trimmed.length-1) != 32) // last char isnt a space so add it
			{
				Trimmed = Trimmed + Untrimmed.substr(i,1);
			}
		}
	}
	return Trimmed;
} 



function autoMask(field, event, sMask, maxVal) {
        var KeyTyped = String.fromCharCode(getKeyCode(event));
        var targ = getTarget(event);
        keyCount = targ.value.length;


	//	if (getKeyCode(event) == 13) { return false; }	//enter key?

	if ( sMask.length == 0 ) { return true; }	//no mask specified, so anything goes
	if (KeyTyped.charCodeAt(0) < 32) return true; //control characters
	if(keyCount == sMask.length) {return false;}	//max length reached


	//allow almost any key ... (no brackets !)
    if (sMask.charAt(keyCount) == '*')  {if ( isNumeric(KeyTyped) || isAlpha(KeyTyped) || isSpecial(KeyTyped) ) return true;}

	if (maxVal > 0) { // limit to a maximum value (ie: 80 hours..)
		if ((field.value + KeyTyped > maxVal) && isNumeric(KeyTyped)) { field.value=maxVal; return false; } else {if (isNumeric(KeyTyped)) {return true;} else {return false; }}
	}


	//the NEXT character in our mask matches what the user typed.. 
	if (sMask.charAt(keyCount + 1) == KeyTyped) {
		//if this is a date field, then we can just PAD the input (turn a 1 or a 2 into 01 or 02) so that this character is allowed.
		if ( sMask == "##/##/##" || sMask == "##/##/####" ) {
			if (targ.value.length <= 2 ) { 
				targ.value = "0" + targ.value
			} else {
				targ.value = targ.value.substring(0,targ.value.length-1) + "0" + targ.value.substring(targ.value.length-1, targ.value.length)
			}
			return true;					
		}
	}




	//the NEXT character in our mask matches what the user typed.. 
	if (sMask.charAt(keyCount + 1) == KeyTyped) {
		//if this is a RPT percentage field, then we can just PAD the input (turn a 1 or a 2 into 01 or 02) so that this character is allowed.
		if ( sMask == "#.##" ) {
			if (targ.value.length <= 2 ) { 
				targ.value = "0" + targ.value
			} else {
				targ.value =  targ.value.substring(0,targ.value.length-1) + "0" + targ.value.substring(targ.value.length-1, targ.value.length)
			}
			return true;					
		}
	}

	//is a phone number
	if ( sMask == "(###) ###-####") { 
		//if they typed the 4th digit, automatically put in the ) and space, rather than force them to type the braket
		if (targ.value.length == 4 ) { 
			targ.value = targ.value + ") "
		}
		keyCount = targ.value.length;
	    //if ((sMask.charAt(keyCount) == '#') && isNumeric(KeyTyped)) { return true;}
	}


    if (sMask.charAt(keyCount) == KeyTyped) {return true;}


    if ((sMask.charAt(keyCount) == '#') && (isNumeric(KeyTyped) || KeyTyped == "." )) { return true;}
    if ((sMask.charAt(keyCount) == 'A') && isAlpha(KeyTyped)){return true;}


	if ((sMask.charAt(keyCount) == 'A') && KeyTyped == "."){return true;}
	if ((sMask.charAt(keyCount) == 'A') && KeyTyped == "_"){return true;}
	if ((sMask.charAt(keyCount) == 'A') && KeyTyped == "-"){return true;}
	if ((sMask.charAt(keyCount) == 'A') && KeyTyped == "@"){return true;}
	
	if ((sMask.charAt(keyCount+1) == '?') )
	{	
		field.value = field.value + KeyTyped + sMask.charAt(keyCount+1);
		return true;
	}
	
	if ((sMask.charAt(keyCount) != '#') && (sMask.charAt(keyCount) != 'A' ) )
	{	//a specified character ie: "(" -- add typed char to next position (if allowed)
		if ((sMask.charAt(keyCount+1) == '#' && isNumeric(KeyTyped)) || (sMask.charAt(keyCount+1) == 'A' && isAlpha(KeyTyped)) )
		{
			field.value = field.value + sMask.charAt(keyCount) + KeyTyped ;
		}
		return false;
	}
	

	return false;
}
function getTarget(e) {
	if (e.srcElement) {return e.srcElement;}
	if (e.target) {return e.target;}
}
function getKeyCode(e) {
	if (e.srcElement) {return e.keyCode}
	if (e.target) {return e.which}
}
function isNumeric(c)
{	var sNumbers = "01234567890";
        if (sNumbers.indexOf(c) == -1)
                return false;
        else return true;
}
function isSpecial(c)
{	var sNumbers = "~`!@#$%^&*()_-=+.,?";
        if (sNumbers.indexOf(c) == -1)
                return false;
        else return true;
}
function isAlpha(c)
{       var lCode = c.charCodeAt(0);
        if ((lCode >= 65 && lCode <= 122) || lCode==32   || lCode==39 ) //spaces or single quotes aLLOWED (for name fields)
        {return true;} else { return false;}
}
function isPunct(c)
{       var lCode = c.charCodeAt(0);
        if (lCode >= 32 && lCode <= 47 )
        {return true;}else{return false;}
}
function checkNum(e) { //allow NUMBERS ONLY
	var strCheck = '0123456789';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode < 31 ) { return true; }	 //dont trap BS key
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
}


function checkNum2(e) { //allow numbers and some formatting chars for phone/ccnumbers
	var strCheck = '0123456789 -/\()ext.EXT';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode < 31 ) { return true; }	 //dont trap control characters
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
}

function checkChars(e) { //allow just about everthign thats not going to mess with an sql, or xss injection
	var strCheck = "'.,?0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@.- / \ ";
	var whichCode = (window.Event) ? e.which : e.keyCode;

	var controlPressed = (window.Event) ? e.modifiers & Event.CONTROL_MASK : e.controlKey;
	if (controlPressed) { return false; }

	if (whichCode < 31) { return true; }	 //dont trap control keys  (8 = bs)
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
}

function stripChars(str) { //strip anythign that looks like it might mess with an xss
	var strCheck = "'.#!,0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@.- / \ ";
	var i=0;
	var tempstr="";

	for (i=0; i< str.length; i++)
	{
		for(j=0; j < strCheck.length; j++)
		{
			if ( str.substr(i,1) == strCheck.substr(j,1) ) { 
				tempstr=tempstr + str.substr(i,1); 
				j=strCheck.length;
			}
		}
	}
	return tempstr;
}

function stripNum(str) {  //
	var strCheck = "#0123456789,.- / \ ";
	var i=0;
	var tempstr="";

	for (i=0; i< str.length; i++)
	{
		for(j=0; j < strCheck.length; j++)
		{
			if ( str.substr(i,1) == strCheck.substr(j,1) ) { 
				tempstr=tempstr + str.substr(i,1); 
				j=strCheck.length;
			}
		}
	}
	return tempstr;
}

function checkEnterKey(e,f) {
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) { f.submit() }
}

function checkDate(obj, key, updown) // real time checking for semi-correct date format (does not check VALUES, use validDate() for that ONBlur)
{	if( obj.value.substr(obj.value.length-1,1) == "/" && key == 191) { return false; } //if theres already a slash then dont add it!
	
	if ( (key>47 && key<58) || (key>95&&key<106) || key == 191) { // numbers or backspace allowed
		if(obj.value.length==2 || obj.value.length==5)
		{
			//dont add a slash if theres already one there
			if( obj.value.substr(obj.value.length-1,1) != "/") 
			{ 	//they didnt put in a slash, so add one
				obj.value = obj.value + '/' 
			} else if(obj.value.length==2 ) { //they put in a slash and its only 2 chars long, so pad it with a '0'
				obj.value = "0" + obj.value
			} else if(obj.value.length==5 ) { //they put in a slash and its only 5 chars long, so pad second block with a '0'
				obj.value = obj.value.substr(0,3) + "0" + obj.value.substr(obj.value.length-2,1) + "/"
			}
		}else if(obj.value.length>=10) { //strip to 10 chars only 
			obj.value = obj.value.substr(0,10)
		}

		return true;

	} else if ( key==9 || key==8 || key ==37 || key ==39 || key ==38 || key ==40 || key ==46)	{	
   		//    tab,   backspace,     arrows,                                     del    
		return true;
	} else {
		return false
	}
}

function validDate(str)  // takes a date in MM/DD/YYYY format and makes sure that M !> 12 and DD !> 31
{	var tempStr;	// checkDate is just for on-the-fly format checking, use thsi function to check for out of bounds data
	str = stripChars(str);
	tempStr = str.split("/");
	//if (tempStr.Length <= 1) { return "blah"; }
	
	if (tempStr[0] > 12) { tempStr[0] = 12;  }	//alert("Your date contained an invalid value for the month field. \n\n It has been changed to the next closest value.");
	if (tempStr[1] > 31) { tempStr[1] = 31; }	//alert("Your date contained an invalid value for the day field. \n\n It has been changed to the next closest value.");
	if (tempStr[2] > 2200) { tempStr[2] = 2200;} //  alert("Your date contained an invalid value for the year field. \n\n It has been changed to the next closest value.");
	if (tempStr[0] == null || tempStr[1] == null || tempStr[2] == null ) {
		return "";
	} else {
		return tempStr[0] + "/" + tempStr[1] + "/" + tempStr[2];
	}
	
}

function autoSizeIFrame()
{
	var h;
	if (!('\v'=='v')) { //if not IE then
	    h = (document.body.offsetHeight + 50);
	} else { //is ie
	    h = (document.body.scrollHeight + 50);
	}
	parent.document.getElementById('bulkApprove_<% response.write(request.querystring("reportID")) %>').height = h;
}

