var ns4 = (document.layers);
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);
var submitOnce = 0;

function isUndefined(v) {
    var undef;
    return v===undef;
}

function raw_popup(url, target, features) {
	var _POPUP_FEATURES = 'location=0,statusbar=0,menubar=0,width=600,height=400';

    if (isUndefined(features)) features = _POPUP_FEATURES;
    if (isUndefined(target  )) target   = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    return theWindow;
}

function link_popup(src, features) {
	return raw_popup(src.getAttribute('href'),src.getAttribute('target') || '_blank',features);
}

/* Function used to (attempt to) limit the amount of text
 * entered into a textarea
 * From various places, and modified strangely */
function textLimiter( field, counter, max ) {
	if( field.value.length > max ) {
		field.value = field.value.substring( 0, max );
		return false;
	} else {
		var divElement = document.getElementById(counter);
		var remain = (max - (field.value.length));
		divElement.innerHTML = remain + " of " + max + " characters remaining.";
	}
}

/*
 * Same as above, but without notification
 */
function textLimitQuiet( field, max ) {
	if( field.value.length > max ) {
		field.value = field.value.substring( 0, max );
		return false;
	}
}

/* When leaving a given field, hide the counter..
 */
function textLimiterClear( counter ) {
	var divElement = document.getElementById(counter);
	divElement.innerHTML = "";
}


/* Check that form submit happens once and once only! */
function checkSubmitOnce() {
	if( submitOnce == 0 ) {
		submitOnce++;
		return true;
	} else {
		return false;
	}
}

function getelement(id){ 
    if (document.getElementById){
         return document.getElementById(id)
    }else{
        return eval('document.all.' + id)
    } 
}

function onLayer(whichLayer) {
	var style2 = getelement(whichLayer).style;
	style2.display = "block";
}

function offLayer(whichLayer) {
	var style2 = getelement(whichLayer).style;
	style2.display = "none";
}

function refreshQuestion(whichQuestion, refreshQuestion){
	var divElement = getelement(whichQuestion);
	var postback   = getelement('__postbackSource');
	postback.value = whichQuestion;
	var frm = divElement.form;
    frm.submit();
}

function toggleLayer(whichLayer){
	var style2 = getelement(whichLayer).style;
	style2.display = style2.display == "block" ? "none" : "block";
}

function timedSaveFunction( postBackTo ){
    var formElements = ["input.text", "input.checkbox", "input.radio", "select", "textarea"];
    var theForm = document.forms['metaform'];
    var elements = theForm.length;
    var ctrls = '';
    var vals = '';
	
    for(var i=0; i<elements; i++){
        var it = theForm.elements[i];
	    if( it.type=='textarea'){
	        if( ctrls.length > 0 ){
	            ctrls += '&';
	        }
	        
			ctrls += it.name + '=' + it.value;
	    }
    }

    if( ctrls.length > 0 ){
        var connection = new XHConn();
        
        if( !connection ) window.status = 'XMLHTTP not available!';
        
        var fnWhenDone = function(oXML) { window.status = 'Data successfully saved.'; }; //alert(oXML.responseText);};
        
        window.status = 'Saving data';  
        connection.connect( postBackTo, "POST", ctrls, fnWhenDone );
    }
    
    // set timeout has to be set -- what, every time?
    setTimeout('timedSaveFunction(\'' + postBackTo + '\')', 10000 );
}

function textAreaChanged( textAreaControl, textAreaSaveBar ){
    textAreaSaveBar.src = "./img/save.gif";
}

function saveTextAreaData( controlToSave, notifyControl, postBackTo ){
    var ctrls = '';
    var vals = '';
    var it = getelement(controlToSave);
    
    if( it != null && it.type=='textarea'){
        if( ctrls.length > 0 ){
            ctrls += '&';
        }
        
		ctrls += it.name + '=' + it.value;
		
        if( ctrls.length > 0 ){
            var connection = new XHConn();
            
            if( !connection ) window.status = 'XMLHTTP not available!';
            
            var fnWhenDone = function(oXML) { 
                window.status='Data successfully saved.'; 
            }; 
            window.status='Saving data';  
            connection.connect( postBackTo, "POST", ctrls, fnWhenDone );
            
            if( notifyControl ){
                notifyControl.src = "./img/saved.gif";
            }
        }
    }
    return false;
}

// Displays a prompt regarding the use of the site in read only mode
// Geoff Olding Evenlogic
function displayreadonlyprompt(){
  	if (!confirm('You are opening a read-only version of the Applicant website in a new window.  This should enable you to see all Applicant information exactly as seen by the Applicant')) {
      window.opener = 'Any Value';	
      window.close();	
    }
}
