//

//  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];

		}

	}

}



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 checkAll(pre, b) {

	var c = document.getElementsByTagName('input');

	for (i = 0; i < c.length; i++) {

		if ((c[i].type=='checkbox') && (c[i].name.indexOf(pre)==0)) {

			c[i].checked = b;

		}

	}

}



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 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;

}







function popupImg(sPath, sPicURL, sTitle) { 

	window.open(sPath+"lib/image.popup.htm?"+sPicURL+'?'+sTitle, "", "resizable=1,height=200,width=200"); 

}







/** 

 * Event handling

 */ 



function attachEvent(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 removeEvent(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);

				attachEvent(aImgs[i], 'mouseover', icdImgHover);

				attachEvent(aImgs[i], 'mouseout', icdImgNormal);

			}

			if (/imgButSelected/.test(aImgs[i].className)) {

				aImgs[i].src = icdImgSrcGetHover(aImgs[i].src);

			}

		}

	}

}



function icdImgButLoad() {

	if (document.getElementById) {

		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);



/* gallery preload */



function imgPreload() {

	if (document.getElementsByTagName) {

		var aImgs = document.getElementsByTagName('IMG');

	} else if (document.all) {

		var aImgs = document.all.tags('IMG');	

	}

	var re = new RegExp('^(.+)/upload/([^\.]+)_tmb\.(.+)$');

	if (typeof(aImgs) != 'undefined') {

		for (var i = 0; i < aImgs.length; i++) {

			if (aImgs[i].src.length) {

				s = aImgs[i].src;

				sNew = s.replace(re, '$1/upload/$2.$3');

				if (sNew != s) {

					ImgPre(sNew);

				}

			}

		}

	}

}

//icd_onload_register(imgPreload);



function galleryGo(x) {

	var a = document.getElementById('galA');

	var img = a.childNodes[0];

	var n = galleryIndex + x;

	if (n < 0 && n >= gallery.length) return false;

	img.src = icdUrl + 'upload/gal.201513/' + gallery[n];

//	document.getElementById('galPrev').className = ((n == 0) ? 'hide' : '');

//	document.getElementById('galNext').className = ((n == gallery.length - 1) ? 'hide' : '');

	galleryIndex = n;

}

function galleryTimer() {

	if (galleryIndex == gallery.length - 1) {

		galleryGo(- gallery.length + 1);

	} else{ 

		galleryGo(1);

	}

}





/* IE :hover */



function sfHover() {



	var sfEls = document.getElementById("menuDD").getElementsByTagName("li");



	for (var i=0; i<sfEls.length; i++) {



		sfEls[i].onmouseover=function() {



			if (this.className == 's') this.className += 'hover hover';



			else this.className+=" hover";



		}



		sfEls[i].onmouseout=function() {



			if (this.className == 'shover hover') this.className='s';



			else this.className=this.className.replace(new RegExp(" hover\\b"), "");



		}



	}



}



if (brIE) icd_onload_register(sfHover);