//var css_browser_selector = function() {var ua=navigator.userAgent.toLowerCase(),is=function(t){return ua.indexOf(t) != -1;},h=document.getElementsByTagName('html')[0],b=(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?'gecko ff2':is('firefox/3')?'gecko ff3':is('gecko/')?'gecko':is('opera/9')?'opera opera9':/opera\s(\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('chrome')?'chrome webkit safari':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';var c=b+os+' js'; h.className += h.className?' '+c:c;}();

//shortcut.add("b",function(){alert("ok");});

// Variables
var months_ro = new Array("Ianuarie","Februarie","Martie","Aprilie","Mai","Iunie","Iulie","August","Septembrie","Octombrie","Noiembrie","Decembrie");
var days_ro = new Array("Duminic&#259;","Luni","Mar&#355;i","Miercuri","Joi","Vineri","S&#226;mb&#259;t&#259;");
var months_en = new Array("January","February","March","Aprili","May","June","July","August","September","October","November","December");
var days_en = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

// cross-browser for getElementById
function xGetElementById(e)
{
  if(typeof(e)=='string') {
    if(document.getElementById) e=document.getElementById(e);
    else if(document.all) e=document.all[e];
    else e=null;
  }
  return e;
}

// Function which validates an email address
function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function isPhone(tel){
	regPhoneNumber = new RegExp(/^0\d{2,3}\-\d{6,7}$/);
	return regPhoneNumber.test(tel);
}

//function which blocks input to a restricted list of characters
function restrictInput(myfield, e, dec, char, special)
{
	var key;
	var keychar;
	var digits = String("0123456789");
	var letters = String("abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ");
	
	if (window.event)
		 key = window.event.keyCode;
	else if (e)
		 key = e.which;
	else
		 return true;
	keychar = String.fromCharCode(key);
	
	chars = digits;
	
	if(char)
		chars += letters;
	
	if(special)
		chars += special;
	
	// return
	if(key==13)
		return true;
	// control keys
	if((key==null) || (key==0) || (key==8) || (key==9) || (key==27))
		return true;
	// numbers & special chars
	else if((chars.indexOf(keychar) > -1))
		return true;
	// decimal point jump
	else if(dec && (keychar == ".") && myfield.value.indexOf(".") == -1)
		return true;
	else
		return false;
}

// function unauthentificated click
function getReal(){
	alert("Trebuie sa fiti autentificat pentru aceasta sectiune !");
	xGetElementById("logon-form").utilizator.focus();
	return false;
}

// function which validates the logon form
function checkLogon(d){
	if(d.utilizator.value == ""){
		alert("Campul 'utilizator' este boligatoriu !");
		d.utilizator.focus();
		return false;
	}
	if(d.parola.value == ""){
		alert("Campul 'parola' este obligatoriu !");
		d.parola.focus();
		return false;
	}
	return true;
}

// function quick navi
function quickNavi(categ,obj){
	if(obj.value != "null")
		document.location = docroot + "anunturi/" + categ + "/" + obj.value + "/";
}

function categorySearch(section,category,form){
	if(form.cauta.value != ""){
		window.location = docroot + "anunturi/" + section + "/" + category + "/cauta/" + form.cauta.value + "/";
	}
	return false;
}

function clearSearch(section,category){
	window.location = docroot + "anunturi/" + section + "/" + category + "/";
}

function formCancel(){
	return confirm('Sigur doriti renuntarea oricarei modificari ?');
}

function contSave(f){
	if(f.utilizator.value == ""){
		alert("Campul 'utilizator' este obligatoriu !");
		f.utilizator.focus();
		return false;
	}
	if(f.cod_utilizator.value == 0 && f.parola.value == ""){
		alert("Campul 'parola' este obligatoriu !");
		f.parola.focus();
		return false;
	}
	if(f.cod_utilizator.value == 0 && f.parola_verify.value == ""){
		alert("Campul 'verificare parola' este obligatoriu !");
		f.parola_verify.focus();
		return false;
	}
	if(f.parola.value != "" && f.parola.value != f.parola_verify.value){
		alert("Parola nu corespunde !");
		f.parola.focus();
		return false;
	}
	if(f.prenume.value == ""){
		alert("Campul 'prenume' este obligatoriu !");
		f.prenume.focus();
		return false;
	}
	if(f.nume.value == ""){
		alert("Campul 'nume' este obligatoriu !");
		f.nume.focus();
		return false;
	}
	if(f.telefon.value != ""){
		if(f.telefon.value.length != 11 || !isPhone(f.telefon.value)){
			alert("Telefonul trebuie sa fie de forma '0xxx-zzzzzz' sau '0xx-zzzzzzz' !");
			f.telefon.focus();
			return false;
		}
	}
	if(f.email.value == ""){
		alert("Campul 'email' este obligatoriu !");
		f.email.focus();
		return false;
	}
	if(!isEmailAddr(f.email.value)){
		alert("Adresa de email '"+f.email.value+"' NU este valida !");
		f.email.focus();
		return false;
	}
	if(f.cod_utilizator.value == 0 && f.check.value == ""){
		alert("Codul de verificare este obligatoriu !");
		f.check.focus();
		return false;
	}
	return true;
}

function restrictPhoneFormat(myfield, e, format)
{
	var digits = String("0123456789");
	var special = String();
	var allowed = String();
	
	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
		
	allowed = digits;
	
	// add allowed special characters from format
	for(i=0; i<format.length; i++){
		char = format.charAt(i); 
    
		if(allowed.indexOf(char) == -1){
			special += char;
    	allowed += char;
    }
  }
	
	// restrict input
	keychar = String.fromCharCode(key);
	
	if((key==13) || (key==null) || (key==0) || (key==8) || (key==9) || (key==27) || (allowed.indexOf(keychar) != -1))
		return true;
	else
		return false;
}

function userLogout(){
	window.location = docroot + 'cont/iesire/';
}

function changeSection(obj){
	var cb = xGetElementById("categorie");
	
	// remove old items
	cb.options.length = 0;
	
	// add new options
	for(key in combos[obj.value]){
		cb.options[cb.options.length] = new Option(combos[obj.value][key],key);
	}
}

function countAdChars(obj,maxChars){
	var counter = xGetElementById("ad-characters-left");
	counter.innerHTML = maxChars - obj.value.length;
}

function adType(obj,maxChars){	
	if(obj.value.length <= maxChars){
		countAdChars(obj,maxChars);
	} else {
  	obj.value = obj.value.substring(0, maxChars);
	}
}

function insertPhone(tel,maxChars){
	if(tel != "" && isPhone(tel)){
		var obj = xGetElementById("anunt");
		var counter = xGetElementById("ad-characters-left");
		var add = new String(((obj.value.substr(obj.value.length - 1) != " " && obj.value.length > 0) ? " " : "") + "Tel. " + tel);
		
		if(obj.value.length <= parseInt(maxChars) - add.length){
			obj.value += add;
			counter.innerHTML = maxChars - obj.value.length;
		} else {
			alert('Nu mai este spatiu suficient pentru telefon !');
		}
	} else {
		alert("Telefonul nu este salvat in contul Dvs. sau nu este in formatul corespunzator !");
	}
}

function anuntSave(form){
	if(form.anunt.value == ""){
		alert("Anuntul este obligatoriu !");
		form.anunt.focus();
		return false;
	}
	return true;
}

function sendForgotPass(form){
	if(form.utilizator.value == ""){
		alert("Campul 'utilizator' este obligatoriu !");
		form.utilizator.focus();
		return false;
	}
	if(form.email.value == ""){
		alert("Campul 'email' este obligatoriu !");
		form.email.focus();
		return false;
	}
	if(!isEmailAddr(form.email.value)){
		alert("Adresa de email este invalida !");
		form.email.focus();
		return false;
	}
	if(form.check.value == ""){
		alert("Codul de verificare este obligatoriu !");
		form.check.focus();
		return false;
	}
	return true;
}

function checkContactForm(f){
	if(f.nume.value == ""){
		alert("Campul 'nume' este obligatoriu !");
		f.nume.focus();
		return false;
	}
   if(f.telefon.value == ""){
		alert("Campul 'telefon' este obligatoriu !");
		f.telefon.focus();
		return false;
	}
	if(f.email.value == ""){
		alert("Campul 'email' este obligatoriu !");
		f.email.focus();
		return false;
	}
	if(!isEmailAddr(f.email.value)){
		alert("Adresa de email nu este valida !");
		f.email.focus();
		return false;
	}
	if(f.subiect.value == ""){
		alert("Campul 'subiect' este obligatoriu !");
		f.subiect.focus();
		return false;
	}
	if(f.mesaj.value == ""){
		alert("Campul 'mesaj' este obligatoriu !");
		f.mesaj.focus();
		return false;
	}
	if(f.check.value == ""){
		alert("Campul 'cod' este obligatoriu !");
		f.check.focus();
		return false;
	}
	return true;
}
