//select or unselect all
function multipleSelection(ckall, ckone)
{
	multipleSelection(ckall, ckone, 0);
}
function multipleSelection(ckall, ckone, fNumber)
{var select = document.forms[fNumber].elements[ckall].checked;
 if (document.forms[fNumber].elements[ckone] == null)
 	document.forms[fNumber].elements[ckone].checked = select;
 else{
 	if (document.forms[fNumber].elements[ckone].length==null){
 		document.forms[fNumber].elements[ckone].checked = select;
 	}else	
		for (i = 0; i < document.forms[fNumber].elements[ckone].length; i++){
			document.forms[fNumber].elements[ckone][i].checked = select;
		}
	}
}


//selects checked values from a lookup into a field on the parent screen
//values are separated by comma
function putSelectedValues(ckone, field) {
	var val="";
	if (document.forms[0].elements[ckone] == null){
	 	val = document.forms[0].elements[ckone].value;
	}else {
	 	if (document.forms[0].elements[ckone].length==null){
	 		val = document.forms[0].elements[ckone].value;
	 	}else	
			for (i = 0; i < document.forms[0].elements[ckone].length; i++){
				if (document.forms[0].elements[ckone][i].checked) 
					val += (val.length==0?"":",")+document.forms[0].elements[ckone][i].value;
			}
	}	
	window.opener.document.forms[0].elements[field].value = val;
	window.opener.document.forms[0].elements[field].focus();
	window.opener.document.forms[0].elements[field].select();
	window.close(this);
}

//selects a single value from a lookup into a field on the parent screen
function putValues(val, field) {
	window.opener.document.forms[0].elements[field].value = val;
	window.opener.document.forms[0].elements[field].focus();
	window.opener.document.forms[0].elements[field].select();
	window.close(this);
}

//validates if there are any checkboxes selected
function validateSelection(ck)
{var selected=false;
 if (typeof(document.forms[0].elements[ck].length) == 'undefined'){
	if (document.forms[0].elements[ck].checked)
		selected=true;
 }else
 	if (document.forms[0].elements[ck].length==null){
		if (document.forms[0].elements[ck].checked)
			selected=true;
 	}else	
		for (i = 0; i < document.forms[0].elements[ck].length; i++){
			if (document.forms[0].elements[ck][i].checked)
				return true; //selected=true;
		}
	return selected;
}

//this function replaces one char from a string to another, and returns the corrected string
function replaceChar(str, charFrom, charTo){
	var pos = 0;
	if (str!=null && str.length > 0){
		while (pos != -1){
			pos = str.indexOf(charFrom);
			if (pos != -1)
				str = str.substring(0, pos) + charTo + str.substring(pos + 1, str.length);
		}
	}
	return str;
}

// Remove the 'invalid' characters from the specified sting.
function removeInvalidChars(s) {
	s = replaceChar(s, '&', '');
	s = replaceChar(s, '?', '');
	s = replaceChar(s, '%', '');
	s = replaceChar(s, '#', '');
	s = replaceChar(s, '\'', '´');
	return s;
}

function loadEmails(name){
	if(document.forms[0].elements[name].disabled==false)
	window.open("/Launch/UserEmailAddressLookupSerlvet","EmialAddress","toolbar=false, width=500, height=400, scrollbars=yes");
}

function putValue(val, field) {
	window.opener.document.forms[0].elements[field].value = val;
}

function printpreview()
{
	var OLECMDID = 7;
	/* OLECMDID values: 
	* 6 - print 
	* 7 - print preview 
	* 8 - page setup (for printing) 
	* 1 - open window 
	* 4 - Save As 
	* 10 - properties 
	*/ 
	
	var PROMPT = 1; //1 PROMPT - 2 DONTPROMPTUSER 
	var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
	document.body.insertAdjacentHTML('beforeEnd', WebBrowser); 
	WebBrowser1.ExecWB(OLECMDID, PROMPT);
	WebBrowser1.outerHTML = "";
}

//function that returns the remainder of 'a' divided by 'b'
function Mod(a, b) { return a-Math.floor(a/b)*b; }


//the next code is to round with 2 decimals
function RoundValue(val, fieldName){
var orgValue = val+'';
var pos = orgValue.indexOf('.');
//only goes in if it has more than 2 decimal values
if (pos>0 && orgValue.substring(pos + 1, orgValue.length).length>2){
	orgValue=orgValue.match(/^\d*\.\d{3}/)*100;
	orgValue=Math.round(orgValue)/100
}
document.forms[0].elements[fieldName].value = orgValue;
}

// This function add decimal numbers to a number without it.
// 12 -> 12.00
function addDecimals(val, fieldName) {
var newVal;
	if(!isNaN(val)) {
		if (val.indexOf('.') == -1) {
			newVal = val + ".00";
			document.forms[0].elements[fieldName].value = newVal;
		} else {
			if((val.substr(val.indexOf('.') + 1)).length <=1) {
				newVal = val + "0";
				document.forms[0].elements[fieldName].value = newVal;
			}
		}
	}
}


// Add days for a current date
function addDayToCurrent(days){
	var dt = new Date();
	var temp_date = new Date(dt.getTime() + (days*24*60*60*1000));
	var dd=temp_date.getDate();
	if (dd<10) dd='0'+dd;
	var mm=temp_date.getMonth()+1;
	if (mm<10) mm='0'+mm;
	return (mm+'/'+dd+'/'+temp_date.getYear());
}


//replace a char or substring for a string
function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

//submits Form on Enter
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   //myfield.form.submit();
   validateForm(myfield.form);
   return false;
   }
else
   return true;
}
