
windowStack = new Array();
function addLoadEvent(_function) {
		var _onload = window.onload;
		if ( typeof window.onload != 'function' ) {
			if ( window.onload ) {
				window.onload = _function;
			} else {
				var _addEventListener = window.addEventListener || document.addEventListener;
				var _attachEvent = window.attachEvent || document.attachEvent;
				if ( _addEventListener ) {
					_addEventListener('load', _function, true);
					return true;
				} else if ( _attachEvent ) {
					var _result = _attachEvent('onload', _function);
					return _result;
				} else {
					//todo: preloading fix for ie5.2 on mac os
					return false;
				}
			}
		} else{
			window.onload = function() {
				_onload();
				_function();
			}
		}
	}
	
	
		function clearValue(field) { 
			if ('type here' == field.value) 
			field.value = ""; } 
function JSEnabledForm(fObj, useIFrameIn){
	var useIFrame = useIFrameIn | false;
	
	
	//this sets everyting valid, in anticipation of the server resetting any continued invalidity
	// this failes on hidden forms, but hey, they are hidden anyways..
	try{
		for(i in fObj.elements){
			if(fObj.elements[i] && fObj.elements[i].style)
				fObj.elements[i].style.border = "";
		}
	}catch(e){}
		
	//if we dont have a target iframe lets create one.
	if(! document.getElementById('formFrame')){
		alert('no iframe to hit, trying to create');
		var frmFrm = document.createElement("iframe");
		frmFrm.id="formFrame";
		frmFrm.height = 0;
		frmFrm.width = 0;
		frmFrm.style.visibility = "hidden";
		document.body.appendChild(frmFrm);
		if(document.all)
			document.getElementById('formFrame').attachEvent('onload',function(){iframeFinished('')});
		else
			document.getElementById('formFrame').addEventListener('load',function(){iframeFinished('')},false);		
	}  



	// did we succesffully ad the frame and are we supposed to use it?
	if( document.getElementById('formFrame') && useIFrame){	
		fObj.action = "/global/scripts/callTaker.cfm?xml=true";		
		fObj.target = "formFrame";
	}
	fObj.enctype = "multipart/form-data";

	try{
		for(i in fObj.elements){
			if(fObj.elements[i] && fObj.elements[i].style)
				fObj.elements[i].style.border = "";
		}
	}catch(e){}
	
	return true;
}

function loadMyFuncModule(code) {
  var dj_global = this; // global scope reference
  if (window.execScript) {
	
    window.execScript(code); // eval in global scope for IE
    return null; // execScript doesn’t return anything
  }
  return dj_global.eval ? dj_global.eval(code) : eval(code);
}


//this function gets called from either an hiden iframe loading or a XHR callback
var lastFormName;
function getterCallBack(respJSON){
	if(respJSON == "")
		return;
		
	try{
		lastGlobalResponse = eval( '(' + respJSON + ')');
	}catch(e){
		logError('error evaluating JSON response\n' + e.message + '\n This is an error.\n' + respJSON);
		return false;
	}
	try{
		close(lastFormName);
	}catch(e){}
	lastFormName = lastGlobalResponse.FORMNAME;
	try{
		if (lastGlobalResponse.ERROR){
			logError(lastGlobalResponse.content);	
		}
	}catch(e){}
	try{	
		var m = 0;	
		if(lastGlobalResponse.JAVASCRIPT != "")
			loadMyFuncModule(lastGlobalResponse.JAVASCRIPT);
		m = 1;
		try{
			if(lastGlobalResponse.CONTENTREQUEST){
				if(lastGlobalResponse.DIALOGUE){
					var DD = displayDialogue(lastGlobalResponse.CONTENT, lastGlobalResponse.FORMNAME, 400, 600);
					try{
					 window.scroll(0,-2000);
					 window.scroll(0,-2000);
					 window.scroll(0,-2000);					 					 
					}catch(e){
						
					}
				}else{
					if (lastGlobalResponse.TARGETDIV != ""){
						if(document.getElementById(lastGlobalResponse.TARGETDIV))	 				
							document.getElementById(lastGlobalResponse.TARGETDIV).innerHTML = lastGlobalResponse.CONTENT;						
					}else{
						for(i in windowStack)
							close(windowStack[i]);
						if(document.getElementById('page'))	 				
							document.getElementById('page').innerHTML = lastGlobalResponse.CONTENT;
						else if (document.getElementById('bodyBox'))
							document.getElementById('bodyBox').innerHTML = lastGlobalResponse.CONTENT;
					}
				}
			}else if(lastGlobalResponse.ACTIONMESSAGE != ""){
//				document.getElementById('ContentArea').innerHTML = lastGlobalResponse.ACTIONMESSAGE + document.getElementById('ContentArea').innerHTML;
				alert(lastGlobalResponse.ACTIONMESSAGE);
			}
		}catch(e){
			alert(e.message);
		}
		try{
			for (form in lastGlobalResponse.FORMS){
				document.getElementById(form).innerHTML = lastGlobalResponse.FORMS[form];
				if(lastGlobalResponse.DIALOGUE)
					squareAndCenter(document.getElementById(lastGlobalResponse.FORMNAME));
				//document.getElementById(form).onsubmit = function(){submitform(this)};
			}
		}catch(e){}
		if(lastGlobalResponse.ERROR && lastGlobalResponse.ERROR != "")
			logError(lastGlobalResponse.ERROR);
		
		m=2;
		try{
			if(lastGlobalResponse.FIELDTOFOCUSON && document.getElementById(lastGlobalResponse.FIELDTOFOCUSON))
				document.getElementById(lastGlobalResponse.FIELDTOFOCUSON).focus();
		}catch(e){}
		m=3;
		try{
			if(lastGlobalResponse.POSTJAVASCRIPT && lastGlobalResponse.POSTJAVASCRIPT != '')
				loadMyFuncModule(lastGlobalResponse.POSTJAVASCRIPT);
		}catch(e){}
		m=4;		
		
	}catch(e){
		alert('error handling response: ' + m +  "\n" + e.message);
	}

					
	

}

function showEmailOptions(){
	try{
		document.getElementById('emailOptions').style.display = "block";	
	}catch(e){}
}

function hideEmailOptions(){
	try{
		document.getElementById('emailOptions').style.display = "none";	
	}catch(e){}
}

function iframeFinished(){
	if (!document.all){
		var resp = getFramecontent('formFrame');
	}else
		var resp = getFramecontent('formFrame');

	getterCallBack(resp);
}

function getFramecontent(frameid){
	var iframe = document.getElementById(frameid);
	var ddoc = iframe.document || iframe.contentDocument ||
	iframe.contentWindow && iframe.contentWindow.document || null;	

	// I love writing all my code for every version of every browser in existence. ^^
	if(document.all)
		return(document.frames(frameid).document.body.innerText);
	else
		return ddoc.documentElement.textContent;

} 

function logError(errorText){
	try{
	globalLoggerDiv.innerHTML = errorText + globalLoggerDiv.innerHTML;
	}catch(e){
		document.getElementById('logger').innerHTML += errorText;
	}
	
}

function displayDialogue(content, divName, height, width, bgcolor){
	grayOut(true);	
	if(document.all && parseInt(navigator.appVersion) < 7){
		var elements = document.documentElement.getElementsByTagName('select');
		for (var i=0; i<elements.length; i++)
           	elements[i].style.visibility = "hidden";
    }
	
	try{
		if(document.getElementById(divName)){
			document.body.removeChild(document.getElementById(divName));
			windowCount--;
		}
	}catch(e){}
	
	var modalDialogue = document.createElement("div");
	modalDialogue.id = divName;
	modalDialogue.width = width;
	modalDialogue.height = height;
	modalDialogue.innerHTML = '<table cellpadding=0 cellspacing=0 class="modalDialogue"><tr><td class="FGUpperLeft"> </td> <td class="FGUpperMiddle"><a href="javascript: close(\'' + divName + '\');" title="close this window dialogue"><img border=0 height=17 width=17 src="/images/blackCloseButton.jpg" height=17 style="position:relative; top:2px"></a> </td> <td class="FGUpperRight">  </td></tr>			<tr><td class="FGMiddleLeft"> </td> <td class="FGContentArea">  '+content+'</td> <td class="FGMiddleRight">  </td></tr>			<tr><td class="FGLowerLeft"> </td> <td class="FGLowerMiddle">  </td> <td class="FGLowerRight">  </td></tr>		</table>';
	var myWidth = "";
	var myHeight = "";
	if( typeof( window.innerWidth ) == 'number' ) {
    	myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    myWidth = document.documentElement.clientWidth;
    	myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    myWidth = document.body.clientWidth;
    	myHeight = document.body.clientHeight;
	}
	var yOffset = (myHeight - height) / 2;
	var xOffset = (myWidth - width) / 2;	
	modalDialogue.style.position = "absolute";
	if(xOffset < 0)
		modalDialogue.style.left 	= '0px';	
	else
		modalDialogue.style.left 	= xOffset + 'px';
	if(yOffset < 0)
		modalDialogue.style.top 	= '0px';
	else
		modalDialogue.style.top 	= yOffset + 'px';

	modalDialogue.style.zIndex 	= 26;
	document.body.appendChild(modalDialogue);
	windowStack[windowStack.length] = divName;
	windowCount++;
	//alert();
	return modalDialogue;
}

var windowCount = 0;

function grayOut(vis, options) {
  var options = options || {}; 
  var zindex = options.zindex || 25;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dsID = options.id || 'darkenScreenObject';
  var dark=document.getElementById(dsID);
  if (!dark) {
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id=dsID;                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById(dsID);  // Get the object.
  }
  if (vis) {
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
	var myWidth = "";
	var myHeight = "";
	if( typeof( window.innerWidth ) == 'number' ) {
    	myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    myWidth = document.documentElement.clientWidth;
    	myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    myWidth = document.body.clientWidth;
    	myHeight = document.body.clientHeight;
	}
	if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		if (document.body.clientWidth > myWidth)
		    myWidth = document.body.clientWidth;
		if (document.body.clientHeight > myHeight)
	    	myHeight = document.body.clientHeight;
	}

    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
	if(true){
	    dark.style.width= myWidth +'px';
    	dark.style.height= myHeight+'px';
	}else{
	    dark.style.width= pageWidth;
    	dark.style.height= pageHeight;	
	}
    dark.style.display='block';                          
  } else {
     dark.style.display='none';
  }
}

document.getElementsByClass = function (needle){
  var         my_array = document.getElementsByTagName("*");
  var         retvalue = new Array();
  var        i;
  var        j;

  for (i = 0, j = 0; i < my_array.length; i++)
  {
    var c = " " + my_array[i].className + " ";
    if (c.indexOf(" " + needle + " ") != -1)
      retvalue[j++] = my_array[i];
  }
  return retvalue;
}

function cclose(divId){
	close(divId);
}

function doClose(divId){
	close(divId);
	return false;
}

function close(divId){
	if(document.getElementById(divId)){
		document.body.removeChild(document.getElementById(divId));
		for (i in windowStack)
			if(windowStack[i] == divId)
		 		delete windowStack[i];	 	
		if (--windowCount <= 0){
			grayOut(false);	
			if(document.all && parseInt(navigator.appVersion) < 7){
				var elements = document.documentElement.getElementsByTagName('select');
				for (var i=0; i<elements.length; i++)
					elements[i].style.visibility = "visible";
			}
		}				
	}
}
