/*
* $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 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) {}
//}

function changeCityDealer (n, nr) {
// in situatia in care se elimina dealeri	
	try {
			
		for (i=0;i<nr+1;i++ ) {
			if(document.getElementById ('div_'+i)) {
			    var selOp = document.getElementById ('div_'+i);
			    selOp.style.display='none';
			}
		}
		
		if(document.getElementById ('div_'+n)) {
		   var selOp = document.getElementById ('div_'+n);
		   selOp.style.display='';
		}
		
	} 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 doOpenZoom (url, wid, hei) {
	n = "popup_zoom";
    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 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/style1/images/plus.gif";
		div.style.display = "none";
	} else if ( c == "y" ) {
		img.src = "styles/style1/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/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+'';
	} 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 setFileValue ( val, id ) {
	$_id = document.getElementById (id);
	$_id.value = val;
}

function switchImage ( img, t ) {
	t1 = document.getElementById (t);
	t1.src = 'styles/style1/images/menu/'+img;
}

function changeBG ( td, c ) {
	td.className = c;
}

function showSubMenu ( id ) {
	for ( i=1;i<=6;i++ ) {
		setVisibility ('submenu_'+i, 'f');
	}
	setVisibility ('submenu_'+id, 't');
}

function adminColor ( col ) {
	_div = document.getElementById ('div_color');
	_div.style.background = col;
}

function contactDivs ($val, $total) {
	
	for ( $i=1;$i<=$total;$i++ ) {
		_div = document.getElementById ('contact_'+$i);
		_div.style.display = "none";
	}
	
	_div = document.getElementById ('contact_'+$val);
	_div.style.display = "";
}

function getOrase ($judid) {
/*
	new Ajax.Request('/index.php?o=json&do=orase&judid='+$judid, { method:'get',
  		onSuccess: function(transport, json){
	  		json = transport.responseText;
      		//alert(json ? Object.inspect(json) : "no JSON object");
      		alert ( json );
      		data = json.evalJSON ();
    		}
  	});
*/
}

function adminDownload ($type) {
	$proid = document.getElementById ('proid');
	$code = document.getElementById ('code');
	
	if ( $type == 1 && $code.value != "" ) {
		$code.value = "";
	} else if ( $type == 2 && $proid.value != "" ) {
		$proid.value = "";
	}
	
	if ( $code.value != "" || $proid.value != "" ) {
		setVisibility ('download', 't');
	} else {
		setVisibility ('download', 'f');
	}
}

function changeClass ( $cl, $el ) {
	$el.className = $cl;
}

function changeproductClass ( $cl, $ti, $el ) {
	$el.className = 'subcategorie_lista_element'+$cl;
	$title = document.getElementById ('product_title_'+$ti);
	$title.className = 'subcategorie_element_title'+$cl;
}

function doDownload ($val) {
	$submit = document.getElementById ('dow');
	if ( $val.checked == true ) {
		$submit.style.display = '';
	} else {
		$submit.style.display = 'none';
	}
}

function toggleVisibility (n) {
	try {
    	var selOp = document.getElementById ('div_'+n);

		if ( selOp.style.display == '' ) {
       		selOp.style.display='none';
       		ret = false;
		} else {
			selOp.style.display='';
			ret = true;
		}
	}
	catch (ex) {}
	return ret;
}
