var parameters = new Array();

// wait for window
var waitTime=500; // ms

var timer=0;
var timeoutId;
var maxTime=30000 // 30 seconds is all we'll wait for a window to load

// all buttons that cause document unload shoudl set=false. if false, onbeforeunload event handler may want to block event
var browserInitiatedUnload=true; 

function beforeUnload() {
  if (browserInitiatedUnload) {
     alert("Please use the buttons provided to navigate thru the application");
     return false;
  }
}
function getParameterValue(aName) {
  if (parameters.length==0) {
    parseQueryString()
  }
  for (var i=0; i < parameters.length; i++) {
    if (parameters[i].n==aName) return parameters[i].v;
  }
  return null;
}
function parseQueryString() {
  parseIntoParamObjects(unescape(document.location.search.substring(1)));
}
function parseIntoParamObjects(aString) {
  var end=0, start=0, eq=0, p=0;
  var n, v;  

  while (end > -1) {
    end = aString.indexOf('&', start)
    eq = aString.indexOf('=', start)
    n = aString.substring(start, eq)
    
    if (end == -1) {
      v = aString.substring(eq+1)
    } else {
      v = aString.substring(eq+1, end)
    }
    var f = 'x = new param("' + n + '","' + v + '")';
    eval(f);
    parameters[p]=x;

    if (end == -1) break;

    start = end+1;
    p++;
  }
}
function param(n, v) {
  this.n=n; 
  this.v=v;
  return this;
}

function writeAsCookies(paramObjects, exceptionList) {
  for (var i=0; i < paramObjects.length; i++) {
    if (notExcludedParam(paramObjects[i].n, exceptionList)==true) {
    	//alert(paramObjects[i].n + "=" + paramObjects[i].v)
    	SetCookie (parameters[i].n, parameters[i].v);
    }
  }
}
function notExcludedParam(paramName, exceptionList) {
  for (var i=0; i < exceptionList.length; i++) {
    if (exceptionList[i]==trim(paramName)) {
    	return false;
    }
  }
  return true;
}
function getCookieVal (offset) {  
    var endstr = document.cookie.indexOf (';', offset)  
    if (endstr == -1)      
  	endstr = document.cookie.length    
    return unescape(document.cookie.substring(offset, endstr))
}
function GetCookie (name) {
   var arg = name + '='
   var alen = arg.length;  
   var clen = document.cookie.length  
   var i = 0    
	while (i < clen) {   
	     var j = i + alen     
	     if (document.cookie.substring(i, j) == arg)   
	         return getCookieVal (j)  
	     i = document.cookie.indexOf(' ', i) + 1
             if (i == 0) break  
         } 
     return null
}
function SetCookie (name, value) { 
   var argv = SetCookie.arguments
   var argc = SetCookie.arguments.length  
   var expires = (argc > 2) ? argv[2] : null  
   var path = (argc > 3) ? argv[3] : null 
   var domain = (argc > 4) ? argv[4] : null 
   var secure = (argc > 5) ? argv[5] : false  
   c =  name + "=" + escape (value) + ((expires == null) ? "" : ("; expires="
	 + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + 
         ((domain == null) ? "" : ("; domain=" + domain)) +   
         ((secure == true) ? "; secure" : "")
  document.cookie =c;
}
function DeleteCookie (name) { 
   var exp = new Date();
   exp.setDate(-1);// = (exp.getTime() - 1);
   var cval = GetCookie(name);
   if (cval != null) {
     c = name + "=" + cval + "; expires=" + exp.toGMTString() + "; path=/;";
     document.cookie = c;
      
   }
}
function parseString(string, delim) {
  var start=0;
  var end;
  var i=0;
  var arr = new Array();
  if (string != null) {
    while(true) {
      end = string.indexOf(delim, start);
      if (end == -1) {
         arr[i++]=string.substring(start);
         break;
      }
      arr[i++]=string.substring(start,end);
      start=end+1;
    }
  } 
  return arr;
}
function getDocumentObject(objname) {
   var f;
   
   if (typeof document.all != "undefined") {
      f = 'x = document.all.' + objname;
   } else {
      f = 'x = document.getElementById("' + objname + '")';
   }
   return eval(f);
}
function inspect(obj, objName) {
 
   var result = '';
   var eventHandlers='';
  // if (objName==null) objName=obj.name
    
   for (var i in obj) {
      //x = '<pre>'  + obj[i]  + '</pre>'
      x = obj[i] 
      if (i.substring(0,2)=='on') {
        eventHandlers += '<br>' + i + ' = ' + x
      } else {
        result += '<br>' + i + ' = ' + x
      }
   }
 
   w1 = window.open('', 'iWindow','')
   w1.document.write("<h2>Object: " + objName + "</h2><p>" + result + '<h2>Event Handlers</h2>' + eventHandlers);
   w1.document.close();
}
function closeWindow() {
 
  // set focus to the window's opener and close the window
  
  //if (window.parent != null) {
  //  if (window.parent.opener != null) {
 //     window.parent.opener.focus()
  //  }
     
 //  window.parent.close()
 // } else {
 
    if (window.opener != null && !window.opener.closed ) {
      window.opener.focus()
    }
    
    window.close();
 // }
}
function functionAvailable(aWindow, functionName) {
  var f = 'aWindow.' + functionName;
  if (typeof eval(f) != "undefined") {
    return true;
  } 
  return false;
}
function getAvailableWidth() {
	var myWidth =0; 
	if (typeof( window.innerWidth ) == 'number' ) { 
		//Non-IE 
		myWidth = window.innerWidth; 
	} else { 
		if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight )) { 
			//IE 6+ in 'standards compliant mode' 
			myWidth = document.documentElement.clientWidth; 
		} else { 
			if (document.body && (document.body.clientWidth || document.body.clientHeight )) { 
				//IE 4 compatible 
				myWidth = document.body.clientWidth; 
			} 
		} 
	} 
	return myWidth ;
}
function getAvailableHeight() {
	var myHeight =0; 
	if( typeof( window.innerHeight ) == 'number' ) { 
		//Non-IE 
		myHeight = window.innerHeight; 
	} else { 
		if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight )) { 
			//IE 6+ in 'standards compliant mode' 
			myHeight = document.documentElement.clientHeight; 
		} else { 
			if (document.body && ( document.body.clientWidth || document.body.clientHeight )) { 
				//IE 4 compatible 
				myHeight = document.body.clientHeight; 
			} 
		} 
	} 
	return myHeight;
}

// global availWidth and Height
var w = window.screen.availWidth;
var h = window.screen.availHeight;

function initWindow(maxW, maxH, layout, wdw) {

if (wdw==null) {
  wdw=this.window;
}

// set up the width and height as available width and height - 5
thisW = w-5;
thisH = h-5;

// if the window as designed (maxH and maxW) is smaller then the available screen, use the max
if (w > maxW) thisW=maxW;
if (h > maxH) thisH=maxH;
  
//wdw.innerHeight=thisH;
//wdw.innerWidth=thisW;

wdw.resizeTo(thisW,thisH);

// fan the windows out at 45 pixel intervals along the top of the screen
if (layout == "tile") {
  x=y=1;
  if (window.opener != null && typeof window.opener != "undefined" ) {

    //x=window.opener.screenLeft;
    //y=window.opener.screenTop;
    //y=y+2;
    //y=y+5;
    //    alert(window.opener.screenLeft)
    // this isn't working y=window.opener.screenLeft+45;
  }
}
if (layout == "center") {
  x = (w/2)-(thisW/2);
  y = (h/2)-(thisH/2);
}

wdw.moveTo(x,y); // (horizontal, vertical)
}

function initStyles() {
// fix styles for resolutions larger than 1024 x 768
  if (w >= 800) {
    if (typeof document.all != "undefined") {
      setStyles1(w);
    } else {
      setStyles2(w);
    }
  }
}
function setStyles1(w) {
  // using document.all
  document.all.base.style.fontSize=12;
}
function setStyles2(w) {
  // using document.getElementById
  document.getElementById('base').style.fontSize=12;
}
function setSelectable(controlName, theValueToChoose) {

  // this function can be called to select the appropriate SELECT option, radio button or checkbox
  // given the name of the control and the value which will decide which choice to select
 
  if (typeof document.all != "undefined") {
      f = 'theControl = document.all.' + controlName;
  } else {
      if (typeof document.getElementById  != "undefined") {
        f = 'theControl = document.getElementById("' + controlName + '")';
      }
  }
  eval(f);
  if (theControl==null) {
    return;
  }
  for (i=0; i < theControl.length; i++) {
    //alert(theControl[i].type + " - " + theControl[i].value + "=" + theValueToChoose);
    if (theControl[i].type=="radio" || theControl[i].type=="checkbox") {
    	if (theControl[i].value==theValueToChoose) {
           theControl[i].checked=true;
           return;
        }
    } else {
        // handle select
    	if (theControl[i].value==theValueToChoose) {
       	   theControl.options[i].selected=true; 
           return;
        }
    }
  }
}
function setFocusOn(controlName) {
   var obj = getDocumentObject(controlName);
   if (obj.focus) { // excludes radios
      obj.focus();
   }
}
function setBackgroundColorOn(controlName, color) {
   var obj = getDocumentObject(controlName);
   if (obj.style) {
      obj.style.backgroundColor=color;
   }
}
function getValueOfChecked(controlName) {
   var obj = getDocumentObject(controlName);
 
   if (obj==undefined) return;
   if (obj.length==undefined) return obj.value; // single radio 
 
   for (i=0; i < obj.length; i++) {
      if (obj[i].checked) {
         return obj[i].value;
      }
   }
   return null;
}
function getIdsOfAllChecked(controlName) {
   var obj = getDocumentObject(controlName);
   var chk='';
   for (i=0; i < obj.length; i++) {
      if (obj[i].checked) {
         chk=chk + obj[i].id + ",";
      }
   }
   return chk;
}
function checkReload() {
	 // not used
	if (reloadopener!= null && pagetoreload!=null) {
	    window.opener.document.location="/index.jsp"
 		//window.opener.document.location= pageToReload;
	}
}
function getAllObjectsOfType(aType) {
	var oObject = document.all;
	results = new Array();
	if (oObject != null){
		if (oObject.length != null){
			for (i = 0; i < oObject.length; i++){
				if (oObject(i).type==aType) {
      				results[results.length]=oObject(i)
      			}
      		}
   		} 
  	} 
	return results;
}
function disableAll(aType) {
	var o = getAllObjectsOfType(aType);
	for (i = 0; i < o.length; i++){
		o[i].disabled=true;
	}
}
function enableAll(aType) {
	var o = getAllObjectsOfType(aType);
	for (i = 0; i < o.length; i++){
		o[i].disabled=false;
	}
}
function hideDiv(divName) { 
	var o = getDocumentObject(divName);
	if (o==null) return;
	if (document.getElementById) { // DOM3 = IE5, NS6 
		o.style.visibility = 'hidden'; 
		o.style.display='none';
	} else { 
		if (document.layers) { // Netscape 4 
			o.visibility = 'hidden'; 
		} else { // IE 4 
			o.style.visibility = 'hidden'; 
		} 
	} 
	/*if (document.getElementById) { // DOM3 = IE5, NS6 
		document.getElementById(divName).style.visibility = 'hidden'; 
	} else { 
		if (document.layers) { // Netscape 4 
			document.hideShow.visibility = 'hidden'; 
		} else { // IE 4 
			document.all.hideShow.style.visibility = 'hidden'; 
		} 
	} */
} 
function showDiv(divName) { 
	var o = getDocumentObject(divName);
	if (o==null) return;
	if (document.getElementById) { // DOM3 = IE5, NS6 
		o.style.visibility = 'visible'; 
		o.style.display='block';
	} else { 
		if (document.layers) { // Netscape 4 
			o.visibility = 'visible'; 
		} else { // IE 4 
			o.style.visibility = 'visible'; 
		} 
	} 
} 

function noenter() {
  return !(window.event && window.event.keyCode == 13); 
}

var numb = '0123456789';
var lwr = 'abcdefghijklmnopqrstuvwxyz';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
 
function isValid(parm,val) {
  if (parm == "") return true;
  for (var i=0; i<parm.length; i++) {
   // alert("l2:" + parm.charAt(i))
    if (val.indexOf(parm.charAt(i),0) == -1) return false;
  }
  return true;
}
 
function isNumeric(parm) {return isValid(parm,numb);}
function isLowerCase(parm) {return isValid(parm,lwr);}
function isUpperCase(parm) {return isValid(parm,upr);}
function isAlpha(parm) {return isValid(parm,lwr+upr);}
function isAlphaNumeric(parm) { return isValid(parm,lwr+upr+numb);}
 
function isValidZip(theZip) {
	if (theZip == null || theZip=='') {
		return false;
	}
	 
	if (theZip.length!=5 && theZip.length!=9) {
		return false;
	}
	 
	if (!isNumeric(theZip)) return false;
	return true;
}
function isValidPhone(phone) {
	var s = phone.replace(/-/g,"");	// replace dash with empty string and check for numerics and length
	if (s.length!=10) {
		return false;
	}
	if (!isNumeric(s)) {
		return false;
	}
	return true;
}
function isValidCreditCardNumber(cardNumber) {
	// probably need more extensive checks
	if (!isNumeric(cardNumber)) {
		return false;
	}
	return true;
}
function validateDate(dt) {
   // accept dates in mm/dd/yyyy or mm-dd-yyyy
   var msg='';
   if (dt.value.indexOf('-') != -1) {
     components = parseString(dt.value, '-')
   } else {
     components = parseString(dt.value, '/')
   }

   if (components.length!=3) {
     msg = "Date (" + dt.value + ") must be in mm-dd-yyyy format";
     return msg;
   }
   if (components[0] == 'NaN' || components[1] == 'NaN' || components[2] == 'NaN' ) {
     msg = "Date (" + dt.value + ") must be in mm-dd-yyyy format";
     return msg;
   }
   if (components[0] < 1 || components[0] > 12) {
     msg = "Date (" + dt.value + ") must be in mm-dd-yyyy format";
     return msg;
   }
   if (components[1] < 1 || components[1] > 31) {
     msg = "Date (" + dt.value + ") must be in mm-dd-yyyy format";
     return msg;
   } 
   if (components[2] < 1995) {
     msg = "Year (" + dt.value + ") appears to be invalid.";
     return msg;
   }
   return msg;
}
function checkInputLength(textArea, maxlen){
    if (textArea.value.length> maxlen) {
        textArea.value=textArea.value.substring(0, maxlen);
    }
}
function htmlTrim(str) {
	str = str.replace (/&nbsp;/g, "");
	return str;
}
function trim(str) {
	str = str.replace (/^\s+/g, "");
	str = str.replace (/\s+$/g, "");
	return str;
}