var DHTML = (document.getElementById || document.all || document.layers);

function getObj(name) {
	if (document.getElementById) {
		this.obj = document.getElementById(name);
	} else if (document.all)  {
		this.obj = document.all[name];
	} else if (document.layers)  {
		this.obj = document.layers[name];
	}
	return this.obj;
}

function objExist(name) {
	if (document.getElementById) {
		this.obj = document.getElementById(name);
	} else if (document.all)  {
		this.obj = document.all[name];
	} else if (document.layers)  {
		this.obj = document.layers[name];
	}
	if(this.obj) 
		return true;
	else 
		return false;
}


function divDsp(hideThis,dspState)
{
	if (!DHTML) return;
	var x = new getObj(hideThis);
	x.style.display = (dspState)? (dspState=='inline'?'':dspState) : (x.style.display=='inline'||x.style.display=='') ? 'none' : '';
}


function popup(href,width,height,win_name) {
	    if (!win_name) win_name = 'none';
	    var window_features = "height="+height+",width="+width+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,copyhistory=0,dependent=1,top=100,left=100";
		win = window.open(href,win_name,window_features);
		if(win) {win.focus();}
		return false;
}

function popupResizable(href,width,height,win_name) {
	    if (!win_name) win_name = 'none';
	    var window_features = "height="+height+",width="+width+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,copyhistory=0,dependent=1,top=100,left=100";
		win = window.open(href,win_name,window_features);
		if(win) {win.focus();}
		return false;
}

function selectAll(selectBox){
	for(var i=0;i<selectBox.options.length;i++){
		selectBox.options[i].selected = true;
	}
	return;
}

function unSelectAll(selectBox){
	for(var i=0;i<selectBox.options.length;i++){
		selectBox.options[i].selected = false;
	}
	return;
}

function selectAllChecked(selectBox){
	for(var i=0;i<selectBox.length;i++){
		selectBox[i].checked = true;
	}
	return;
}

function unSelectAllChecked(selectBox){
	for(var i=0;i<selectBox.length;i++){
		selectBox[i].checked = false;
	}
	return;
}

function numOnly(el) {
	return el.value=el.value.replace(/[^0-9.]/g,'');
}

function Highlight(j,color) {
	if(!color) color 	= "#FFF8CB";
	var n 				= null;
	if (j.parentNode && j.parentNode.parentNode) {
		if (j.parentNode.parentNode.nodeName == 'TD') {
			n = j.parentNode.parentNode.parentNode;			
		} else {
			n = j.parentNode.parentNode;
		}
		//alert(n.nodeName);
	}
	else if (j.parentElement && j.parentElement.parentElement) {
		n = j.parentElement.parentElement;
	}
	if (n) {
		n.style.backgroundColor = color; //if (n.className == "row-color") {  } //E3F1FF //C1DDF2 //F4F6D3
	}
}

function Unhighlight(j) {
    var n = null;
	if (j.parentNode && j.parentNode.parentNode) {
		if (j.parentNode.parentNode.nodeName == 'TD') {
			n = j.parentNode.parentNode.parentNode;			
		} else {
			n = j.parentNode.parentNode;
		}		
	}
	else if (j.parentElement && j.parentElement.parentElement) {
		n = j.parentElement.parentElement;
	}
	if (n) {
		n.style.backgroundColor=n.getAttribute('bgcolor'); //if (n.className == "row-color") {  }
	}
}


// AJAX Functionality
function holler(method,url,parameters,callback) {
	method = method.toUpperCase();
	 try{
	    if (window.XMLHttpRequest) {
	        xmlhttp = new XMLHttpRequest();
	    } else if (window.ActiveXObject) {
	        //xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			try {
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
	    }
		
		if(method=="POST"){
			xmlhttp.open(method, url, true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");//application/x-www-form-urlencoded
			xmlhttp.setRequestHeader("Content-length", parameters.length);//parameters.length
			xmlhttp.setRequestHeader("Connection", "close");
			xmlhttp.send(parameters);
		} else{
			xmlhttp.open(method, url+'&'+parameters, true);
			xmlhttp.send(null);
		}
		
     } catch(e){
	 	alert('Error occurred while trying to process your request');
	 	return;
	 }
	
    xmlhttp.onreadystatechange = function () {
	  		if (xmlhttp.readyState == 4 || xmlhttp.readyState == 'complete') {
				if (xmlhttp.status == 200) {
					eval(callback)
				} else{
					alert('An error occurred while trying to return your request. \nError '+xmlhttp.status+': '+xmlhttp.statusText);
					return;
				}
		    }
		};
	//xmlhttp.setRequestHeader("Content-Length", "66");
}

function hollerBack(method,url,parameters,thisObj,LoadingTxt) {
	 //alert(parameters);
	 method = method.toUpperCase();
	 try{
	    if (window.XMLHttpRequest) {
	        xmlhttp = new XMLHttpRequest();
	    } else if (window.ActiveXObject) {
	        //xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			try {
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
	    }
		
		if(method=="POST"){
			xmlhttp.open(method, url, true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");//application/x-www-form-urlencoded
			xmlhttp.setRequestHeader("Content-length", parameters.length);//parameters.length
			xmlhttp.setRequestHeader("Connection", "close");
			xmlhttp.send(parameters);
		} else{
			xmlhttp.open(method, url+'&'+parameters, true);
			xmlhttp.send(null);
		}
		
     } catch(e){
	 	alert('Error occurred while trying to process your request');
	 	return;
	 }
	
    xmlhttp.onreadystatechange = function () {
			
		if (objExist(thisObj)) { 
			var x = new getObj(thisObj);
		}

		if (xmlhttp.readyState == 4 || xmlhttp.readyState == 'complete') {
		
			try {
				x.innerHTML = xmlhttp.responseText;
			} catch (e) {
				alert('An error occurred while trying to return your request. \nError '+e.description);
			}
			
			if (xmlhttp.status != 200) {
				alert('An error occurred while trying to return your request. \nError '+xmlhttp.status+': '+xmlhttp.statusText);
				return;
			}
	    } else{
			
			if(LoadingTxt){
				x.innerHTML = '<div>'+LoadingTxt+'</div>';
			} else {
				x.innerHTML = '<div><img src="img/loading.gif" alt="Loading..." width="220" height="19" border="0" /></div>';
			}
			return;
			//x.innerHTML = '<b>Loading...</b>';
		}
	};
	//xmlhttp.setRequestHeader("Content-Length", "66");
}

function infoDiv(url) {
	holler('POST',url,'layout=blank','swapInfo()');
	return;
}

function swapInfo() {
	var x = new getObj('info');
	x.innerHTML = xmlhttp.responseText;
}

function toggleDiv() {
	
}	

function toggleNoteBox(dsp) {
	noteBoxObj = getObj('noteBox');
	
	if(dsp){
		noteBoxObj.style.visibility = dsp;
	}else{
		if(noteBoxObj.style.visibility=="hidden")
			noteBoxObj.style.visibility = "visible";
		else
			noteBoxObj.style.visibility = "hidden";
	}
}

function checkCustomDenominator(){
	denominatorObj = document.forms['data'].metadata_denominator_id;
	customTextObj  = document.forms['data'].metadata_denominator_custom_text;
	textVal        = denominatorObj.options[denominatorObj.selectedIndex].text;
	
	if(textVal == "-- Custom --"){
		customTextObj.style.display = "";
	}else{
		customTextObj.style.display = "none";
		customTextObj.value = "";
	}
	
}

function expandCollapse(id){
	rowObj    = getObj('ind_'+id);	
	switchObj = getObj('switch_'+id);
	
	if(rowObj.style.display=="none"){
		rowObj.style.display = "";
		switchObj.src = "images/btn_close.gif";
	}else{
		rowObj.style.display = "none";
		switchObj.src = "images/btn_open.gif";
	}
}

function checkUserType(obj){
	objects = new Array();
	objects[0] = getObj('date_begin1');
	objects[1] = getObj('date_end1');
	objects[2] = getObj('date_begin2');
	objects[3] = getObj('date_end2');
		
	if(obj.value==3){
		for(i=0;i<objects.length;i++)
			objects[i].style.display='';
	}else{
		for(i=0;i<objects.length;i++)
			objects[i].style.display='none';
	}
}
