/*
/  Timer function fired with multi-site selection 
/  Will automatically login user after specified number of seconds
/  Time is set when the function is called from the login class (10sec default)
*/
var login_timer;
auto_login_timer = function(time,str1,str2) {
    if (o = gGetElementById('failedmessage')) {
    	o.innerHTML=str1+' '+time+' '+str2;
    	time = time-1;
	}
    if (time > 0 && gGetElementById('form_action').value != 'login') {
    	login_timer = setTimeout("auto_login_timer("+time+",'"+str1+"','"+str2+"');", 1000, "Javascript");
    	return login_timer;
    } else {
    	return login();
    }
};
/*
/  Alternative getElementById function to overcome browser compatibility issues
*/
gGetElementById = function(s) {
	var o = (document.getElementById ? document.getElementById(s) : document.all[s]);
	return o == null ? false : o;
}
/*
/  Sets value of input field
*/
set_value = function(whichLayer,value) {
	elem = gGetElementById(whichLayer);
	elem.value = value;
}
/*
/  Uses the failedmessage div to display a 'working' AJAX loader .gif
*/
display_login_ajax_loader = function(t) {
   	clearTimeout(login_timer);
    gGetElementById('login_button').disabled = true;
    if (gGetElementById('site_select'))
    	gGetElementById('site_select').disabled = true;
	o = gGetElementById('failedmessage');
    o.innerHTML='';
    img = document.createElement('img')
    img.setAttribute("src", "images/ajax-loader_circle.gif")
    o.appendChild(img)
}
/*
/  Main AJAX engine that accepts url, div, parameters
/  Processes a request from url using parameters string and return results to div (t)
/  Allows for processing of HTML and JS in responseText (should be separated by ##js##)
*/
ajax_loadhtml = function(url, t, paramstring){
    var page_request = false
    display_login_ajax_loader(t)
	if (window.XMLHttpRequest) // if Mozilla, IE7, Safari etc
		page_request = new XMLHttpRequest()
	else if (window.ActiveXObject){ // if IE6 or below
		try {
		page_request = new ActiveXObject("Msxml2.XMLHTTP")
		}
		catch (e){
			try{
			page_request = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){}
		}
	}
	else
		return false
    ajax_timer = ajax_request_timeout()
	page_request.onreadystatechange=function(){ajax_inserthtml(page_request, t, ajax_timer)}
	page_request.open('POST', url, true)
		//Send the proper header information along with the request
		page_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		//page_request.setRequestHeader("Content-length", paramstring.length);
		//page_request.setRequestHeader("Connection", "close");
	page_request.send(paramstring)
	ajax_retry_request = function() {
		page_request.abort();
        ajax_loadhtml(url, t, paramstring);
	}
};
ajax_request_timeout = function() {
	return setTimeout("ajax_retry_request();", 10000, "Javascript");
};

/*
/  Monitors and processes AJAX state changes
/  Inserts HTML into target div (t)
/  Executes any js code returned after ##js## separator string
*/
ajax_inserthtml = function(page_request, t, ajax_timer){
	if (page_request.readyState == 4){
	    clearTimeout(ajax_timer);
	    if (page_request.status==200 || window.location.href.indexOf("http")==-1) {
        	var response = page_request.responseText.split('##js##');
	        if (response[0] != '##noreload##')
		        t.innerHTML=response[0];
	        if (response.length > 1)
	        	eval(response[1]);
        } else {
        	ajax_retry_request();
        }
	}
}; 
/*
/  Monitors and processes AJAX state changes
/  Inserts HTML into target div (t)
/  Executes any js code returned after ##js## separator string
*/
process_login = function(url) {
    var f = gGetElementById('login_form');
    paramstring = get_login_form_params(f); 
	thisDiv = gGetElementById('formDiv');
    ajax_loadhtml(url,thisDiv, paramstring)
};
/*
/  Collects form fields and returns a param string posted via AJAX
/  Text is encoded and will automatically be decoded through PHP's when received
*/
get_login_form_params = function(f) {
	var paramstring = '';
    var params = new Array();
	  for (var n=0; n < f.elements.length; n++) {
      	switch (f.elements[n].type) {
        	case 'select-multiple':
	        	var opts = new Array();
			    for (var i = 0; i < f.elements[n].length; i++) {
			        if (f.elements[n].options[i].selected == true) {
                    	var val = escape(f.elements[n].options[i].value);
			            opts.push(val);
			        }
			    }
               	var key = escape(f.elements[n].name);
	            params[n] = key+'='+'['+opts.join(',')+']';
            break;
            case 'button':
            case 'file':
		        params[n] = format_enc_var(f.elements[n].name,'');
            break;
            case 'radio':
        		if (f.elements[n].checked) {
                   	params[n] = format_enc_var(f.elements[n].name,f.elements[n].value);
                }            break;
            case 'checkbox':
        		if (f.elements[n].checked) {
                   	params[n] = format_enc_var(f.elements[n].name,'on');
                } else {
	        		params[n] = format_enc_var(f.elements[n].name,'');
                }
            break;
            default:
        		params[n] = format_enc_var(f.elements[n].name,f.elements[n].value);
      	}
	  }
    paramstring += params.join('&');
    return paramstring;
};
/*
/  Provides key,value pair encoding for parameter string
*/
format_enc_var = function(key,val) {
	key = escape(key);
	val = escape(val);
	return key+'='+val;
};
/*
/  Allows form submission on Enter keypress
/  Called with 'onkeypress' for specified input field
*/
function exec_on_enter (e,jsFunction) {
	if(window.event) // IE
		keynum = e.keyCode;
	else if(e.which) // Netscape/Firefox/Opera
		keynum = e.which;
	if (keynum && keynum == 13) {
    	eval(jsFunction+'();');
    }
    return true;	
}; 

function noenter(e) {
	var keynum;
    if(window.event) // IE
		keynum = e.keyCode;
	else if(e.which) // Netscape/Firefox/Opera
		keynum = e.which;
	return !(keynum == 13);
}; 


