/**
 * Browser detect
 */

function is() {
 var agent = navigator.userAgent.toLowerCase();
 this.major = parseInt(navigator.appVersion);
 this.minor = parseFloat(navigator.appVersion);
 this.ns = ((agent.indexOf('mozilla')!=-1) && ((agent.indexOf('spoofer')==-1) && (agent.indexOf('compatible')==-1)));
 this.ns2 = (this.ns && (this.major == 3));
 this.ns3 = (this.ns && (this.major == 3));
 this.ns4b = (this.ns && (this.minor < 4.04));
 this.ns4 = (this.ns && (this.major == 4));
 this.ns6 = (this.ns && (this.major >= 5));
 this.ie = (agent.indexOf("msie") != -1);
 this.ie3 = (this.ie && (this.major == 2));
 this.ie4 = (this.ie && (this.major == 4));
 this.ie5 = (this.ie && (this.major >= 5));
 this.moz = (agent.indexOf('Gecko')!=-1);
 this.opera = (agent.indexOf("opera") != -1);
 this.win = (agent.indexOf("win")!=-1);
 this.mac = (agent.indexOf("mac")!=-1);
 this.unix = (agent.indexOf("x11")!=-1);
}
var br = new is();

var DHTML = (document.getElementById || document.all || document.layers);

/**
 * Image preloading & handling
 */

document.imgsw = new Array;

function imgS() {
  var i, j=0, x, a=imgS.arguments;
	for(i=0; i < (a.length-1); i+=2) {
		if ((x=findObj(a[i])) != null) {
			document.imgsw[j++]=x; 
			if (!x.oSrc) x.oSrc=x.src; 
			x.src=a[i+1];
		}
	}
}

function imgRest() {
  var i, x, a=document.imgsw;
	for(i=0; a&&i<a.length&&(x=a[i])&&x.oSrc; i++) x.src=x.oSrc;
	document.imgsw = new Array;
}



/**
 * opens a link in the target window
 */

function go(link, target) {
	if (target == '_blank') {
		window.open(link);
		return true;
	}
	if (!target) {
		target = document;
	}
	target.location = link;
}

/**
 * Form validation functions
 */

function validateLogin() {
	var o = null;
	o = findObj('login_user');
	if (o.value=='') { 
		alert("You must type a username, a password and the code!"); 
		o.focus(); 
		o.select(); 
		return false;
	}
	o = findObj('login_pass');
	if (o.value=='') { 
		alert("You forgot the password!"); 
		o.focus(); 
		o.select(); 
		return false;
	}
	o = findObj('login_code');
	code = o.value;
	if (o.value=='') { 
		alert("You forgot the code!"); 
		o.focus(); 
		o.select();
		return false;
	}
/*	o = findObj('login_code_hidden');	
	if (o.value!=calcMD5(code)) { 
		alert("You suplied a wrong code!"); 
		o.focus(); 
		o.select(); 
		return false;
	}*/
	return true;
}

function check_old_pass() {
	p_old = findObj('oldpass');
	if (p_old.value == '') {
		p_old.focus();
		return false;
	} else return true;
}

function check_new_pass() {
	p_new=findObj('newpass');
	p_re=findObj('repass');
	if (p_new.value!=p_re.value) {
		p_new.value='';
		p_new.focus();
		p_re.value='';
		return false;
	} else if (p_new.value=='') {
		p_new.focus();
		return false;
	} else return true;
}

function check_add_user(f)
{
	if (findObj('newname', f).value=='') {
		findObj('newname', f).focus();
		return false;
	} else return true;
}

function empty()
{	
	if ((f1.addname.value=='')||(f1.addsurname.value=='')) {
		alert("Please enter a name and surname!");
		return false;
	}
	else return true;
}

function checkreport()
{
	if ((!document.all.r1.checked)&&(!document.all.r2.checked)&&(!document.all.r3.checked)&&(!document.all.r4.checked))
		return false;
	if ((document.all.r2.checked)&&((f1.bet1.value.length==0)||(f1.bet2.value.length==0)))
		return false;
	if ((document.all.r3.checked)&&(f1.gr.value.length==0))
		return false;
	if ((document.all.r4.checked)&&(f1.sm.value.length==0))
		return false;
	if ((!document.all.r5.checked)&&(!document.all.r6.checked))
		return false;
	return true;
}

/**
 * Misc Functions
 */

function show_loading(div, flag) 
{ 
	if (!DHTML) return;
	var x = findObj(div); 
	x.visibility = (flag) ? 'visible':'hidden'; 
	return true; 
}

function ReplaceContent(layerName, Continut) {
	if(br.ie) { findObj(layerName).innerHTML = Continut }
	if(br.ns) {
		with(document.layers[layerName].document) { 
			open();
			write(Continut);
			close();
		}
	}
}

function findObj(n, d) {
	var p,i,x; if(!d) d=document; 
	if(!x && d.getElementById) { return d.getElementById(n); }
	if(!(x=d[n])&&d.all) { return d.all[n]; } 
	if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p); }
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for (i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
	return x;
}

function showHideLayer() {
	var i,p,v,obj,args=showHideLayer.arguments;
	for (i=0; i<(args.length-1); i+=2) 
		if ((obj=findObj(args[i]))!=null) { 
			v=args[i+1];
			if (obj.style) { 
				obj=obj.style; 
				v=(v=='show')?'visible':(v=='hide')?'hidden':v;
			}
			obj.visibility=v; 
		}
}



/**
 * Checkboxes functions
 */

function checkbox_click(obj) {
	obj_parent = obj;
	while (obj_parent.tagName.toLowerCase() != 'tr') {
		obj_parent = obj_parent.parentNode;
	}
	if (obj.checked) {
		vcat_highlight(obj_parent, 'select', true);
	} else {
		vcat_highlight(obj_parent, 'out', true);
	}
}

function checkbox_all(f, prefix, sender, highlight_row) {
	var form = findObj(f);
	if (typeof sender != 'object') {
		var obj  = findObj(sender);
	} else {
		obj = sender;
	}
	var elem = form.elements;
	var s = '';
	var i = 0;
	for (i = 0; i < elem.length; i++) {
		if ((elem[i].name.indexOf(prefix) == 0) && (elem[i].type == 'checkbox') && (elem[i] != obj)) {
			elem[i].checked = obj.checked;
			if (typeof(highlight_row) != 'undefined' && highlight_row) {
				checkbox_click(elem[i]);
			}
		}
	}
}



/**
 * Page onload functions handling
 */

var vcat_onload_funcs=new Array();

function vcat_onload_register(func) {
	if (func != null) {
		vcat_onload_funcs[vcat_onload_funcs.length] = func;
	}
}

function vcat_onload() {
	for (i=0; i<vcat_onload_funcs.length; i++) {
		vcat_onload_funcs[i]();
	}
}

/**
 * Highlighting
 */

/**
 * Highlights a row according to an action passed (adapted from PMA's setPointer() function)
 *
 * @param   object    the table row
 * @param   string    the action calling this script (over, out or click)
 *
 * @return  boolean  whether pointer is set or not
 */
function vcat_highlight(obj_row, action, force)
{
    var cells = null;
	if (typeof(force) == 'undefined') force = false;

    // 1. Pointer and mark feature are disabled or the browser can't get the row -> exits
	if (typeof(obj_row.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        cells = obj_row.getElementsByTagName('td');
    } else if (typeof(obj_row.cells) != 'undefined') {
        cells = obj_row.cells;
    } else {
        return false;
    }

    var rowCellsCnt  = cells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
	var action_index = null;
	var action_color = null;

    // 3. Gets the current color...
    // ... with DOM compatible browsers except Opera that does not return valid values with "getAttribute"
//    if (typeof(window.opera) == 'undefined' && typeof(cells[0].getAttribute) != 'undefined') {
    if (typeof(cells[0].getAttribute) != 'undefined') {
        currentColor = cells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // ... with other browsers
    else {
		alert(cells[0].getAttribute('bgcolor'));
        currentColor = cells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

	// Initialize the _state and _default_color private members
	if (typeof(obj_row._state) == 'undefined') {
		obj_row._state = 0;
		obj_row._default_color = currentColor;
	}
	if (typeof(obj_row._default_color) == 'undefined') {
		obj_row._default_color = currentColor;
	}

	// Calculate the current action index and color
	if (action == 'out') {
		action_color = obj_row._default_color;
		action_index = 0;
	} else {
		for (i=0; i<row_colors.length; i+=2) {
			if (action == row_colors[i]) {
				action_index = (i / 2) + 1;
				action_color = row_colors[i + 1];
			}
		}
	}

	// 4. Define the new color... depending on teh current state of the row
	switch (obj_row._state) {
	// 4.1 state is normal
		case 0: 
			newColor = action_color;
			obj_row._state = action_index;
			break;
	// 4.2 state is over
		case 1:
			newColor = action_color;
			obj_row._state = action_index;
			break;
	// 4.2 state is clicked
		case 2:
			if (force) {
				newColor = action_color;
				obj_row._state = action_index;
			} else {
				if (action_index == 2) {
					newColor = obj_row._default_color;
					obj_row._state = 0;
				} else if (action_index == 3) {
					newColor = action_color;
					obj_row._state = action_index;
				}
			}
			break;
	// 4.3 state is selected
		case 3: 
			if (force) {
				newColor = action_color;
				obj_row._state = action_index;
			} else {
				if (action_index == 3) {
					newColor = obj_row._default_color;
					obj_row._state = 0;
				}
			}
			break;
	} // end 4

	// 5. Sets the new color...
    if (newColor) {
        var c = null;
        // ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                cells[c].setAttribute('bgColor', newColor, 0);
            }
        }
        // ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                cells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

	return true;
}

function changeClass(obj, new_class) {
	if (typeof(obj) == 'string') {
		obj = findObj(obj);
	}
	obj.className = new_class;
}



function setFocusIndex() {
/*
	// search for a tabIndexed field to focus on
	for(var firstIndex=1; firstIndex < 5; firstIndex ++ ){
		var nextIndex = firstIndex;
		for (var frm = 0; frm < document.forms.length; frm++) {
			for (var fld = 0; fld < document.forms[frm].elements.length; fld++) {
				var elt = document.forms[frm].elements[fld];
				if ( elt.tabIndex != nextIndex ) continue;
				if ((elt.type == "text" || elt.type == "textarea" || elt.type == "password")) {
					elt.focus();
					if (elt.type == "text") {
						elt.select();
					}
					return true;
				} else {
					nextIndex++;
					fld = 0;
				}
			}
		}
	}
*/
	if (document.getElementsByTagName) {
		var elems = document.getElementsByTagName('input');
		for (var fld = 0; fld < elems.length; fld++) {
			var elt = elems[fld];
			if (elt.type == "text" || elt.type == "textarea" || elt.type == "password") {
				elt.focus();
				if (elt.type == "text") {
					elt.select();
				}
				return true;
			}
		}
	} else {
		// failed to find a tabIndexed field, try to find the field based on it's natural position.
		for (var frm = 0; frm < document.forms.length; frm++) {
			for (var fld = 0; fld < document.forms[frm].elements.length; fld++) {
				var elt = document.forms[frm].elements[fld];
				if (elt.type == "text" || elt.type == "textarea" || elt.type == "password") {
					elt.focus();
					if (elt.type == "text") {
						elt.select();
					}
					return true;
				}
			}
		}
	}
	return true;
}

function setNamedFocus(element_name) {
	var elt = findObj(element_name);
	if (typeof(elt) != 'undefined') {
		elt.focus();
		if (elt.type == "text") {
			elt.select();
		}
		return true;
	}
	for (var frm = 0; frm < document.forms.length; frm++) {
		for (var fld = 0; fld < document.forms[frm].elements.length; fld++) {
			var elt = document.forms[frm].elements[fld];
			if (elt.name == element_name) {
				elt.focus();
				if (elt.type == "text") {
					elt.select();
				}
				return true;
			}
		}
	}
	return true;
}




function cloneElement(readId, writeId, counter, setFocus) {		 
	var newFields = document.getElementById(readId).cloneNode(true);
	newFields.id = '';
	newFields.style.display = 'block';
	var newField = newFields.childNodes;
	for (var i=0;i<newField.length;i++) {
		var theName = newField[i].name
		if ((theName)&&(theName != 'undefined')) {
			newField[i].name = theName + counter;
			if (i==0)
			{
				b = newField[i];
			}
		}
	}
	var insertHere = document.getElementById(writeId);
	insertHere.parentNode.insertBefore(newFields,insertHere);
	if ((setFocus)||(setFocus == undefined))
	{
		b.focus();
	}
}




function disableAll(f, prefix, sender) {
	var form = findObj(f);
	var obj;
	if (typeof sender == 'object') obj = sender; else obj  = findObj(sender);
	var elem = form.elements;
	var s = '';
	var i = 0;
	for (i = 0; i < elem.length; i++) {
		if ((elem[i].id.indexOf(prefix) == 0) && (elem[i].type == 'checkbox') && (elem[i] != obj)) {
			elem[i].checked = false;
			elem[i].disabled = obj.checked;
		}
	}
}


function checkAll(obj, name, id)
{
	var checks = document.getElementsByName(name);

	//If no id given, we'll asume that we want all the boxes checked/unchecked.
	if(typeof id == "undefined")
	{
		for(var x = 0; x < checks.length; x++)
		{
			alert(checks[x].id);
			checks[x].checked = obj.checked;
		}
	}
	//Else lets see wether to check or uncheck the "master" checker
	else
	{
		var checkall = document.getElementById(id);

		//If we uncheck a cluster then lets uncheck the master
		if(checkall.checked == true && obj.checked == false)
		{
			checkall.checked = false;
		}
		//If we check a cluster, lets see if we should check the master too
		else if(obj.checked == true)
		{
			var isChecked = true;
			for(var x = 0; x < checks.length; x++)
			{
				if(checks[x].checked != obj.checked)
				{
					isChecked = false;
					break;
				}
			}

			//If bool is true, all clusters are checked, and we should check the master
			checkall.checked = isChecked;
		}
	}
}

function checkform_subjects_rating( f )
{
	

	elem = document.getElementById(f).elements;
	for (i = 0; i < elem.length; i++) {		
		if ((elem[i].type == 'checkbox')&&(elem[i].checked)) {	
				return true;
			}
	}
	
	alert('You must select at least one option!');	
	
	return false;

}