

function trim(str) {
	return str.replace(/^\s+|\s+$/g, '');
}


function validateEmail(vfld){
	  var tfld = trim(vfld.value);
	  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/

	  if (tfld == "" || !email.test(tfld)) {
		  return false;
	  }
	  return true;
}

function checkPresent(str){
	if(trim(str) != ''){
		return true;
	}
	return false;
}


function checkLength(mystr, minlenght){
	var str = trim(mystr.value);

	if(str.length >= minlenght){
		return true;
	}
	return false;
}


function openPopup(image_name, image_width, image_height){
	var top = (screen.height - image_height) /2;
	var left = (screen.width - image_width) /2;
	window.open("inc/image_popup.php?file_name="+image_name,"picture","resizable=no, width="+image_width+", 	height="+image_height+", top="+top +", left="+left);
}

function showDialog(name)
{
	var dlgCommon = dojo.widget.byId(name);
	dlgCommon.show();
	hideFlashDivs();
	return true;
}

function hideDialog(name)
{
	showFlashDivs();
	var dlgCommon = dojo.widget.byId(name);
	dlgCommon.hide();
	return true;
}

function hideFlashDivs()
{
	var logo = dojo.byId('logo');
	var news = dojo.byId('news');
	var anim = dojo.byId('anim');
	if (logo != null) logo.style.display="none";
	if (news != null) news.style.display="none";
	if (anim != null) anim.style.display="none";
}

function showFlashDivs()
{
	var logo = dojo.byId('logo');
	var news = dojo.byId('news');
	var anim = dojo.byId('anim');
	if (logo != null) logo.style.display="block";
	if (news != null) news.style.display="block";
	if (anim != null) anim.style.display="block";
}

function show_div(id)
{
	if(document.getElementById(id)){
		document.getElementById(id).style.display ="block";
	}
}

function hide_div(id)
{
	if(document.getElementById(id)){
		document.getElementById(id).style.display ="none";
	}
}

function show_hide_div(id)
{
	if(document.getElementById(id)){
		if(document.getElementById(id).style.display =="none") {
			document.getElementById(id).style.display ="block";
		} else { 
			document.getElementById(id).style.display ="none";
		}
	}
}

function hide_all_infos()
{
	hide_div('productes_sao_blanc_info');
	hide_div('productes_sao_abrivat_info');
	hide_div('productes_sao_expressiu_info');
	hide_div('productes_sao_expressiu_info');
	hide_div('productes_lots_6u_info');
	hide_div('productes_lots_3u_info');
}

//-------------------------------------------------------------------------------
// Check Browser
function loadBrowserCSS(id, path){

	var theUA = navigator.userAgent.toLowerCase();
	isExplorer = (theUA.indexOf('msie')!=-1);
	var posInicio = theUA.lastIndexOf('msie')+4;
	theversion = parseFloat(theUA.substring(posInicio, posInicio+4));
	
	if (isExplorer){
		//var link ='<link href="' + path + "styles_ie" + theversion + '.css" rel="stylesheet" type="text/css" id="IE_style" />';
		//document.write(path + "styles_ie" + theversion + '.css');
		/*
		if(document.getElementById(id)){
			document.write('Load Style');
			document.getElementById(id).href = path + "styles_ie"+theversion+".css";
		}
		*/
		
		var link = document.createElement('link');  
		link.href = path + "styles_ie" + theversion + '.css';  
		link.rel = 'stylesheet';  
		link.type = 'text/css';
		document.getElementsByTagName('head')[0].appendChild(link);

		//document.write(link);
		//alert(document.getElementById(id).href+"::");
	}
}

function loadCSS(id, path, browser, version, os)
{
	var link = document.createElement('link');  
	link.href = path + "styles_"+browser+"_" + version + '.css';  
	link.rel = 'stylesheet';  
	link.type = 'text/css';
	document.getElementsByTagName('head')[0].appendChild(link);
}

function showDialog(name)
{
	var dlgCommon = dojo.widget.byId(name);
	if(dlgCommon != null) dlgCommon.show();
	hideFlashDivs();
	return true;
}

function hideDialog(name)
{
	showFlashDivs();
	var dlgCommon = dojo.widget.byId(name);
	if(dlgCommon != null) dlgCommon.hide();
	return true;
}



/************************* SCROLL ***********************************/
var current_pos = null;
var first_pos = 0;
var last_pos = null;
var _step = 3;
var _scroll = false;
var _scrollUp = false;
var _scrollDown = false;
/** TODO: duration debe ser en relacion al contenido **/

function startScrollUp(node_id)
{
	_scrollUp = true;
	_scrollDown = false;
	scrollUp(node_id);
}

function scrollUp(node_id)
{
	if( (current_pos[node_id] > -last_pos[node_id]) && _scrollUp){							
		var slide = dojo.fx.slideTo({node: node_id, duration: 10, top: current_pos[node_id] - _step});
		slide.play();
		current_pos[node_id] = current_pos[node_id] - _step;
		setTimeout('scrollUp("'+node_id+'")', 10);
	}
}

function startScrollDown(node_id)
{
	_scrollUp = false;
	_scrollDown = true;
	scrollDown(node_id);
}

function scrollDown(node_id)
{
	
	if( (current_pos[node_id] < first_pos) && _scrollDown){
		var slide = dojo.fx.slideTo({node: node_id, duration: 10, top: current_pos[node_id] + _step});
		slide.play();
		current_pos[node_id] = current_pos[node_id] + _step;					
		setTimeout('scrollDown("'+node_id+'")', 10);
	}
}

function stopScroll()
{
	_scrollUp = false;
	_scrollDown = false;
}
		
function initScrolls()
{	
	current_pos = Array();
	last_pos =  Array();
	current_pos['reconeixements_text_1'] = 0;
	current_pos['reconeixements_text_2'] = 0;
	current_pos['reconeixements_text_3'] = 0;
	current_pos['tenda_text_0'] = 0;
	current_pos['tenda_text_1'] = 0;
	current_pos['tenda_text_2'] = 0;
	current_pos['prod_text_2'] = 0;
	current_pos['prod_text_3'] = 0;
	current_pos['prod_text_4'] = 0;
	last_pos['reconeixements_text_1'] = 0;
	last_pos['reconeixements_text_2'] = 0;
	last_pos['reconeixements_text_3'] = 0;
	last_pos['tenda_text_0'] = 0;
	last_pos['tenda_text_1'] = 0;
	last_pos['tenda_text_2'] = 0;
	last_pos['prod_text_2'] = 0;
	last_pos['prod_text_3'] = 36;
	last_pos['prod_text_4'] = 50;
	
	var _total = 0;
	
	// Scrolls reconocimientos
	if(dojo.byId('nota_final_1')){
		_total = dojo.byId('nota_final_1').offsetTop;
		if(_total > 478){
			last_pos['reconeixements_text_1'] = _total - 500 ;
			show_div('flechas_reconocimientos_1');
		}
		
		_total = dojo.byId('nota_final_2').offsetTop;
		if(_total > 478){
			last_pos['reconeixements_text_2'] = _total - 500 ;
			show_div('flechas_reconocimientos_2');
		}
		
		_total = dojo.byId('nota_final_3').offsetTop;
		if(_total > 478){
			last_pos['reconeixements_text_3'] = _total - 500 ;
			show_div('flechas_reconocimientos_3');
		}
	}
	
	// Scroll de productes
	if(dojo.byId('final_product_2')){
		_total = dojo.byId('final_product_2').offsetTop;
		if(_total > 510){
			last_pos['prod_text_2'] = _total - 510 ;
		}
	}
	
	// Scroll de tenda online
	if(dojo.byId('caja_tenda_text_0_last')){
		_total = dojo.byId('caja_tenda_text_0_last').offsetTop;
		if(_total > 500){
			last_pos['tenda_text_0'] = _total - 500 ;
		}
		
		_total = dojo.byId('caja_tenda_text_1_last').offsetTop;
		if(_total > 500){
			last_pos['tenda_text_1'] = _total - 500 ;
		}
		if(dojo.byId('caja_tenda_text_2_last')){
			_total = dojo.byId('caja_tenda_text_2_last').offsetTop;
			if(_total > 500){
				last_pos['tenda_text_2'] = _total - 500 ;
			}
		}
	}
	/*
	var _ids1 = document.getElementsByName('HID_TotalReconoixementsIds_1');
	var _ids2 = document.getElementsByName('HID_TotalReconoixementsIds_2');
	
	var _total = 0;
	for (var i=0; i < _ids1.length; i++){
		_total = _total + document.getElementById(_ids1[i].value).clientHeight;
	}
	if(_total > 500){
		last_pos['reconeixements_text_1'] = _total - 500 ;
	}
	
	
	_total = 0;
	for (var i=0; i < _ids1.length; i++){
		_total = _total + document.getElementById(_ids1[i].value).clientHeight;
	}
	if(_total > 500){
		last_pos['reconeixements_text_2'] = _total - 500;
	}
	*/
}	


/************************* Change HOME GB IMage *****************************/
function changeBackgroundImage(section)
{
	nro = dojo.byId('current_image').value;
	nro++;
	if(nro > dojo.byId('last_image').value){
		nro=1;
	}
	setBackgroundImage(section, nro);
	setTimeout("changeBackgroundImage('"+section+"')",5000);
	dojo.byId('current_image').value = nro;
}

/******************* Resize  *********************************************/
function setBackgroundImage(section, nro, action)
{
	//alert(name+"::section"+section);
	
	//if(section=="vinyes") alert("outerWidth:"+window.outerWidth+"\n innerWidth:"+window.innerWidth+"\ client width:"+document.body.clientWidth);
	//var div_contenedor = dojo.byId('background').gh;
	//alert("Width:"+dojo.byId('background').clientWidth+" :: Height:"+dojo.byId('background').clientHeight);
	//alert("Width:"+dojo.byId('background').scrollWidth+" :: Height:"+dojo.byId('background').scrollHeight);
	//if(section=="vinyes") alert("outerHeight:"+window.outerHeight+"\n innerHeight:"+window.innerHeight+"\ client width:"+document.body.clientHeight);
	var size = getSize();
	var height = getImageHeight();
	var width = getImageWidth();
	var root_directory_url = document.formGeneric.HID_RootDirectoryUrl.value;
	//if(section=="vinyes") alert("size:"+size);
	
	image = 'url('+root_directory_url+'images/backgrounds/'+section+'/'+nro+'_'+size+'.jpg)';
	var image_src = root_directory_url+'images/backgrounds/'+section+'/'+nro+'_'+size+'.jpg';
	//var image_src = 'images/backgrounds/'+section+'/'+nro+'_medium.jpg';
	if(section=="productes"){
		image_src = root_directory_url+'images/backgrounds/productos/'+action+'.jpg';
	}

	
	if(section != "cistella" && section != "tenda" && section != "login" && section != "bill"){
		if(dojo.byId('background').scrollWidth > dojo.byId('background').scrollHeight){
			document.getElementById('background_image').width = width;
			document.getElementById('background_image').height = height;
		} else {
			document.getElementById('background_image').height = height;
			document.getElementById('background_image').width = width;
		}
		
		document.getElementById('background_image').height = height;
		document.getElementById('background_image').width = width;
		
		document.getElementById('background_image').src = image_src;
	} else {
		document.getElementById('background').style.display = "none";
	}
}

/**
  *	 Images Size: 
			large 2048x1536
			medium 1200x900
			small 960x720
			otro: 1188x695
*/
/*
function getSize(){
	var _size = "small";
	if(dojo.doc.width <= 960){
		_size = "small";
	} else if(dojo.doc.width <= 1200){
		_size = "medium";
	} else {
		_size = "large";
	}
	return _size;
}
*/

// OJO: devuevel siempre medium
function getSize()
{

	var _size = "medium";
	/*
	_px_size = document.body.clientWidth;
	
	if(_px_size <= 960){
		_size = "small";
	} else if(_px_size <= 1800){
		_size = "medium";
	} else {
		_size = "large";
	}
	*/
	
	return _size;
}

function getImageHeight()
{
	var _height;
	var _size = getSize();
	if(_size == "small"){
		_height = 720;
	} else if(_size == "medium"){
		_height = 900 ;
	} else if(_size == "695x1188"){
		_height = 695;
	} else {
		_height = 1536;
	}
	return _height;
}


function getImageWidth()
{
	var _width;
	var _size = getSize();
	if(_size == "small"){
		_width = 960;
	} else if(_size == "medium"){
		_width = 1200 ;
	} else if(_size == "695x1188"){
		_width = 1188;
	} else {
		_width = 2048;
	}
	return _width;
}


function resizeBackgroundImage()
{
	//dojo.byId('background').style.width = dojo.doc.width + "px;";
	/*
	dojo.style(dojo.byId('background'), 'width', dojo.doc.width+'px');
	dojo.style(dojo.byId('background'), 'height', dojo.doc.height+'px');
	*/
}

// Resize window
function setWindowSize(width,height) {
	self.resizeTo(width,height);
	return;
}

// Resize BG Image
function doResize() {
	if(!dojo.byId('page_body')) return;
	var pageDimensions = document.viewport.getDimensions();
	var divWidth = pageDimensions.width;
	$('background-content').setStyle({width:'1px',height:'1px'});
	dojo.byId('page_body').setStyle({width:'1px',height:'1px'});
	 // workaround an IE rendering problem
	if(Prototype.Browser.IE) window.scrollTo(0,0);
	if(Prototype.Browser.Gecko)
		pageDimensions.height = window.innerHeight + window.scrollMaxY;
	else
	 	pageDimensions.height =     document.height ? document.height :
	window.innerHeight ? (window.innerHeight + (window.scrollMaxY || 0)) : Math.max(document.documentElement.clientHeight, $$('html')[0].scrollHeight);
	var body = dojo.byId('body').first();
	var divHeight = pageDimensions.height - Position.page($('background-content'))[1];   var imageHeight = pageDimensions.height - Position.page($('background-content'))[1];
	var imageWidth = pageDimensions.width;
	
	if (divWidth < 1200) {
		divWidth = 1200;
	}  if (divHeight < 800) {
		divHeight = 800;
	}
	
	imageWidth = imageHeight * 1200/800;
	if (imageWidth < divWidth) {
		imageWidth = divWidth;
		imageHeight = imageWidth * 800/1200;
	}
	if (imageHeight < divHeight) {
		imageHeight = divHeight;
		imageWidth = imageHeight * 1200/800;
	}
	$('content').setStyle({width:divWidth - 23 + "px"});
	dojo.byId('page_body').setStyle({
		visibility:"visible", width:imageWidth + "px", height:imageHeight + "px",
		marginLeft:((pageDimensions.width-imageWidth)/2).round()+"px"
	});
	$('background-content').setStyle({width:divWidth + "px", height:divHeight + "px", display:"block"});
}


function loadData(form_name, id_name, _url)
{
	if(_url=="undefined"){
		var _url = "secure_page_low.php";
	}

	//alert(id_name+"::"+dojo.byId(id_name));
	//document.getElementById(id_name).innerHTML = "cargando ... ";
	var kw = {
		url: _url,
		sync: true,
		load: function(data){
				//alert("LOAD "+id_name+" :: "+data);
				if(trim(data)!=""){
					document.getElementById(id_name).innerHTML = data;
					//alert("contenido:"+document.getElementById(id_name).innerHTML);
				}
		},
		error: function(data){
				console.debug("A ocurrido un error: ", data);
		},
		timeout: 3000,
		form: form_name
	};
	dojo.xhrGet(kw);
}

/// Emulando myConfig

function getRootDirectoryUrl()
{
	return document.formGeneric.HID_RootDirectoryUrl.value;
}


