/*
* $Id: functions.js,v 1.1 2006/04/26 11:56:38 razvan.stanga Exp $
*/

// init
_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) {
	//val = val.replace ("\'", "\\\\\\'");
	eval ("f = document.forms."+f+"."+field);
	f.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, s) {
	var selOp = eval ('document.forms.'+f+'.'+n+'');
	var len = selOp.options.length;
	selOp.options[len] = new Option (t, v);
	if ( s == true ) {
		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) {
	try {
	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';
	}
	}
	catch (ex) {}
}

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 doOpenPopup2 (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=no,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 goTO ( url, val ) {
	if ( val ) {
		window.location=url;
	}
}

function changeShow ( id, lid ) {
	var _div 	= document.getElementById("div_show_"+id);
	if ( lid == 1 ) {
		var _link 	= document.getElementById("link_show_"+id);
	}

	if ( _div.style.display == "none" ) {
		_div.style.display = "";
		if ( lid == 1 ) {
			writeToLayer ("link_show_"+id, 'inchide');
		}
	} else {
        _div.style.display = "none";
        if ( lid == 1 ) {
        	writeToLayer ("link_show_"+id, 'detalii');
       	}
	}
}

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/style2/images/plus.gif";
		div.style.display = "none";
	} else if ( c == "y" ) {
		img.src = "styles/style2/images/minus.gif";
		div.style.display = "";
	}
}

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/style2/images/leadings/home_'+i+'/preview.gif';
	} else {
		F1.src = 'styles/style2/images/spacer.gif';
	}
}

function uploadedPreview ( f, field, i ) {
	var F1 = eval ('document.forms.'+f+'.'+field);
	if ( i != "" ) {
		F1.src = 'images/upload/t-'+i+'';
	} else {
		F1.src = 'styles/style2/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/style2/images/spacer.gif';
	}
}

function toImagesWindow (f, e, v) {
	eval ("window.opener.document."+f+"."+e+".value += '"+v+"'");
}

function setFileValue ( val, id ) {
	$_id = document.getElementById (id);
	$_id.value = val;
}

function switchImage ( img, t ) {
	t1 = document.getElementById (t);
	t1.src = 'styles/style2/images/menu/'+img;
}

function changeBG ( td, c ) {
	td.className = c;
}

function showSubMenu ( id, menus ) {
	for ( i=1;i<=menus;i++ ) {
		setVisibility ('submenu_'+i, 'f');
	}
	setVisibility ('submenu_'+id, 't');
}

function doGallery ( i, dir, news ) {
	try {
		if ( news == 0 ) {
 			hi = document.getElementById ('highlightimage');
 			hi.src = 'images/gallery/'+dir+'/'+i+'.jpg';
 		}
 	}
 	catch (ex) {}
 	
 	try {
 		hi = document.getElementById ('hllinkb');
 		hi.href = 'index.php?o=zoom&dir='+dir+'&img='+i+'&nrpoze='+$images;
 	}
 	catch (ex) {}
 	
 	try {
 		hi = document.getElementById ('hlwallpaper');
 		hi.href = 'index.php?o=wallpaper&dir='+dir+'&img='+i+'&sw='+screen.width;
 	}
 	catch (ex) {}
 	
	try {
		for ( _i=1;_i<=$images;_i++ ) {
 			hi = document.getElementById ('galtd_'+_i);
 			hi.className = 'galbg';
 			if ( news == 1 ) {
 				hi = document.getElementById ('gallink_'+_i);
 				hi.href = 'index.php?o=zoom&dir='+dir+'&img='+i+'&nrpoze='+$images;
 				hi.target = '_blank';
 			}
		}
 		hi = document.getElementById ('galtd_'+i);
 		hi.className = 'galbgactiv';
	}
	catch (ex) {}
 	
 	$current = i;
}

function doGalleryAct (curr, lar) {
	
	if ( curr == 'next' ) {
		if ( $current < $images ) {
			$current = $current + 1;
		}
	} else if ( curr == 'prev' ) {
		if ( $current > 1 ) {
			$current = $current - 1;
		}
	}
	
	doGallery ( $current, $dir );
	
	hi = document.getElementById ('highlightimage');
	hi.src = 'images/gallery/'+$dir+'/'+lar+''+$current+'.jpg';
	
	if ( $current > 1 ) {
		$prev = $current - 1;
	} else {
		$prev = 0;
	}
	if ( $current < $images ) {
		$next = $current + 1;
	} else {
		$next = $images + 1;
	}

	try {
    	hlp = document.getElementById ('hlprev');
		hln = document.getElementById ('hlnext');
		if ( $prev < 1 ) {
        	hlp.src = 'styles/style2/images/spacer.gif';
		} else {
			//hlp.src = 'styles/style2/images/spacer.gif';
			hlp.src = 'images/gallery/'+$dir+'/t-'+$prev+'.jpg';
		}
	
		if ( $next > $images ) {
        	hln.src = 'styles/style2/images/spacer.gif';
		} else {
			hln.src = 'images/gallery/'+$dir+'/t-'+$next+'.jpg';
			//hlp.src = 'styles/style2/images/spacer.gif';
		}
	}
	catch (ex) {}
	
	try {
 		hi = document.getElementById ('hllinkb');
 		hi.href = 'index.php?o=zoom&dir='+$dir+'&img='+$current+'&nrpoze='+$images;
 	
 		hi = document.getElementById ('hlwallpaper');
 		hi.href = 'index.php?o=wallpaper&dir='+$dir+'&img='+$current+'&sw='+screen.width;
 	}
 	catch (ex) {}
}

function doCTW ($i, $dir, $desc) {
	hi = document.getElementById ('highlightimage');
	hi.src = $dir+'/'+$i+'.jpg';
	
	try {
		for ( _i=1;_i<=$images;_i++ ) {
 			hi = document.getElementById ('galtd_'+_i);
 			hi.className = 'culgal';
		}
 		hi = document.getElementById ('galtd_'+$i);
 		hi.className = 'culgalact';
	}
	catch (ex) {}
	
	//hi = document.getElementById ('CTW_desc');
	//hi.innerHTML = $desc;
}

function toggleNews (n, classOn, classOff) {
	try {
        var selOp = document.getElementById ("div_"+n);
        var picOp = document.getElementById ("pic_"+n);
        var linkOp = document.getElementById ("link_"+n);
        
		if ( selOp.style.display == "none" ) {
       		selOp.style.display = '';
       		picOp.src = "/styles/style2/images/u_arrow.gif";
       		linkOp.className = classOn;
		} else {
			selOp.style.display = 'none';
			picOp.src = "/styles/style2/images/d_arrow.gif";
			linkOp.className = classOff;
		}
	}
	catch (ex) {}
}


function changeCityDealer (n, nr) {
	
	try {
		
	for (i=0;i<nr+1;i++ ) {
		var selOp = document.getElementById ('div_'+i);
		selOp.style.display='none';
	}
	
	var selOp = document.getElementById ('div_'+n);
	selOp.style.display='';
	
	
	}
	catch (ex) {}
}
