String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}

var rut ='http://www.rotravelplus.com/';
var Tsearch;
var isIE = (navigator.appName == "Microsoft Internet Explorer");
var hasFocus = true;
var active_element;

var $Mesaje = {'ro':[],'en':[]};
$Mesaje['en'][0] = 'Please enter your email address.';
$Mesaje['en'][1] = 'Invalid email address.';
$Mesaje['en'][2] = 'Please enter your name.';
$Mesaje['en'][3] = 'Invalid name.';
$Mesaje['en'][4] = 'Please enter your phone number.';
$Mesaje['en'][5] = 'Please enter your message.';
$Mesaje['en'][6] = 'Your message is too short.';

$Mesaje['ro'][0] = 'Nu ati introdus adresa de email.';
$Mesaje['ro'][1] = 'Adresa de email introdusa nu este valida.';
$Mesaje['ro'][2] = 'Nu ati introdus numele.';
$Mesaje['ro'][3] = 'Numele introdus nu este valid.';
$Mesaje['ro'][4] = 'Nu ati introdus numarul de telefon.';
$Mesaje['ro'][5] = 'Nu ati introdus mesajul.';
$Mesaje['ro'][6] = 'Mesajul introdus este prea scurt.';

var expresii = new Array();
//password - fara spatiu
expresii[0] = /^\S*$/;
//username
expresii[1] = /^[a-zA-Z0-9_.]*$/;
//nume
expresii[2] = /^[a-zA-Z'\- ]*$/;
//email1
expresii[3] = /^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$/;
//email2
expresii[4] = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
//este numar intreg
expresii[5] = /^\d*$/;
//numar intreg > 0
expresii[6] = /^[1-9]\d*$/;
//cnp : numar intreg,nu are voie sa inceapa cu 0 si are 13 caractere
expresii[7] = /^[1-9][\d]{12}$/;
//cif : numar intreg, maxim 10 cifre
expresii[8] = /^[\d]{1,10}$/;

function urldecode (str) {
    // Decodes URL-encoded string  
    // 
    // version: 1006.1915
    // discuss at: http://phpjs.org/functions/urldecode    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous    // +   improved by: Orlando
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +      bugfixed by: Rob
    // %        note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // %        note 2: Please be aware that this function expects to decode from UTF-8 encoded strings, as found on    // %        note 2: pages served as UTF-8
    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'
    
    return decodeURIComponent(str.replace(/\+/g, '%20'));
}
function in_array (needle, haystack, argStrict) {
    // Checks if the given value exists in the array  
    // 
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/in_array    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // +   input by: Billy
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
    var key = '',        strict = !! argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {                return true;
            }
        }
    } else {
        for (key in haystack) {            if (haystack[key] == needle) {
                return true;
            }
        }
    } 
    return false;
}
function array_search (needle, haystack, argStrict) {
    // Searches the array for a given value and returns the corresponding key if successful  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/array_search    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: array_search('zonneveld', {firstname: 'kevin', middle: 'van', surname: 'zonneveld'});
    // *     returns 1: 'surname'    // *     example 2: ini_set('phpjs.return_phpjs_arrays', 'on');
    // *     example 2: var ordered_arr = array({3:'value'}, {2:'value'}, {'a':'value'}, {'b':'value'});
    // *     example 2: var key = array_search(/val/g, ordered_arr); // or var key = ordered_arr.search(/val/g);
    // *     returns 2: '3'
    var strict = !!argStrict,        key = '';
    
    if (haystack && typeof haystack === 'object' && haystack.change_key_case) { // Duck-type check for our own array()-created PHPJS_Array
        return haystack.search(needle, argStrict);
    }    if (typeof needle === 'object' && needle.exec) { // Duck-type for RegExp
        if (!strict) { // Let's consider case sensitive searches as strict
            var flags = 'i' + (needle.global ? 'g' : '') +
                        (needle.multiline ? 'm' : '') +
                        (needle.sticky ? 'y' : ''); // sticky is FF only            needle = new RegExp(needle.source, flags);
        }
        for (key in haystack) {
            if (needle.test(haystack[key])) {
                return key;            }
        }
        return false;
    }
     for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            return key;
        }
    } 
    return false;
}

//check if the email address is valid
function check_email(emladrs){
   var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
   var rez = true
   if (emladrs == ""){
       rez=true
   }
   else{
		if (filter.test(emladrs))
			rez=true
		else{
			rez=false
		}
    }
    return (rez)
}

//testeaza daca o variabila este goala
function empty (mixed_var) {
    var key;    
    if (mixed_var === "" || mixed_var === 0 || mixed_var === "0" || mixed_var === null || mixed_var === false || typeof mixed_var === 'undefined'){
        return true;
    } 
    if (typeof mixed_var == 'object') {
        for (key in mixed_var) {
            return false;
        }        return true;
    }
    return false;
}

//partea de AJAX
function GetXmlHttpObject(){
	var xmlHttp=null;
	try
	{
// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

//refresh cod securitate
function do_sndCode(txt,img){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	   alert ("Your browser does not support AJAX!");
	   return;
	} 
	var url=rut+"/script/captcha.php";
	url=url+"?sid="+Math.random();
	//document.getElementById(img).src=rut+'img/captcha_b.gif';
	xmlHttp.onreadystatechange=function(){
		if (xmlHttp.readyState==4){		
			img = document.getElementById(img); 
			img.src = rut+ '/script/captcha.php?' + Math.random()+"&txt="+txt;
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

//verifica daca elementul tastat este a-z + tab,up+down+left+right,ctrl,alt,shift,capslock,enter,delete,backspace
function checkvalidkeys(e){
	var characterCode; 
	if(typeof e != "undefined"){
		e = window.event;
		characterCode = e.keyCode;
	}
	else if(document.layers){
		characterCode = e.which;
	}
	
	if(characterCode == 8 || characterCode == 9 || characterCode == 16 || characterCode == 17|| characterCode == 18 || characterCode == 20 || characterCode == 32 || (characterCode >= 37 && characterCode <= 40) || characterCode == 46 || (characterCode >= 65 && characterCode <= 90)){		
		return false
	}
	else{
		return true
	}
}
//de modificat pentru stari default: checkbox,selectbox,radio.
function clear_form(idd,defaults){
	$(':input','#'+idd).not(':button, :submit, :reset, :hidden, :text').removeAttr('checked').removeAttr('selected');
	$(':text,textarea','#'+idd).each(function(){
		X = $(this).attr("id");
		D = (!empty(defaults) && typeof defaults[X] != "undefined")? defaults[X]:'';
		$(this).val(D);
	});
}
function check_contact(defaults,lg){
	k = true;
	erori = {};
	data = {};
	//...
	
	if(k){
		if($("#nume").val().trim() == "" || $("#nume").val().trim() == defaults['nume']){
			k= false;
			erori['nume'] = $Mesaje[lg][2];
		}
		else if(!expresii[2].test($("#nume").val())){
			k = false;
			erori['nume'] = $Mesaje[lg][3];
		}
	}
	if(k){
		if($("#eml").val().trim() == "" || $("#eml").val().trim() == defaults['eml']){
			k= false;
			erori['eml'] = $Mesaje[lg][0];
		}
		else if(!expresii[4].test($("#eml").val())){
			k = false;
			erori['eml'] = $Mesaje[lg][1];
		}
	}
	if(k){
		if($("#tel").val().trim() == "" || $("#tel").val().trim() == defaults['tel']){
			k= false;
			erori['tel'] = $Mesaje[lg][4];
		}
	}
	if(k){
		if($("#msj").val().trim() == "" || $("#msj").val().trim() == defaults['msj']){
			k= false;
			erori['msj'] = $Mesaje[lg][5];
		}
		else if($("#msj").val().length<3){
			k = false;
			erori['msj'] = $Mesaje[lg][6];
		}
	}
	data.status = k;
	data.erori = erori;
	return data;
}
function check_search(defaults,lg){
	k = true;
	data = {};
	erori = {};
	if($("#key").val().trim() == "" || $("#key").val().trim() == defaults['key']){
		k = false;
		erori['key'] = 'Please enter at least one keyword.';
	}
	data.status = k;
	data.erori = erori;
	return data;
}

			
function setFocusEvents(){
	active_element = document.activeElement;
	if (isIE){
		document.onfocusout = function(){onWindowBlur();}
		document.onfocusin = function(){onWindowFocus();}
	}
	else{
		window.onblur = function(){onWindowBlur();}
		window.onfocus = function(){onWindowFocus();}
	}
}
function onWindowFocus(){
	hasFocus = true;
}
function onWindowBlur(){
	if (active_element != document.activeElement){
		active_element = document.activeElement;
		return;
	}
	hasFocus = false;
}
