<!--
//main search functions
  //sortfield and sortdirections for main searches
  //defined in each page used
	function ScrollTo(arg) {
	    document.fmain.resultsPage.value = arg;
	    document.fmain.submit();
	}
	
	function SortOn(arg){
	    if(arg == SortField){
	        if(SortDirection == "ASC"){
	            document.fmain.SortDirection.value = "DESC";
	        } else {
	            document.fmain.SortDirection.value = "ASC";
	        }
	    } else {
	        document.fmain.SortDirection.value = "ASC";
	        document.fmain.SortField.value = arg;
	    }
	    document.fmain.submit();
	}
//end main search functions


//this function takes an input (text/password/etc) 
//and clears its value
function clearField(field){field.value='';}

/**
*This function takes in an object, and will find it's Y coordinate
*/
function findPosY(obj){
	if(obj){
		var curtop = 0;
		if (obj.offsetParent){
			while (obj.offsetParent){
				curtop += obj.offsetTop
				obj = obj.offsetParent;
			}
		}
		else if (obj.y)
			curtop += obj.y;
		return curtop;
	}
}

/**
*This method takes in an X and Y coordinate and will scroll the page to these locations
*/
function anchorScroll(x,y){
	self.scrollTo(x,y);
}

//this function will select/deselect
//multiple checkboxes with the same name
var checkflag = "false";
function checkMultiple(field) {
	if(field!=null){
		numFields = field.length;
		if(!numFields){
			numFields = 1;
		}
		if (checkflag == "false") {
			for (i = 0; i < numFields; i++) {
				if(numFields>1){
					field[i].checked = true;
				}else{
					field.checked=true;
				}
			}
			checkflag = "true";
		}else{
			for (i = 0; i < numFields; i++) {
				if(numFields>1){
					field[i].checked = false;
				}else{
					field.checked=false;
				}
			}
			checkflag = "false";
		}
	}
}
/**
* This function takes in a checkbox input element, and a list of field names 
* to enable or disable, depending on the checkbox.checked property
* If boolean onChecked is true - then the form elements will toggle when the box is checked
* Else if onChecked is false - the form elements will toggle when the box is unchecked
* If boolean clearElements is true, when the form elements are disabled, their values will be cleared
*/
function toggleFormElements(checkbox,onChecked,clearElements){
	if((checkbox.checked==true && onChecked==true) || checkbox.checked==false && onChecked==false){
		for(i=3;i<arguments.length;i++){
			$(arguments[i]).disabled='';
			$(arguments[i]).className = "";
		}
		if($(arguments[3])){
			if($(arguments[3]).tagName == 'SELECT'){
				Field.focus($(arguments[3]));
			}else{
				Field.activate($(arguments[3]));
			}			
		}
	}else{
		for(i=3;i<arguments.length;i++){
			$(arguments[i]).disabled='true';
			$(arguments[i]).className = "disabled"
			if(clearElements){
				if($(arguments[i]).tagName == 'SELECT'){
					$(arguments[i]).selectedIndex = 0;
					//$(arguments[i]).selectedIndex = -1;
				}else{
					$(arguments[i]).value='';
				}
			}
		}
	}
}

function confirmDelete(name, extra){
	if(name==null || name.length==0){
		name = 'Object';
	}
	var textConfirm = 'Are you sure you want to delete this '+name+'?';
	if(extra!=null && extra.length>0){
		textConfirm += '\n\n'+extra+'.';
	}
	if(confirm(''+textConfirm)){
		return true;
	}else{
		return false;
	}
}

function iwValidateNumber(field, defval, fieldname){
    var tval = String(field.value);
    tval = tval.replace(/,/g,"");
    tval = tval.replace("$","");
    tval = tval.replace("%","");
    var newrem = Number(tval);
    if(isNaN(newrem)){
        alert("You must enter a valid number in the " + fieldname + " field.");
        field.value = field.defaultValue;
        field.blur();
        field.select();
        field.focus();
        return false;
    } else {
        field.value = tval;
    }
    return true;
}

/**
* This method validates date or datetime input fields
* TODO - Pass in a boolean if the field is required?
*/
function val_date(input,defVal){
	var date = input.value;
	if(date == '' || date.length==0){ //check here for boolean required
		return false;
	}
	var checkTime=false;

	if(date.length>12){
		checkTime=true;
		date=date.toUpperCase();
		var time = date.slice(date.indexOf(" ")+1);
		date = date.substr(0,date.indexOf(" "));
	}
	
	date = date.split("/");
	var year = date[2];
	var month = date[0]-1;
	var day = date[1];
	var theDate = new Date(year,month,day);

	//set the hours/minutes/seconds/milliseconds
	theDate.setSeconds(0);
	theDate.setMilliseconds(0);		
	if(checkTime){ //set the hours of theDate based on input
		var m = time.substr(time.indexOf(" ")+1); //grab am or pm 
		time = time.substr(0,time.indexOf(" ")).split(":"); //grab array with hours, minutes
		var hour = (time[0]=='12') ? new Number(0) : new Number(time[0]); //make a number out of hours, if it's 12, set it to 0
		hour = (m=='PM') ? hour + 12 : hour; //if it's a PM time, add 12 to hours
		var minute = new Number(time[1]);//store minutes
		theDate.setHours(hour);
		theDate.setMinutes(minute);
	}else{
		theDate.setHours(0);
		theDate.setMinutes(0);
	}
	
	/* check for errors in date */
	var goodDate = true;
	if(theDate.getFullYear() != year){
		goodDate=false;
	}
	if(theDate.getMonth() != month){
		goodDate=false;
	}
	if(theDate.getDate() != day){
		goodDate=false;
	}
	/* additional checking for time */
	if(checkTime){
		if(theDate.getMinutes() != minute){
			goodDate=false;
		}
		if(theDate.getHours() != hour){
			goodDate=false;
		}
	}
	
	if(!goodDate){
		alert('You must enter a valid date in the '+input.name+' field.');
		input.value=defVal;
		input.focus();
		return false;
	}
	return theDate;
}
	
/**
* This function takes two date or date/time inputs and two default values
* It validates each of the dates alone, and then checks to see if the second date
* comes after the second. (for start/end dates)
*/
function check_dates(input1,def1,input2,def2){
	date1 = val_date(input1,def1);
	date2 = val_date(input2,def2);
	if(date2 && date1){
		if(date2.getTime()<date1.getTime()){
			alert('The date/time for '+input2.name+' must come after the date/time for '+input1.name+'.');
			input2.focus();
		}
	}
}


function RemoveBad(field){
	var InStr = String(field.value);
	InStr = InStr.replace(/\</g,"");
    InStr = InStr.replace(/\>/g,"");
    InStr = InStr.replace(/\"/g,"");
    InStr = InStr.replace(/\'/g,"");
    InStr = InStr.replace(/\%/g,"");
    InStr = InStr.replace(/\;/g,"");
    InStr = InStr.replace(/\(/g,"");
    InStr = InStr.replace(/\)/g,"");
    InStr = InStr.replace(/\&/g,"");
    InStr = InStr.replace(/\+/g,"");
    field.value = InStr;
    return InStr;
}


/**
* This method pops up a new window with no toolbar, etc.
* It also sets the new window to 'dependent', meaning when the parent window is closed
* the popup will also be closed.
* @param url	The url/location of the new window
* @param width	The width of the new window
* @param height	The height of the new window
*
*/
function popup(url,w,h){
	var newWind = window.open(url,'remote','toolbar=0,dependent=1,directories=0,scrollbars=1,location=0,status=0,menubar=0,resizable=1,width='+w+',height='+h+'');
	if(newWind.opener==null){
		newWind.opener=window;
	}
}
function popup_shortcut(URL, label){
	var theURL='popup_shortcut.jsp?URL='+escape(URL)+'&label='+label;
	popup(theURL,550,120);
}
function popup_color(FormName, IDField, NameField){
	var theURL = 'popup_color.jsp?FormName='+FormName+'&IDF='+IDField+'&NameF='+NameField;
	popup(theURL,200,200);
}
function popup_person(FormName,IDField,NameField){
	var theURL = 'popup_person.jsp?FormName='+FormName+'&IDF='+IDField+'&NameF='+NameField;
	popup(theURL,550,400);
}
function popup_company(FormName, IDField, NameField){
	var theURL = 'popup_company.jsp?FormName='+FormName+'&IDF='+IDField+'&NameF='+NameField;
	popup(theURL,550,400);
}

function submitPsearch(fieldName, formName, title){
    if(fieldName.value > 0){
        formName.submit();
    } else {
        alert("Please select a "+title+" first");
    }
}

//this function will check whether an element is hidden, and change
//its display to the opposite 
//pass in the ID of one div/table/etc to hide or show
//also, pass in an optional form element to focus on display
//optional - defaultHidden: if the element is set to display:none in the style sheet
function swap(box1, focus1, boolInline){
	var box1 = document.getElementById(box1);
	if(box1.style.display==''){ //If this is the first time swapping this object, grab its style from the style sheet
		if (box1.currentStyle){//IF INTERNET EXPLORER
			var currDisp = eval('box1.currentStyle.' + 'display');
		}
		else if (document.defaultView.getComputedStyle){//IF MOZILLA
			var currDisp = document.defaultView.getComputedStyle(box1,null).getPropertyValue('display');
		}
		//Might check here to over-ride style in style sheet with inline style?
		box1.style.display=currDisp; //set the box's display to stylesheet value
	}
	
	var disp = "block";
	box1.style.display=(box1.style.display=="none")?disp:"none"; //if none, go to block or inline & vice versa
	if(focus1){
		if(box1.style.display==disp){Field.activate($(focus1));}
	}
}

function swapRow(box1, focus1, boolInline){
	var box1 = document.getElementById(box1);
	if(box1.style.display==''){ //If this is the first time swapping this object, grab its style from the style sheet
		if (box1.currentStyle){//IF INTERNET EXPLORER
			var currDisp = eval('box1.currentStyle.' + 'display');
		}
		else if (document.defaultView.getComputedStyle){//IF MOZILLA
			var currDisp = document.defaultView.getComputedStyle(box1,null).getPropertyValue('display');
		}
		//Might check here to over-ride style in style sheet with inline style?
		box1.style.display=currDisp; //set the box's display to stylesheet value
	}
	
	var disp = "";
	box1.style.display=(box1.style.display=="none")?disp:"none"; //if none, go to block or inline & vice versa
	if(focus1){
		if(box1.style.display==disp){Field.activate($(focus1));}
	}
}

/**
* This function will show the first element and hide the next two
* useful in hiding/display inactive,active, or both (projects/opportunities/activities)
*/
function showAndHide(show,hide1,hide2){
	$(show).style.display='block';
	$(hide1).style.display='none';
	$(hide2).style.display='none';
	
	$(show+'_link').style.fontWeight='bold';
	$(hide1+'_link').style.fontWeight='normal';
	$(hide2+'_link').style.fontWeight='normal';
}

//This function will show all elements of class:toShowClass
//and hide all elements of class:toHideClass - uses prototype function getElementsByClassName
function swapElementsByClass(toShowClass,disp,toHideClass){
	var elementsToShow = document.getElementsByClassName(toShowClass);
	if(!disp){disp='block';}
	for (var i = 0; i < elementsToShow.length; i++){
		elementsToShow[i].style.display=disp;
	}
	var elementsToHide = document.getElementsByClassName(toHideClass);
	for (var i = 0; i < elementsToHide.length; i++){
		elementsToHide[i].style.display='none';
	}
	return false;
}

function myContact_icon_click(obj){
	var url = 'ajax_myContact.jsp';
	var act = ""
	if(obj.getAttribute("Activate")==1){
		act = "&Activate=1"
	}else{
		act = "&Deactivate=1"
	}
	var pars = 'PersonID=' + obj.getAttribute("PersonID") + "&ParentID = " + obj.getAttribute("ParentID") + act;
	var target = obj.parentNode;
	var myAjax = new iw_Ajax.Updater({success: target, failure: document.body}, url, {method: 'get', parameters: pars});
	Element.cleanWhitespace(target)
}
try{
Event.observe(document, "mouseover", showHelpMessage, false);
Event.observe(document, "mouseout", hideHelpMessage, false);
}catch(err){}
function showHelpMessage(event){
	var obj = Event.element(event);
	var help = Element.getAttribute(obj,"help");
	var warn = Element.getAttribute(obj,"warn");
	var info = Element.getAttribute(obj,"info");
	var errMsg = Element.getAttribute(obj,"errMsg");
	if(errMsg != "" && errMsg != null){
		var popUp = iw_DHTML.showPopUp(obj, errMsg, "show");
		popUp.style.backgroundImage = "url(imgs/popUp/icon_alert.gif)";
		Event.stop(event);
		return false;
	}
	if(warn != "" && warn != null){
		var popUp = iw_DHTML.showPopUp(obj, warn, "show");
		popUp.style.backgroundImage = "url(imgs/popUp/icon_warn.gif)";
		Event.stop(event);
		return false;
	}
	if(info != "" && info != null){
		var popUp = iw_DHTML.showPopUp(obj, info, "show");
		popUp.style.backgroundImage = "url(imgs/popUp/icon_information.gif)";
		Event.stop(event);
		return false;
	}
	if(help != "" && help != null){
		var popUp = iw_DHTML.showPopUp(obj, help, "show");
		popUp.style.backgroundImage = "url(imgs/popUp/icon_help.gif)";
		Event.stop(event);
		return false;
	}
	Event.stop(event);
	return false;
}

function hideHelpMessage(event){
	var obj = Event.element(event);
	var msg = obj.getAttribute("help");
	msg += obj.getAttribute("warn");
	msg += obj.getAttribute("error");
	if(msg != "" && msg != null){
		Event.stop(event);
		return false;
	}
	iw_DHTML.hidePopUp()
	Event.stop(event);
	return false;
}

function getKey(e) {
	var key, keychar;
	if (window.event){
	   key = window.event.keyCode;
	} else if (e){
	   key = e.which;
	}else{
	   key = null;
	}
	//alert("Key = " + key);
	if(key == null || key == 0) return;
	keychar = String.fromCharCode(key);
	//alert("Key = " + keychar);
	return keychar;
}
//-->