//
//  Functions "id & ip Romania"
//  Agachi Valentin
//

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.op = (agent.indexOf("opera") != -1);
 this.ie = (!this.op && (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.win = (agent.indexOf("win")!=-1);
 this.mac = (agent.indexOf("mac")!=-1);
 this.unix = (agent.indexOf("x11")!=-1);
}
var br = new is();



// Other funcs

document.imgsw = new Array;
document.imgp = new Array;

function getObj(n, d) { 
	var p,i,x; if(!d) d=document; 
	if(!x && document.getElementById) { return document.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 ImgPre() {
  var d=document; 
		if(!d.imgp) d.imgp=new Array();
		var i, j=d.imgp.length, a=ImgPre.arguments; 
		for(i=0; i<a.length; i++)
			if (a[i].indexOf("#")!=0) { 
				d.imgp[j]=new Image; d.imgp[j++].src=a[i];
			}
}

function ImgS() {
  var i,j=document.imgsw.length,x,a=ImgS.arguments;
	for(i = 0; i < (a.length-1); i += 2) {
		if (typeof(a[i]) == 'string')	{
			x = getObj(a[i]);
		} else {
			x = a[i];
		}
		if (x != 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;
}

function showLayer() {
  var i,p,v,obj,args=showLayer.arguments;
  for (i=0;i<(args.length-1);i+=2) 
		if ((obj=getObj(args[i]))!=null) { 
			v=args[i+1];
			if (obj.style) { 
				obj=obj.style; 
				v=(v=='show')?'visible':(v='hide')?'hidden':v; 
			}
			obj.visibility=v; 
		}
}



/**
 * Location functions
 */

function go(link, target) {
	if (target == '_blank') {
		popup(link, 700, 550, 'go_win');
	} else {
		if (!target) {
			target = document;
		}
		target.location = link;
	}
}

function win_new(link, width, height) {
	var iw = width + 20;
	var ih = height + 28;
	var left = (screen.width - width) / 2;
	var top = (screen.height - height) / 2;
	var settings = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width='+width+',height='+height+',left='+left+',top='+top;
	icd = window.open(link, 'icd', settings);
	icd.focus();
}



/**
 * Forms stuff
 */

function get_checked_no(form, pre) {
	var c = 0;
	var f = getObj(form);
	for (i = 0; i < f.elements.length; i++) {
		a = f.elements[i];
		if ((a.type=='checkbox')&&(a.name.indexOf(pre)==0)&&(a.checked)) { c++; }
	}
	return c;
}

function update_submit(f) {
	var n = get_checked_no(f, 'del_');
	disabled = (n == 0);
	return (n == 0);
}

function check_uncheck(s, f) {
	var form = getObj(f);
	var o = getObj(s, getObj(f));
	o.checked = !o.checked;
	return update_submit(f);
}

function verify_checkboxes(form, pre, conf) {
	var c = get_checked_no(form, pre);
	if (c <= 0) return false;
	else return window.confirm(conf);
}

function form_check(elems) {
	for (i = 0; i < elems.length; i++) {
		o = getObj(elems[i]);
		if (o.value.length == 0) {
			alert('Nu ati completat formularul corect!');
			o.focus();
			return false;
		}
	}
	return true;
}

// Disable funcs

var disabled = 1;
function dis_event() {
	if (!disabled) ImgS(dis_event.arguments[0],dis_event.arguments[1]);
}

function dis_update() {
	ImgRest(); 
	obj = getObj('delete');
	if (disabled) obj.src = obj._disabled;
	else obj.src = obj._normal;
}

function get_disabled() {
	return disabled;
}

function submit_form() {
	var f = getObj(submit_form.arguments[0]);
	getObj('op', f).value = submit_form.arguments[1]; 
	f.submit(); 
}



/**
 * Change attributes
 */

function change_class(obj, new_class) {
	var class_field = '';
	if (br.ie) {
		class_field = 'className';
	} else {
		class_field = 'class';
	}
	if (typeof(window.opera) == 'undefined' && typeof(obj.getAttribute) != 'undefined') {
		obj.setAttribute(class_field, new_class, 0);
	} else {
		obj.style.className = new_class;
	}
}

function changeAttribute(objName, attrName, attrValue) {
	obj = getObj(objName);
	if (typeof(obj.setAttribute) != 'undefined') {
		obj.setAttribute(attrName, attrValue, 0);
	}
}

/**
 * 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 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') {
        currentColor = cells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // ... with other browsers
    else {
        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;
}



/**
 * Page onload functions handling
 */

var icd_onload_funcs=new Array();

function icd_onload_register(func) {
	if (func != null) {
		icd_onload_funcs[icd_onload_funcs.length] = func;
	}
}

function icd_onload() {
	for (i=0; i < icd_onload_funcs.length; i++) {
		icd_onload_funcs[i]();
	}
}



/**
 * Menu hover
 */

function menuOver(o, col) {
//	alert(typeof(o));
//	o.setAttribute('color', 'green', 0);
//	o.style.color = col;
//	var p = o.parentNode.parentNode;
//	while (p.tagName != 'TD') {
//		p = p.parentNode;
//	}
//	p.style.backgroundColor = 'white';
//	changeAttribute(o, 'color', col);
	highlight(o, 'over', null);
	var a = o.children(1).children(0);
	a.style.color = 'white';
}

function menuOut(o, col) {
	highlight(o, 'out');
	var a = o.children(1).children(0);
	a.style.color = 'black';
}

function loadLang(lang) {
	var c = frames['contents'];
	var s = c.document.location.toString();
	s = s.substr(0, s.length - 4) + '.newLang.' + lang + '.htm';
	alert(s);
	document.location = s;
}




/* form Radio mouseovers */

var radioSelected = null;
function radioOver(obj) {
	if (radioSelected != null) {
		return false;
	}
	if (obj.tagName == 'INPUT') {
		var sFile = icdUrl + 'upload/cerere/' + obj.value + '.JPG';
		var oImg = getObj('formImg');
		oImg.src = sFile;
	} else if (obj.tagName == 'LABEL') {
		var o = getObj(obj.htmlFor);
		radioOver(o);
	}
}

function radioClick(obj) {
	if (obj.tagName == 'INPUT') {
		radioSelected = obj;
		var sFile = icdUrl + 'upload/cerere/' + obj.value + '.JPG';
		var oImg = getObj('formImg');
		oImg.src = sFile;
	} else if (obj.tagName == 'LABEL') {
		var o = getObj(obj.htmlFor);
		radioClick(o);
	}
}



function popupImg(sPath, sPicURL, sTitle) { 
	window.open(sPath+"lib/image.popup.htm?"+sPicURL+'?'+sTitle, "", "resizable=1,height=200,width=200"); 
}