//
//  Functions "id & ip Romania"
//  Agachi Valentin
//

var brIE = ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) );



// 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];
		}
	}
	return false;
}

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')?'block':(v='hide')?'none':v; 
			}
			obj.display=v; 
		}
}



/**
 * Location functions
 */

function go(link, target, w, h) {
	if (target == '_blank') {
		if (!w) w = 700; if (!h) h = 550;
		popup(link, w, h);
	} else {
		if (!target) target = document;
		target.location = link;
	}
}

function popup(link, w, h) {
	var left = (screen.width - w) / 2;
	var top = (screen.height - h) / 2;
	var settings = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width='+w+',height='+h+',left='+left+',top='+top;
	icd = window.open(link, 'icd', settings);
	icd.focus();
}
function win_new(l, w, h) { popup(l, w, h); }



/**
 * 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;
}


/**
 * 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);
	}
}

/**
 * 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]();
	}
}



function popupImg(sPath, sPicURL, sTitle) { 
	window.open(sPath+"lib/image.popup.htm?"+sPicURL+'?'+sTitle, "", "resizable=1,height=200,width=200"); 
}



/** 
 * Event handling
 */ 

function attachEvent2(el, eventName, func) {
	if (el.attachEvent) { // IE
		el.attachEvent("on" + eventName, func);
	} else if (el.addEventListener) { // Gecko / W3C
		el.addEventListener(eventName, func, true);
	} else {
		el["on" + eventName] = func;
	}
};

function removeEvent2(el, eventName, func) {
	if (el.detachEvent) { // IE
		el.detachEvent("on" + eventName, func);
	} else if (el.removeEventListener) { // Gecko / W3C
		el.removeEventListener(eventName, func, true);
	} else {
		el["on" + eventName] = null;
	}
};

function eventGetElement(ev) {
	if (brIE) {
		return window.event.srcElement;
	} else {
		return ev.currentTarget;
	}
};

/**
 * Menu hover
 */

var oPreloads = new Object();

function icdImgHover(ev) {
	var oImg = eventGetElement(ev);
	oImg.src = oPreloads['o' + oImg.id].src;
}
function icdImgNormal(ev) {
	var oImg = eventGetElement(ev);
	oImg.src = oPreloads['n' + oImg.id].src;
}

function icdImgSrcGetHover(sSrc) {
	var aSrc = sSrc.split('.');
	aSrc[aSrc.length] = aSrc[aSrc.length - 1];
	aSrc[aSrc.length - 2] = 'o';
	return aSrc.join('.');
}
function icdImgSrcGetNormal(sSrc) {
	var aSrc = sSrc.split('.');
	aSrc[aSrc.length - 2] = aSrc[aSrc.length - 1];
	aSrc[aSrc.length - 1] = null;
	return aSrc.join('.');
}

function icdImgButLoadArray(aImgs) {
	for (var i = 0; i < aImgs.length; i++) {
		if (aImgs[i].className.length) {
			if (/imgButHover/.test(aImgs[i].className)) {
				oPreloads['n' + aImgs[i].id] = new Image;
				oPreloads['n' + aImgs[i].id].src = aImgs[i].src;
				oPreloads['o' + aImgs[i].id] = new Image;
				oPreloads['o' + aImgs[i].id].src = icdImgSrcGetHover(aImgs[i].src);
				attachEvent2(aImgs[i], 'mouseover', icdImgHover);
				attachEvent2(aImgs[i], 'mouseout', icdImgNormal);
			}
			if (/imgButSelected/.test(aImgs[i].className)) {
				aImgs[i].src = icdImgSrcGetHover(aImgs[i].src);
			}
		}
	}
}

function icdImgButLoad() {
	if (document.getElementsByTagName) {
		var aImgs = document.getElementsByTagName('IMG');
	} else if (document.all) {
		var aImgs = document.all.tags('IMG');	
	}
	if (typeof(aImgs) != 'undefined') {
		icdImgButLoadArray(aImgs);
	}
	if (document.getElementById) {
		var aInputs = document.getElementsByTagName('INPUT');
	} else if (document.all) {
		var aInputs = document.all.tags('INPUT');	
	}
	if (typeof(aInputs) != 'undefined') {
		icdImgButLoadArray(aInputs);
	}
}
icd_onload_register(icdImgButLoad);
