//===============================================================================
//========== General Functions ==================================================
//===============================================================================
function on(img_name) {
  var s=document[img_name].src;
  var ss=s.substring(0,(s.length - 4))+'_a'+s.substring((s.length - 4),s.length)
  document[img_name].src=ss;
}

function off(img_name) {
  var s=document[img_name].src;
  var ss=s.substring(0,(s.length - 6))+s.substring((s.length - 4),s.length)
  document[img_name].src=ss;
}

function popUpFrame(url) {
  var win = window.open(url);
}

function popUp(url, name, features) {
  var newwin = window.open(url, name, features);
}

function helpPopUp(url) {
  var win = window.open(url,"","resizable,scrollbars,toolbar,status,WIDTH=700,HEIGHT=550");
}

function goBack(){
	window.history.go(-1);
}

function modalWindow(theURL,args,features) { 
    window.showModalDialog(theURL,args,features);
}

// for dev purposes
function modalWindowM(theURL,winName,features) { 
    window.open(theURL,"","resizable,scrollbars,menubar,WIDTH=480,HEIGHT=320");
}

function submitThisForm(f, action){
	//alert("Hit OK to submit: "+f.name)
	f.func.value=action
	f.target="_self";
	return f.submit();
}

function goTo(url) {
	location = url;
}

function printPopUp(url) { 
    var win = window.open(url,"","resizable,scrollbars,toolbar,status,WIDTH=500,HEIGHT=400");
}

function remindersPopUp(url) { 
    if (document.all) {
        window.showModalDialog(url , "", "dialogHeight:500px;dialogWidth:700px;center:yes;resizable:yes;scroll:yes;status:no");
    } else {
        window.open(url,"","resizable,scrollbars,menubar,WIDTH=700,HEIGHT=500");
    }
}

function confirmLink(link, warning) {
    if (confirm(warning)) {
        document.location=link;
    }
}

function getRadioValue(idOrName) {
    var value = null;
    var element = document.getElementById(idOrName);
    var radioGroupName = null;  

    // if null, then the id must be the radio group name
    if (element == null) {
        radioGroupName = idOrName;
    } else {
        radioGroupName = element.name;  
    }

    if (radioGroupName == null)	{
        return null;
    }

    var radios = document.getElementsByTagName('input');

    for (var i=0; i<radios.length; i++) {
        var input = radios[ i ]; 
        if (input.type == 'radio' && input.name == radioGroupName && input.checked) {
            value = input.value;
            break;
        }
    }
    return value;
}

function getSelectedRadioByName(name) {
    var radioGroupName = name;  

    if (radioGroupName == null)	{
        return null;
    }

    var radios = document.getElementsByTagName('input');
    
    for (var i=0; i<radios.length; i++) {
        var input = radios[ i ]; 
        if (input.type == 'radio' && input.name == radioGroupName && input.checked) {
            return input;
        }
    }
    
    return null;
}

function getSelectedCheckBoxByName(name) {
    var checkBoxName = name;  

    if (checkBoxName == null)	{
        return null;
    }
    
    var checkBoxes = document.getElementsByTagName('input');
    
    for (var i=0; i<checkBoxes.length; i++) {
        var input = checkBoxes[ i ]; 
        if (input.type == 'checkbox' && input.name == checkBoxName) {
            return input;
        }
    }
    
    return null;
}

function getTextBoxByName(name) {
    var textBoxName = name;  

    if (textBoxName == null)	{
        return null;
    }
    
    var textBoxes = document.getElementsByTagName('input');
    
    for (var i=0; i<textBoxes.length; i++) {
        var input = textBoxes[ i ]; 
        if (input.type == 'text' && input.name == textBoxName) {
            return input;
        }
    }
    
    return null;
}

//===============================================================================
//========== General Business Functions used in Event Model =====================
//===============================================================================

function setVariables() {
  if (navigator.appName == "Netscape") {
    v=".top=";
    dS="document.";
    sD="";
    y="window.pageYOffset";
  } else {
    v=".pixelTop=";
    dS="";
    sD=".style";
    y="document.body.scrollTop";
  }
}

function checkLocation() {
  object="header";
  yy=eval(y);
  eval(dS+object+sD+v+yy);
  setTimeout("checkLocation()",10);
}

//===============================================================================
// Change this to the time delay that you desire 
var what = null; 
var newbrowser = true; 
var check = false; 

function init() {  
    if (document.layers) {  
        layerRef="document.layers";  
        styleSwitch="";  
        visibleVar="show";  
        what ="ns4";  
    }  else if(document.all) {  
        layerRef="document.all";  
        styleSwitch=".style";  
        visibleVar="visible";  
        what ="ie4";  
    }  else if(document.getElementById) {  
        layerRef="document.getElementByID";  
        styleSwitch=".style";  
        visibleVar="visible";  
        what="dom1";  
    }  else {  
        what="none";  
        newbrowser = false;  
    } 
    check = true; 
} 
 
// Toggles the layer visibility on 
function showLayer(layerName) {  
    if(check) {  
        if (what =="none") {  
            return;  
        }  else if (what == "dom1") {  
            document.getElementById(layerName).style.visibility="visible";  
        }  else {  
            eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="visible"');  
        } 
    }  else {  
        return;  
    }  
} 

// Toggles the layer visibility off 
function hideLayer(layerName) {  
    if(check) {  
        if (what =="none") {  
            return;  
        }  else if (what == "dom1") {  
            document.getElementById(layerName).style.visibility="hidden";  
        }  else {  
            eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="hidden"');  
        }  
    }  else {  
        return;  
    }  
} 

function changeSelect(selector, subtypes_array, category, subcategory) {	
    selector = ge(selector);
    if( selector.getAttribute("typefor") )
        subselector = ge(selector.getAttribute("typefor"));

    if(category != null && subcategory != null){
        for(var i=0; i < selector.options.length; i++){
            if((selector.options[i].value) == category){
                selector.selectedIndex = i;
            }
        }
    }

	// Switch between State view and Province view   
    
    
    if(selector.value == "US" || selector.value == "CA"){
    	if(document.getElementById(selector.id + 'State') != null && document.getElementById(selector.id + 'Province') != null){
	    	document.getElementById(selector.id + 'State').style.display = "";
	    	document.getElementById(selector.id + 'Province').style.display = "none";	
    	}
    }else{
       	if(document.getElementById(selector.id + 'State') != null && document.getElementById(selector.id + 'Province') != null){
	    	document.getElementById(selector.id + 'State').style.display = "none";
	    	document.getElementById(selector.id + 'Province').style.display = "";	    	
    	}
    }
    
    if (selector && subselector) {
        // Clear Old Options
        subselector.options.length = 1;
        type_value = selector.selectedIndex;

        // Fill with New Options
        index = 0;


        suboptions = subtypes_array[type_value];

        for (var index=0; index < suboptions.length; index++) {
            subselector.options[index] = new Option(suboptions[index][0], suboptions[index][1]);
        }

        // Select Subcategory
        if (category != null && subcategory != null){
            for (var i=0; i < subselector.options.length; i++){
                if((subselector.options[i].value) == subcategory){
                    subselector.selectedIndex = i;
                }
            }
        }
        // Potentially Disable Subtype Selector
        subselector.disabled = subselector.options.length <= 0;
    }
}

function ge() {
    var ea;
    for (var i = 0; i < arguments.length; i++) {
        var e = arguments[i];
        if (typeof e == 'string')
            e = document.getElementById(e);
        if (arguments.length == 1)
            return e;
        if (!ea)
            ea = new Array();
        ea[ea.length] = e;
    }
    return ea;
}

function getElementsById(sId) {
	var outArray = new Array();	
	if(typeof(sId)!='string' || !sId) {
		return outArray;
	}
	if(document.evaluate) {
		var xpathString = "//*[@id='" + sId.toString() + "']"
		var xpathResult = document.evaluate(xpathString, document, null, 0, null);
		while ((outArray[outArray.length] = xpathResult.iterateNext())) { }
		outArray.pop();
	} else if(document.all) {
		for(var i=0,j=document.all[sId].length;i<j;i+=1){
			outArray[i] =  document.all[sId][i];
		}
	} else if(document.getElementsByTagName) {
		var aEl = document.getElementsByTagName('*');
		for (var i=0,j=aEl.length; i<j;i+=1) {
			if(aEl[i].id == sId ) {
				outArray.push(aEl[i]);
			}
		}
	}
	return outArray;
}

function showMenu(id) {
	var d = document.getElementById(id);
	for (var i = 1; i<=5; i++) {
		if (document.getElementById('dropMenu'+i)) {document.getElementById('dropMenu'+i).style.display='none';}
	}
	if (d) {d.style.display='block';}
}

function isNumericField(evt, object){ 
    try{
      var charCode = (evt.which) ? evt.which : event.keyCode;
      if (charCode > 31 && (charCode < 48 || charCode > 57)
          && charCode != 46 // is not a point
       	) { 
       	   return false;
       	}
    	if (charCode == 46 && object.value.indexOf('.') > -1) {
       		return false; 
       	}
       if (object.value.charAt(0) == '.') {
       	object.value = '0' + object.value;
       }
       return true;
   }
   catch(error){
      	return true;
   }
 }

function isStrictNumericField(evt, object) {
    try{
        var charCode = (evt.which) ? evt.which : event.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57)) { 
        	return false;
        }
        return true;
     }
     catch(error){
        return true;
     }
}

//===============================================================================
// Used to re-initialize the session timer before the session timeout
// if confirmed by the current user
// delay (in seconds) is the interval between the display of the page and the display of the confirmation message
// maxInactivity should be in seconds
var delay = 120; 
function reinitializeSession(actionType) {
	thisForm = document.forms[0];
	if(thisForm != null && thisForm.action != null) {
		thisForm.action.value = actionType;
		thisForm.submit();
	}
}
function confirmLogOff(maxInactivity, actionType) {
  if (confirm("Your session is about to expire.\nPress OK to continue for another " +  (maxInactivity / 60) + " minutes.\nOr press CANCEL to be redirected to the home page.")){
 	reinitializeSession(actionType);
  } else {
  	document.location='welcome.do';
  } 
}
function setInactivityTimeOut(maxInactivity, actionType) {
	timer=setTimeout(function() {confirmLogOff(maxInactivity, actionType)}, (maxInactivity - delay) * 1000);
}
function runInactivityTimeOut(checkSessionPropoerty, actionType, sessionAlert) {
	if(checkSessionPropoerty) {
		thisForm = document.forms[0];
		if(thisForm != null) {
			if(thisForm.sessionTimeOutAlertActivated != null && thisForm.sessionTimeOutAlertActivated.value == sessionAlert) {
				setInactivityTimeOut(maxInactivity, actionType);
			}
		}
	} else {
		setInactivityTimeOut(maxInactivity, actionType);
	}

} 

//===============================================================================
// Used to Load Javascript Files

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


init();

