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+$/,"");
}

rut ='http://www.superfaber.ro/';

$Mesaje = {'ro':[],'en':[],'fr':[]};
$Mesaje['ro'][0] = 'Nu ati introdus adresa de email.';
$Mesaje['ro'][1] = 'Adresa de email 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.';

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

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

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'));
}

//verifica cnp-ul
function check_cnp(cnp){
	var filter = /^[1-9][\d]{12}$/;
	var rez = true;
	if (cnp == ""){
		rez=false;
	}
	else{
		if (filter.test(cnp))
			rez=true;
		else{
			rez=false;
		}
    }
	if(rez){
		ll = cnp.length;
		if(ll==13){
			if(parseInt(cnp.charAt(3)+cnp.charAt(4))>0 && parseInt(cnp.charAt(3)+cnp.charAt(4))<13){
				if(parseInt(cnp.charAt(5)+cnp.charAt(6))>0 && parseInt(cnp.charAt(5)+cnp.charAt(6))<32){
					if(parseInt(cnp.charAt(12))!=parseInt(((((parseInt(cnp.charAt(0))*2)+(parseInt(cnp.charAt(1))*7)+(parseInt(cnp.charAt(2))*9)+(parseInt(cnp.charAt(3))*1)+(parseInt(cnp.charAt(4))*4)+(parseInt(cnp.charAt(5))*6)+(parseInt(cnp.charAt(6))*3)+(parseInt(cnp.charAt(7))*5)+(parseInt(cnp.charAt(8))*8)+(parseInt(cnp.charAt(9))*2)+(parseInt(cnp.charAt(10))*7)+(parseInt(cnp.charAt(11))*9))%11)+"").charAt(0))){
						rez = false;
					}
				}
				else{
					rez = false;
				}
			}
			else{
				rez = false;
			}
		}
		else{
			rez = false;
		}	
	}
    return rez;
}

//verifica cif
function check_cif(cif){
	var filter = /^[\d]{1,10}$/;
	var rez = true;
	if (cif == ""){
		rez=false;
	}
	else{
		if (!filter.test(cif)){
			rez=false;
		}
    }
	if(rez){
		ll = cif.length;
		if(ll<11){
			test_key = '753217532';
			while(cif.length!=10){
				cif = '0'+cif;
			}
			suma = 0;
			for(i=0;i<9;i++){
				suma += parseInt(cif.charAt(i))*parseInt(test_key.charAt(i)); 
			}
			
			if(parseInt(cif.charAt(9))!=((suma%11)%10)){
				rez = false;
			}
		}
		else{
			rez = false;
		}
	}
	return rez;
}

//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)
}

//verifica daca valoarea unui camp este un intreg > 0 setand valoarea cu 1 in caz contrar
function check_value(id){
	field = document.getElementById(id);
	if((!(/^\d*$/.test(field.value)))||(field.value<1))
	field.value = 1;
 }
 
 //redirectioneaza catre parametrul cale
 function go_now(cale) {
	window.location.href = cale;
}

//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;
}
//variabila globala
//cache needed for overagressive garbage collectors.
var cache = [];
var image_loaded=[];
//var imag = '';
(function($){
//images can either be an array of paths to images or a  single image.
$.loadImages = function(images, callback){ 
	//convert to array if needed so rest of script works
	if (!(images instanceof Array)) {
		images = [images];
	} 
	var imagesLength = images.length;
	var loadedCounter = 0;
	for (var i = imagesLength; i--;) {
		var cacheImage = document.createElement('img');
		//set the onload method before the src is called otherwise will fail to be called in IE
		cacheImage.onload = function(){
			loadedCounter++;
			if (loadedCounter >= imagesLength) {
				if ($.isFunction(callback)) {
					callback(images);
				}
			}
		}
		cacheImage.src = images[i];
		cache.push(cacheImage);
	}
}
})(jQuery);
// -----------------------------------------------------------------------
// Eros Fratini - eros@recoding.it
// jqprint 0.3
//
// - 19/06/2009 - some new implementations, added Opera support
// - 11/05/2009 - first sketch
//
// Printing plug-in for jQuery, evolution of jPrintArea: http://plugins.jquery.com/project/jPrintArea
// requires jQuery 1.3.x
//
// Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
//------------------------------------------------------------------------

(function($) {
    var opt;

    $.fn.jqprint = function (options) {
        opt = $.extend({}, $.fn.jqprint.defaults, options);

        var $element = (this instanceof jQuery) ? this : $(this);
        
        if (opt.operaSupport && $.browser.opera) 
        { 
            var tab = window.open("","jqPrint-preview");
            tab.document.open();

            var doc = tab.document;
        }
        else 
        {
            var $iframe = $("<iframe  />");
        
            if (!opt.debug) { $iframe.css({ position: "absolute", width: "0px", height: "0px", left: "-600px", top: "-600px" }); }

            $iframe.appendTo("body");
            var doc = $iframe[0].contentWindow.document;
        }
        
        if (opt.importCSS)
        {
            if ($("link[media=print]").length > 0) 
            {
                $("link[media=print]").each( function() {
                    doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' media='print' />");
                });
            }
            else 
            {
                $("link").each( function() {
                    doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' />");
                });
            }
        }
        
        if (opt.printContainer) { doc.write($element.outer()); }
        else { $element.each( function() { doc.write($(this).html()); }); }
        
        doc.close();
        
        (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).focus();
        setTimeout( function() { (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).print(); if (tab) { tab.close(); } }, 1000);
    }
    
    $.fn.jqprint.defaults = {
		debug: false,
		importCSS: true, 
		printContainer: true,
		operaSupport: true
	};

    // Thanks to 9__, found at http://users.livejournal.com/9__/380664.html
    jQuery.fn.outer = function() {
      return $($('<div></div>').html(this.clone())).html();
    } 
})(jQuery);
