/*----------------------------------------------*/
/*					Ghrs Common  				*/
/*----------------------------------------------*/

/*------------------------------------------------------------------------------*/
/* Declaration of global variables used to create and manage the calendar. 		*/
/*------------------------------------------------------------------------------*/
var ghrs_radio_selected = false; var ghrs_radio_displayed = false; var ghrs_autocomplete = false; var ghrs_onload_setted = false; var ghrs_loaded = false;

function ghrs_setSelectInOperaDimension(){
	var slc_array = document.getElementsByTagName("select");
	for(var i = 0; slc_array[i]; i++){
		slc_array[i].style.height = '18px';
		slc_array[i].style.width = (slc_array[i].offsetWidth + 4) + 'px'; 
	}
}

function ghrs_IEDomTrick(){
	return (document.readyState=='complete' || !(/*@cc_on!@*/false));
}

/* This function has the behaviour of tell us if the browser is any kind of Explorer. */
function ghrs_isExplorer(){
	return (String(navigator.userAgent).indexOf("MSIE") > -1);
}

/* This function has the behaviour of tell us if the browser is Explorer 6. */
function ghrs_isExplorer6(){
	return (String(navigator.userAgent).indexOf("MSIE 6") > -1);
}

/* This function has the behaviour of tell us if the browser is Explorer 7. */
function ghrs_isExplorer7(){
	return (String(navigator.userAgent).indexOf("MSIE") > -1);
}

function ghrs_isFirefox(){
	return String(navigator.userAgent).indexOf("Firefox") > -1;
}

/* This function has the behaviour of tell us if the browser is Opera. */
function ghrs_isOpera(){
	return String(navigator.userAgent).indexOf("Opera") > -1;
}

/* This function replace the normal setAttribute javascript function to allow us */
/* to set the attribute in all browsers without errors. */
function ghrs_setAttribute(node,name,value){
	var attr = '';
	if(name=='style' && (ghrs_isExplorer6() || ghrs_isExplorer7())) node.style.cssText = value;
	else{
		if(node.getAttribute(name)){
			node.setAttribute(name, value);
			if(name=='action' && ghrs_isExplorer7()){
				node.removeAttribute(name);
				attr = document.createAttribute(name);
				attr.value = value;
				node.setAttributeNode(attr);
			}
		}
		else{
			attr = document.createAttribute(name);
			attr.value = value;
			node.setAttributeNode(attr);
		}
	}
}

/* This function has the behaviour to create an element with attributes and a beginning value. */
function ghrs_createElementWithAttribute(type,attributes,val){
	var elem = document.createElement(type);
	if(val) elem.innerHTML = val;
	if(attributes){
		for(var id in attributes){
			ghrs_setAttribute(elem, id, attributes[id]);
		}
		// Explorer fix.
		if(attributes['style']=='display : none;') elem.style.display = 'none';
	}
	return elem;
}

/* This function allow us to obtain the position of an object contain inside other objects. */
function ghrs_findPosition(element){
	var left_pos = top_pos = 0;
	var tmp_element = element;
	if(element.offsetParent){
		do{
			left_pos = left_pos + element.offsetLeft + ((element!=tmp_element && !ghrs_isOpera()) ? ghrs_getCSSTypeInfo(element, "border-left-width") : 0);
			top_pos = top_pos + element.offsetTop + ((element!=tmp_element && !ghrs_isOpera()) ? ghrs_getCSSTypeInfo(element, "border-top-width") : 0);
			element = element.offsetParent;
		} while(element);
	}
	return [left_pos,top_pos];
}

/* This function allow us to obtain a desired rule CSS of an object. */
function ghrs_getCSSTypeInfo(element,rule){
	var value = 0;
	if(document.defaultView && document.defaultView.getComputedStyle){
		value = parseInt(document.defaultView.getComputedStyle(element, null).getPropertyValue(rule),10);
	}
	else if(element.currentStyle){
		rule = rule.replace(/\-(\w)/g, function (str, p1){
				return p1.toUpperCase();
		});
		value = parseInt(element.currentStyle[rule],10);
	}
	return (value>0) ? value : 0;
}

/* This function will stop the forwarding of the datas if sumbit check returt am */
/* error message. 								 */
function ghrs_stopEventPropagation(event){
	if (!event) event = window.event;
	event.cancelBubble = true;
	if (event.stopPropagation) event.stopPropagation();

	return true;
}

/* Testing make request */
function ghrs_makeRequest(url,func_name,func_params) {
	// Creating the http request object.
	var http_request = false;
	document.body.style.cursor='progress';
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line.
		}
	}
	else if (window.ActiveXObject) { // Internet Explorer.
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		document.body.style.cursor='auto';
		alert('Cannot create an XMLHTTP instance');
		return false;
	}
	// On status change the ghrs_showContents function will be called.
	if(func_params) http_request.onreadystatechange = function(){eval(func_name)(http_request,func_params); ghrs_autoCursorTypeRequest(http_request)};
	else http_request.onreadystatechange = function(){eval(func_name)(http_request); ghrs_autoCursorTypeRequest(http_request);};
	http_request.open('GET', url, true);
	http_request.send(null);
	return true;
}

function ghrs_autoCursorTypeRequest(http_request){
	var test_num = (eval(http_request.state)) ? http_request.state : http_request.readyState;
	if(test_num == 200 || test_num == 4) document.body.style.cursor='auto';
}

function ghrs_mouseMovement(event){ 
	//For Internet Explorer
	if(event.offsetX || event.offsetY) return [event.offsetX , event.offsetY];
	//Common browsers
	else return[event.pageX,event.pageY];
}

/* This function will show the div or the iframe that focus the user on	the list. */
function ghrs_showLightBox(){
	if(ghrs_isExplorer6() && !document.getElementById("iframe_hide_main_window_list")){
		var iframe = ghrs_createElementWithAttribute('iframe',{'id':'iframe_hide_main_window_list','class':'iframe_hide_main_window_list'},null);
		document.getElementsByTagName('body')[0].appendChild(iframe);
	}
	if(!ghrs_isExplorer6() && !document.getElementById("hide_main_window_list")){
		var div = ghrs_createElementWithAttribute('div',{'id':'hide_main_window_list','class':'hide_main_window_list'},null);
		document.getElementsByTagName('body')[0].appendChild(div);
	}
	if(ghrs_isExplorer6()){
		var lightbox = document.getElementById("iframe_hide_main_window_list");
		lightbox.style.height = window.document.body.scrollHeight + "px";
		lightbox.style.width = getWidth() + "px";
	}
	else{
		var lightbox = document.getElementById("hide_main_window_list");
		lightbox.style.height = "100%";
		lightbox.style.width = "100%";
	}
}

/* This function will show the div or the iframe that focus the user on	the list. */
function ghrs_showElementLightBox(elem_id,add_id,class_id){
	if(ghrs_isExplorer6() && !document.getElementById("iframe_hide_main_window_list")){
		var iframe = ghrs_createElementWithAttribute('iframe',{'id':'iframe_hide_element_'+add_id,'class':'iframe_'+class_id},null);
		document.getElementsByTagName('body')[0].appendChild(iframe);
	}
	if(!ghrs_isExplorer6() && !document.getElementById("hide_main_window_list")){
		var div = ghrs_createElementWithAttribute('div',{'id':'hide_element_'+add_id,'class':class_id},null);
		document.getElementsByTagName('body')[0].appendChild(div);
	}
	var lightbox = (ghrs_isExplorer6()) ? document.getElementById('iframe_hide_element_'+add_id) :  document.getElementById('hide_element_'+add_id);
	var elem = document.getElementById(elem_id);
	lightbox.style.height = elem.offsetHeight + "px";
	lightbox.style.width = elem.offsetWidth + "px";
	lightbox.style.top = ghrs_findPosition(elem)[1] + "px";
	lightbox.style.left = ghrs_findPosition(elem)[0] + "px";
	lightbox.style.zIndex = 99;
	return lightbox;
}

/* This function will hide the div or the iframe that focus the user on	the list. */
function ghrs_hideLightBox(){
	if((ghrs_isExplorer6() && document.getElementById("iframe_hide_main_window_list")) || (!ghrs_isExplorer6() && document.getElementById("hide_main_window_list"))){
		if(ghrs_isExplorer6()){
			var lightbox = document.getElementById("iframe_hide_main_window_list");
		}
		else{
			var lightbox = document.getElementById("hide_main_window_list");
		}
		lightbox.style.height = 0;
		lightbox.style.width = 0;
	}
}

function ghrs_disableMainPageScroll(){
	window.scrollTo(0,0);
	if(ghrs_isExplorer()) document.body.scroll = "no"; //Internet Explorer
	document.documentElement.style.overflow = 'hidden'; //Common browsers
}

function ghrs_enableMainPageScroll(){
	if(ghrs_isExplorer()) document.body.scroll = "yes"; //Internet Explorer
	document.documentElement.style.overflow = 'auto'; //Common browsers
	window.scrollTo(0,1);
	window.scrollTo(0,0);
}

function ghrs_replaceAttributeSubstring(elem,attribute,toreplace,replace){
	if(String(elem.getAttribute(attribute)).indexOf(toreplace)>0){
		alert(String(elem.getAttribute(attribute)).replace(toreplace,replace));
		ghrs_setAttribute(elem,attribute,String(elem.getAttribute(attribute)).replace(toreplace,replace));
	}
}

function ghrs_getElementClass(elem){
	var elem_class = null;
	if(elem.getAttribute('className')) elem_class = elem.getAttribute('className');
	if(elem.getAttribute('class')) elem_class = elem.getAttribute('class'); 
	return elem_class;
}

/* Chrome and other browsers didn't recognize inputs with index specified with the name of the input. */
function ghrs_findElementInArrayByName(array,name){
	for(var i = 0; i < array.length; i++){
		if(array[i].getAttribute('name')==name) return array[i];
	}	
	return null;
}
