/*
* $Id: functions.js,v 1.17 2005/10/28 08:32:05 razvan.stanga Exp $
*/

_browser = getBrowser ();

function confirmSubmit (msg) {
	var agree=confirm(msg);
	if (agree) {
		return true ;
	} else {
		return false ;
	}
}

function is_empty (str) {
    return str.search("^[ \t\n\r]*$")>-1 ? true : false;
}

function is_int (str) {
    return str.search("^[0-9]*$")>-1 ? true : false;
}

function validField (str, msg) {
    if ( is_empty(str) ) {
		alert (msg);
		return false;
    }
    return true;
}

function multipleSelect ( arr, f, field ) {

    eval ('var val = document.forms.'+f+'.'+field+'.length');
	for ( j=0 ; j < val ; j++ ) {
	    if ( arr == 'all' ) {
	    	eval ('document.forms.'+f+'.'+field+'[j].selected = true;');
	    } else {
	        eval ('document.forms.'+f+'.'+field+'[j].selected = false;');
	    }
	}
	if ( arr == 'all' ) {
	    return "";
	}

	var b = arr.split (',');
	for ( i=0 ; i < b.length ; i++ ) {
	    for ( j=0 ; j < val ; j++ ) {
	        if ( b[i] == eval ('document.forms.'+f+'.'+field+'[j].value') ) {
	    		eval ('document.forms.'+f+'.'+field+'[j].selected = true');
	        }
	    }
	}
}

function setValue (field, f, val) {
	eval ("document.forms."+f+"."+field+".value='"+val+"'");
}

function getLength (field, f) {
	x = eval ("document.forms."+f+"."+field+".length");
	return x;
}

function selectOption (field, f ,i) {
	eval ("document.forms."+f+"."+field+"["+i+"].selected=true;");
}

function getValue (field, f) {
	eval ("var x = document.forms."+f+"."+field+".value");
	return x;
}

function getOption (n, f, o) {
	var selOp = eval ('document.forms.'+f+'.'+n+';');
	if ( o == 't' ) {
		return selOp.options[selOp.selectedIndex].text;
	} else {
     	return selOp.options[selOp.selectedIndex].value;
   	}
}

function getOptionIndex (n, f, v, o) {
	var selOp = eval ('document.forms.'+f+'.'+n+';');
	if ( o == 't' ) {
		return selOp.options[v].text;
	} else {
     	return selOp.options[v].value;
   	}
}

function doSubmit ( f ) {
	eval ('document.forms.'+f+'.submit ();');
}

function checkboxAll (field, f, act, s, e) {
	for ( i=s ; i < e ; i++ ) {
	    if ( act == 'c' ) {
	    	eval ('document.forms.'+f+'.'+field+'['+i+'].checked = true;');
	    } else if ( act == 'u' ) {
	    	eval ('document.forms.'+f+'.'+field+'['+i+'].checked = false;');
	    }
	}
}

function inputAll (field, f, val, s, e) {
	for ( i=s ; i < e ; i++ ) {
    	eval ('document.forms.'+f+'.'+field+'['+i+'].value = "'+val+'";');
	}
}

function checkBox (f, field, m) {

	var checkbox = 	eval ('document.forms.'+f+'.'+field);
	var len = checkbox.length;

	var j = 0;
	for ( i=0 ; i < len ; i++ ) {
	    if ( checkbox[i].checked == true ) {
			j++;
			if ( j > m ) {
			    checkbox[i].checked = false;
			    alert ("Nu puteti selecta mai mult de "+m+" optiuni");
			    return false;
			}
	    }
	}
}

function addOption (n, f, t, v) {
	var selOp = eval ('document.forms.'+f+'.'+n+'');
	var len = selOp.options.length;
	selOp.options[len] = new Option (t, v);
	selOp[len].selected = true;
}

function removeOption (n, f, op) {
	var selOp = eval ('document.forms.'+f+'.'+n+'');
	for ( i=0;i<selOp.length;i++ ) {
		if ( i == op ) {
			selOp[i] = null;
			break;
		}
	}
}

function checkCookie () {

var acceptsCookies = false;
if(document.cookie == '') {
    document.cookie = 'acceptsCookies=yes';
    if(document.cookie.indexOf('acceptsCookies=yes') != -1) {
	acceptsCookies = true;
    }
    return acceptsCookies;
} else {
  acceptsCookies = true;
  return acceptsCookies;
}
}


function setCookie (name, value, hours, path, domain, secure) {
    if ( checkCookie () == true ) {
        name = 'b'+name;
	var not_NN2 = (navigator && navigator.appName
		       && (navigator.appName == 'Netscape')
		       && navigator.appVersion
		       && (parseInt(navigator.appVersion) == 2))?false:true;

	if(hours && not_NN2) {
	    if ( (typeof(hours) == 'string') && Date.parse(hours) ) {
		var numHours = hours;
	    } else if (typeof(hours) == 'number') {
		var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
	    }
	}
	document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':'');
    }
}


function getCookie(name) {
    if(document.cookie == '') {
		return false;
    } else {
		var firstChar, lastChar;
		var theBigCookie = document.cookie;
		firstChar = theBigCookie.indexOf(name);
		var NN2Hack = firstChar + name.length;
		if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) {
	    	firstChar += name.length + 1;
	    	lastChar = theBigCookie.indexOf(';', firstChar);
	    	if(lastChar == -1) lastChar = theBigCookie.length;
	    	return unescape(theBigCookie.substring(firstChar, lastChar));
		} else {
	    	return false;
		}
    }
}

function writeToLayer (l, txt) {
	
	if (document.layers) {
		eval ( "var lay = document.layers."+l+"" );
		lay.innerHTML = txt;
	} else if (document.all) {
		eval ( "var lay = document.all."+l+"" );
		lay.innerHTML = txt;
	} else {
		var lay = document.getElementById (l);
		lay.innerHTML = txt;
   	}
}

function getFromLayer (l) {

	if (document.layers) {
		eval ( "var lay = document.layers."+l+";" );
		return lay.innerHTML;
	} else if (document.all) {
		eval ( "var lay = document.all."+l+";" );
		return lay.innerHTML;
	} else {
		var lay = document.getElementById (l);
		return lay.innerHTML;
   	}
}

function getBrowser () {
	var browserName = navigator.appName;
	if ( browserName.search ('Internet Explorer') != -1  ) {
		return 'Internet Explorer';
	} else if ( browserName.search ('Netscape') != -1 ) {
        return 'Netscape';
	} else if ( browserName.search ('Opera') != -1 ) {
        return 'Opera';
	} else if ( browserName.search ('Firefox') != -1 ) {
        return 'Firefox';
	} else {
		return 'Other';
	}
}

function setVisibility (n, act) {
	if ( getBrowser () == 'Internet Explorer' ) {
        var selOp = eval ("document.all.div_"+n+"");
	} else {
        var selOp = eval ("document.getElementById ('div_"+n+"');");
	}
	//
	//var selOp = eval ("document.all.div_"+n+"");
	if ( act == 't' ) {
       	selOp.style.display='';
	} else if ( act == 'f' ) {
		selOp.style.display='none';
	}
}

function toNumber ( nr ) {
	return parseFloat ( nr );
}

function addBR ( field, f ) {
	
	txt = getValue (field, f);
	i = 1;
	while ( txt.search ('[:::]') != -1 ) {
		txt = txt.replace ("[:::]", '\r\n');
		i++;
		if ( i >= 150 ) {
			break;
		}
	}
	setValue (field, f, txt);
}

function doOpenPopup (url, wid, hei, n) {
	if ( n == "") {
		n = "popup_url";
	}
    var w = (window.screen.width-wid)/2;
	var h = (window.screen.height-hei)/2;
	window.open(url, n, 'scrollbars=yes,resizable=no,menubar=no,statusbar=no,toolbar=no, width='+wid+', height='+hei+', left='+w+',top='+h+',screenX='+w+',screenY='+h+' ');
}

function deActivate ( field, f, val ) {
	if ( val ) {
		eval ('document.forms.'+f+'.'+field+'.disabled=false;');
	} else {
		eval ('document.forms.'+f+'.'+field+'.disabled=true;');
	}
}

function registerPass (f, field1, field2) {
	var p1 = eval ('document.forms.'+f+'.'+field1+'.value;');
	var p2 = eval ('document.forms.'+f+'.'+field2+'.value;');
	if ( p1 != "" && p2 != "" ) {
		if ( p1 == p2 ) {
			return 1;
		} else {
			return 0;
		}
	} else {
		return 2;
	}
}

function isEmail (email) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) {
		return true;
	} else {
		alert ("Va rugam sa introduceti o adresa de email valida");
		return false;
	}
}

function addToField (f, field, val) {
    var fField = eval ('document.forms.'+f+'.'+field);
    fField.value += val;
}

function toWindow (f, e, v) {
	eval ("window.opener.document."+f+"."+e+".value += '"+v+"'");
}

function showmenu (id, val) {
	if (document.getElementById) {
		if(document.getElementById("menu_"+id)) {
			var elem = document.getElementById("menu_"+id);
			if ( val == 0 ) {
				elem.style.display = "none";
			} else if ( val == 1 ) {
                elem.style.display = "";
			}
		}
		return false;
	}
}




function showmenuTimer ( id ) {
    setTimeout( 'showmenu('+id+')', 1000);
}

function goTO ( url, val ) {
	if ( val ) {
		window.location=url;
	}
}

function changeTable ( id ) {
	var div 	= document.getElementById("table_div_"+id);
	var img 	= document.getElementById("table_img_"+id);

	if ( div.style.display == "none" ) {
		div.style.display = "";
		img.src = "styles/style1/images/minus.gif";
		setCookie (id, 'y');
	} else {
        div.style.display = "none";
        img.src = "styles/style1/images/plus.gif";
        setCookie (id, 'n');
	}
}


function showMore ( id ) {
	var div 	= document.getElementById("div_"+id);

	if ( div.style.display == "none" ) {
		div.style.display = "";
	} else {
        div.style.display = "none";
	}
}

function setLeadingCookie (id) {
	setCookie ('n'+id, 'y');
}

function setTable ( id ) {
	var div 	= document.getElementById("table_div_"+id);
	var img 	= document.getElementById("table_img_"+id);
	var c = getCookie ( id );
	if ( c == "n" ) {
		img.src = "styles/style1/images/plus.gif";
		div.style.display = "none";
	} else if ( c == "y" ) {
		img.src = "styles/style1/images/minus.gif";
		div.style.display = "";
	}
}

function switchMenuImg (td, val, typ) {
	if ( typ == 1 ) {
		if ( val == 0 ) {
			td.style.background = "#2D9106 url(styles/style1/images/spacer.gif)";
		} else if ( val == 1 ) {
			td.style.background = "#2D9106 url(styles/style1/images/meniu_sageata1.gif)";
		}
	} else if ( typ == 2 ) {
		if ( val == 0 ) {
			td.style.background = "#FFBE00 url(styles/style1/images/spacer.gif)";
		} else if ( val == 1 ) {
			td.style.background = "#FFBE00 url(styles/style1/images/meniuDR_sageata.gif)";
		}
	}
}

function changeInputColor ( inp, val ) {
		if ( val == 1 ) {
			inp.style.bgColor = "#000000";
		} else if ( val == 0 ) {
			inp.style.bgColor = "#E2E7E7";
		}
}

function selectMove (f, f1, f2) {
	var F1 = eval ('document.forms.'+f+'.'+f1);
	var F2 = eval ('document.forms.'+f+'.'+f2);
	for ( i=0;i<F1.length;i++ ) {
		if ( F1[i].selected == true ) {
			t = getOptionIndex (f1, f, i, 't');
			v = getOptionIndex (f1, f, i, 'v');
			removeOption (f1, f, i)
			addOption (f2, f, t, v);
		}
	}
}

function checkLeadings ( f, field, l ) {
	var F1 = eval ('document.forms.'+f+'.'+field);
	if ( l > 0 ) {
		if ( F1.length < l ) {
			alert ('Trebuie sa existe 4 leadings');
			return false;
		} else if ( F1.length > l ) {
			alert ('Trebuie sa existe 4 leadings');
			return false;
		}
	}
	return true;
}

/*function selectSelectAll (f, n) {
	var selOp = eval ('document.forms.'+f+'.'+n+'');
	for ( i=0;i<selOp.length;i++ ) {
		selOp[i].selected = true;
	}
}*/

function unSelect ( f, field, val ) {
	if ( val ) {
		var F = eval ('document.forms.'+f+'.'+field);
		F.value = "";
	}
}

function changePreview ( f, field, i ) {
	var F1 = eval ('document.forms.'+f+'.'+field);
	if ( i != "" ) {
		F1.src = 'styles/style1/images/leadings/home_'+i+'/preview.gif';
	} else {
		F1.src = 'styles/style1/images/spacer.gif';
	}
}

function uploadedPreview ( f, field, i ) {
	var F1 = eval ('document.forms.'+f+'.'+field);
	if ( i != "" ) {
		F1.src = 'images/upload/t-'+i+'.jpg';
	} else {
		F1.src = 'styles/style1/images/spacer.gif';
	}
}

function imagePreview ( f, field, i, path ) {
	var F1 = eval ('document.forms.'+f+'.'+field);
	if ( i != "" ) {
		F1.src = path+'/'+i+'.gif';
	} else {
		F1.src = 'styles/style1/images/spacer.gif';
	}
}

function toImagesWindow (f, e, v) {
	eval ("window.opener.document."+f+"."+e+".value += '"+v+"'");
}

function switchImage ( img, t ) {
	t.src = 'images/meniu/'+img;
}

function switchImageOrg ( img, t ) {
	t.src = 'images/'+img;
}

function showmenu (id, val) {
	if (document.getElementById) {
		if(document.getElementById("menu_"+id)) {
			var elem = document.getElementById("menu_"+id);
			if ( val == 0 ) {
				elem.style.display = "none";
			} else if ( val == 1 ) {
                elem.style.display = "";
			}
		}
		return false;
	}
}



function changeBG ( td, c ) {
	td.className = c;
}