
var popup = null;

function triggerButtonClickEvent(buttonID)
{
   try
   {       
        if(button.dispatchEvent) // Firefox
        {
            var newEvent = document.createEvent("MouseEvents");
            newEvent.initMouseEvent("click", true, true,window, 1, 1, 1, 1, 1, false, false, false, false, 0, button);
            button.dispatchEvent(newEvent);
        }
        else
        {
            button.click();
        }
        event.returnValue=false;
   }
   catch(e){alert(e.message);return;} 
}

function getEventKeyPressCode(ev)
{
    try
    {
        if(window.event) // IE
        {
            return ev.keyCode;
        }
        else if(ev.which) // Netscape/Firefox/Opera
        {
            return ev.which;
        }
   }
   catch(e){return -1;} 
}

function getToday () {
// get today's date as a string
var tmpdate = new Date();
return ( tmpdate.getMonth() +1 ) + "/" + tmpdate.getDate() + "/" + tmpdate.getFullYear();
}

function weekday ( xdate ) {
// get weekday number ( Monday = 1 )
var date = new Date();
return date.getDay();
}

function getTodayDateNum(){
	var xtmpdate = new Date();
	return xtmpdate.getDate();
}

function setFocusOnControl(controlName)
{
	var control = document.getElementById(controlName);
	if( control != null )
	{
		control.focus();
		return;
	}
}

// changes color of Calendar cells
var rowPicked = new String (getTodayDateNum());  // needs to be today's date onLoad.....
if(rowPicked.length == 1){rowPicked = "0" + rowPicked;}
function setColor(control){
	var cell = document.getElementById(rowPicked);
	cell.className = "normal";
	if(control == null){
		control=document.getElementById(rowPicked);
	}
	if(control != null){  // thinking of cases where you might not have the 31st in the next month chosen....
		control.className="tag";
		rowPicked=control.id;
	}
}

function helpPop (pageName) {
	closePopup();
	var url = getPath() + "/DesktopModules/Catalog/" + pageName;
	var xh = '400'; 
	var xw = '400';
	//openPopup( pageName, name , height , width )
	var dialogparam = "scrollbars=yes,status=no,location=no,menubar=no,resizable=yes";
	var screenparam = "height="+xh+",width="+xw ;
	var params = screenparam+","+dialogparam;
	var popup = window.open( url , 'HelpWin' , params );
}

function getCookieStr (xname) {
	var xcookie = document.cookie;
	if ( xcookie.indexOf ( xname ) < 0 ) { return ""; } 
	if ( xcookie.indexOf ( ";" ) > 0 ) { // get end of cookie
		var xend = xcookie.indexOf ( ";" ); }
		else { var xend = xcookie.length - 1; }
	var xstart = xcookie.indexOf ( xname ) + xname.length + 1;
	return unescape ( xcookie.substring ( xstart , xend ) );
}

function delCookie(xcookie) {
	var lastyear = new Date();
	lastyear.setFullYear(lastyear.getFullYear() - 20);
	document.cookie = xcookie + "=0; path=/ expires=" + lastyear.toGMTString();
}

function doLoad() {
if ( parent.frames.length == 0) {  // no frames found
	window.setTimeout( 'setFrames()',500); // need to pause the code, or else we got in a onLoad - no frames loop
	}
}

function checkDate(field) {  // returns true or false if date not formatted correctly
	var xval = field.value;
	var pattern = /\d\d\W\d\d\W\d\d\d\d\b/;
	var pattern2 = /\d\W\d\d\W\d\d\d\d\b/;
	var pattern3 = /\d\d\W\d\W\d\d\d\d\b/;
	var pattern4 = /\d\W\d\W\d\d\d\d\b/;
	var result = pattern.test ( xval ) ;
	var result2 = pattern2.test ( xval ) ;
	var result3 = pattern3.test ( xval ) ;
	var result4 = pattern4.test ( xval ) ;
	if ( result == true || result2 == true || result3 == true || result4 == true ) {
		return true; }
		else { return "Check Format: " + field.title + " Field"; }
}

function checkVal(){
	var thisform = window.document.forms[0];
	var xval = new String();
	var xreturn = new String();
	var xdfields = new String();
	for (i = 0; i < thisform.elements.length; i++) {
		var field = thisform.elements[i];
		var xfname = field.name.toLowerCase();
	// check any non-hidden date fields	
		if ( xfname.indexOf ( "date" ) > -1 && field.type != "hidden" && field.value != "" ) { 
			if ( checkDate(field) != true ) {
				xdfields = xdfields + "\r\n" + checkDate(field); }
		}
	// check all required fields	
		if ( field.REQUIRED=="TRUE"){
			if (field.type=="select-one" || field.type=="select-multiple")
			{
				xval =getSelectValue (thisform.elements[i]);
			}
			else 
			{
				xval = field.value;
			}
			if (xval == "" || xval == null || xval.indexOf("<<") > -1 ){
				xreturn = xreturn + "\r\n" + field.title;
			}
		}
	}
	xreturn = xreturn + xdfields;
	if ( xreturn != "" ){
		alert ( "Please fill out the following fields before Saving: " + "\r\n" + xreturn );
		return false; }
		else { return true; }
}

function popPic (url){
	closePopup();
	popup = window.open(url , "picWin", "");
}

function openPopupWithScroll( url, name , height , width ) {
	closePopup();	//ONLY ONE POPUP, and the reference is stored in the variable popup (hardcoded!)
	var xh = height; 
	var xw = width;
	var dialogparam = ",scrollbars=1,status=0,location=0,menubar=0,resizable=1";
	var screenparam = "height="+xh+",width="+xw ;
	params = screenparam+dialogparam;
	popup = window.open( url , name , params );
}

function openPopup( url, name , height , width ) {
	closePopup();	//ONLY ONE POPUP, and the reference is stored in the variable popup (hardcoded!)
	//url = getPath() + "/" + url;
	var xh = height; 
	var xw = width;
	var dialogparam = ",scrollbars=0,status=0,location=0,menubar=0,resizable=0";
	var screenparam = "height="+xh+",width="+xw ;
	params = screenparam+dialogparam;
	popup = window.open( url , name , params );
}

function closePopup() {
	var popup = popup;
	if (popup != null && popup.open) {
		popup.close();}
	popup = null;
}

function getSelectValue (select) {
	if (select.value == "") return "";
	if (select.value == null ) return "";
	if (select.type == "select-one") {
	return select.options[select.selectedIndex].value};  
	// Type is "select-multiple"
	var answer = "";
	for (var i = 0; i < select.options.length; i++) {
		var option = select.options[i];
		if (option.selected) answer += "," + option.value;
	}
	return answer.substring(1);
}   

function getPath(){
var xtmp = window.location.pathname.toLowerCase();
var xpath = window.location.pathname;
var xindex=xtmp.indexOf("default.aspx") - 1;
return xpath.substring(0,xindex);
}

function repSubstring ( xval , xfrom , xto ) {
// Replace subsrtrings
	while ( xval.indexOf ( xfrom ) > 0 ) { 
		xval = xval.slice ( 0 , xval.indexOf ( xfrom ) ) + xto + xval.slice ( xval.indexOf ( xfrom ) + xfrom.length , xval.length );
	}
return xval;
}
