/*
* $Id: functions.js,v 1.17 2005/10/28 08:32:05 razvan.stanga Exp $
*/

// check for parent, if exists, close popup and load on the main page
try {
	if ( window.opener.location && window.opener.location.search ("mitsubishi") == -1 ) {
		window.opener.location = window.location;
		window.close ();
	}
}
catch (ex) {}

_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, 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 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 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+'';
	} 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 changeTopBG (bg, val) {
	if ( val == 1 ) {
		bg.className = 'tdhandover';
	} else if ( val == 0 ) {
		bg.className = 'tdhand';
	}
}

function galleryJS (nrpoze, dir, alt, act) {
	img = document.getElementById ('jsGalleryIMG');
	imgb = document.getElementById ('galleryJSb');
	imgf = document.getElementById ('galleryJSf');
	if ( act == 'b' && _nrpoza > 1 ) {
		img.src = "/images/galeriejs/"+dir+"/"+(_nrpoza-1)+".jpg";
		_nrpoza = _nrpoza - 1;
	}
	if ( act == 'f' && _nrpoza < nrpoze ) {
		_nrpoza = _nrpoza + 1;
		img.src = "/images/galeriejs/"+dir+"/"+_nrpoza+".jpg";
	}
	
	if ( alt ) {
		alt = alt.split ("|");
		writeToLayer ('galleryjsalt', alt[ _nrpoza-1 ], 'clear');
	}

	if ( _nrpoza == 1 ) {
		imgb.src = "/styles/style1/images/spacer.gif";
		imgb.alt = "";
	} else if ( _nrpoza > 1 ) {
		imgb.src = "/styles/style1/images/but_gal_back.gif";
		imgb.alt = "Inapoi";
	}
	
	if ( _nrpoza == nrpoze ) {
		imgf.src = "/styles/style1/images/spacer.gif";
		imgf.alt = "";
	} else if ( _nrpoza >= 1 ) {
		imgf.src = "/styles/style1/images/but_gal_next.gif";
		imgf.alt = "Inainte";
	}
}

function newGalleryJS (nrpoze, braid, act) {
	img = document.getElementById ('jsGalleryIMG');
	imgb = document.getElementById ('galleryJSb');
	imgf = document.getElementById ('galleryJSf');
	
	if ( act == 'b' && _nrpoza > 0 ) {
		_nrpoza = _nrpoza - 1;
	}
	if ( act == 'f' && _nrpoza < (nrpoze-1) ) {
		_nrpoza = _nrpoza + 1;
	}
	_current = $_opk[_nrpoza].split ("||");
	_imgid = _current[0];
	_carid = _current[1];
	_alt   = _current[2];
	img.src = "http://web2.webstyler.ro/autotiriac/images/extras/"+braid+"/"+_carid+"-"+_imgid+".jpg";

	writeToLayer ('galleryjsalt', _alt, 'clear');

	if ( _nrpoza == 0 ) {
		imgb.src = "/styles/style1/images/spacer.gif";
		imgb.alt = "";
	} else if ( _nrpoza > 0 ) {
		imgb.src = "/styles/style1/images/but_gal_back.gif";
		imgb.alt = "Inapoi";
	}

	if ( _nrpoza == (nrpoze-1) ) {
		imgf.src = "/styles/style1/images/spacer.gif";
		imgf.alt = "";
	} else if ( _nrpoza >= 0 ) {
		imgf.src = "/styles/style1/images/but_gal_next.gif";
		imgf.alt = "Inainte";
	}
}

function departmentAct ( val ) {
	if ( val == 1 ) {
		setVisibility ("department", 't');
	} else {
		setVisibility ("department", 'f');
	}
}

function changeSrc (img, name) {
	img.src = '/styles/style1/images/showroom/'+name;
}

function switchImage ( img, t ) {
	t.src = 'styles/style1/images/topmenu/'+img;
}


function downloadJS (nrpoze, dir, poza) {
	img = document.getElementById ('imgMed');
	link_a = document.getElementById ('linka');
	link_a1 = document.getElementById ('linka1');
	link_b = document.getElementById ('linkb');
	link_b1 = document.getElementById ('linkb1');

	img.src = '/images/download/'+dir+'/med_'+poza+'.jpg';
	link_a.href = 'index.php?o=popup&img=/images/download/'+dir+'/800_'+poza+'.jpg';
	link_a1.href = 'index.php?o=popup&img=/images/download/'+dir+'/800_'+poza+'.jpg';
	link_b.href = 'index.php?o=popup&img=/images/download/'+dir+'/1024_'+poza+'.jpg';
	link_b1.href = 'index.php?o=popup&img=/images/download/'+dir+'/1024_'+poza+'.jpg';
}

function pagesJS ( page, act ) {
	for ( i=0;i<=100;i++ ) {
		setVisibility ('page'+i, 'f');
	}
	page = parseFloat ( page );
	if ( act == "b" && page > 1 ) {
		page = page - 2;
	} else if ( act == "f" ) {
		
	}

	setVisibility ('page'+page, 't');
}


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 {
		return 'Other';
	}
}

var gallerySelected = 1;
var gImages = 5;
function changeGallery ( n, c ) {
	x = document.getElementById ('bigimage');
	x.src = "imagini/zi1/"+c+"/"+n+"m.jpg";
	
	for ( i=1;i<=gImages;i++ ) {
		var td = document.getElementById ('gImg_'+i);
		td.className = "hand";
		var img = document.getElementById ('Img_'+i);
		img.src = "imagini/zi1/"+c+"/"+i+".jpg";
	}
	
	for ( i=1;i<=gImages;i++ ) {
		if ( i == n ) {
			var td = document.getElementById ('gImg_'+i);
			td.className = "hand";
			gallerySelected = i;
		}
	}
}

function changeCorners ( n, i ) {
	if ( i == 1 ) {
		var td = document.getElementById ('gImg_'+n);
		td.className = "hand";
	} else if ( i == 0 && gallerySelected != n ) {
		var td = document.getElementById ('gImg_'+n);
		td.className = "hand";
	}
}


function setVisibilityGallery (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';
	}
}

_id = 1;
function closeAll (id) {
_id = id;
	for (i=1; i<=2; i++){
		setVisibilityGallery ('pag'+i, 'f');
	}
	setVisibilityGallery ('pag'+id, 't');
}

function changePage (act) {
	if ( act == 'b' ) {
		closeAll (_id-1);
	} else if ( act == 'f' ) {
		closeAll (_id+1);	
	}
}

function numberFormat ( number ) {
  laenge = "2";
  sep = ".";
  th_sep = ",";
  number = Math.round( number * Math.pow(10, laenge) ) / Math.pow(10, laenge);
  str_number = number+"";

  arr_int = str_number.split(".");
  if(!arr_int[0]) arr_int[0] = "";
  if(!arr_int[1]) arr_int[1] = "";
  if(arr_int[1].length < laenge){
    nachkomma = arr_int[1];
    for(i=arr_int[1].length+1; i <= laenge; i++){  nachkomma += "0";  }
    arr_int[1] = nachkomma;
  }
  if(th_sep != "" && arr_int[0].length > 3){
    Begriff = arr_int[0];
    arr_int[0] = "";
    for(j = 3; j < Begriff.length ; j+=3){
      Extrakt = Begriff.slice(Begriff.length - j, Begriff.length - j + 3);
      arr_int[0] = th_sep + Extrakt +  arr_int[0] + "";
    }
    str_first = Begriff.substr(0, (Begriff.length % 3 == 0)?3:(Begriff.length % 3));
    arr_int[0] = str_first + arr_int[0];
  }
  //return arr_int[0];
  return arr_int[0]+sep+arr_int[1];
}

function calcAccesTotal () {
	$total = Number (0);
	$a = document.forms.accessories.accessory;
	for ( $i=0;$i<$a.length;$i++ ) {
		if ( $a[$i].checked == true ) {
			$total += Number ($a[$i].value);
		}
	}
	$a = document.getElementById ('accessory_total');
	$a.innerHTML = numberFormat ($total);
}


function toggleVisibility (div) {
	try {
		for ( $i=0;$i<$_opk.length;$i++ ) {
			$v = $_opk[$i].split ("||");
			setVisibility ('acc_'+$v[0], 'f');
		}

        var selOp = document.getElementById (div);
		if ( selOp.style.display == 'none' ) {
       		selOp.style.display='';
		} else {
			selOp.style.display='none';
		}
	}
	catch (ex) {}
}

function arataRaspuns () {
	v = 0;
	
	arr = document.forms.intrebare.raspuns;
	for ( i=0;i<arr.length;i++ ) {
		if ( arr[i].checked == true ) {
			v = arr[i].value;
		}
	}

	for (i=1;i<=3;i++){
		if ( v == i ) {
			setVisibility ('raspuns_'+i, 't');
		} else {
			setVisibility ('raspuns_'+i, 'f');
		}
	}
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; 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=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

