function trim(str) {
	return str.replace(/^\s+|\s+$/g, '');
}


function validateEmail(vfld){
	  var tfld = trim(vfld);
	  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/

	  if (tfld == "" || !email.test(tfld)) {
		  return false;
	  }
	  return true;
}

//OK
//valida num telefono
//formatos validos xxx-xxx-xxxx xxx-xxxxxxx xxxxxxxxxx 
function validateTelnr(param)
{
     if (/^[0-9]{3}-?[0-9]{4}-?[0-9]{4}$/.test(param)){
      return true;
  }else{ return false; }
}


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";
	}
}

//-------------------------------------------------------------------------------
// 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 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;
}


/*
function changeLocation (section, action, action2)
{
	var normal_location = '';
	var seo_location = '';


	if(action2 != ""){
		normal_location = 'http://xng.dyndns.biz/Veritran/site/index.php?section='+section+'&action='+action+'&action2='+action2+'&current_page=1';
		seo_location = '<?php echo $root; ?>' + '/'+ section +'/'+ action +'/'+ action2 +'.html';
	} else {
		if(action != "") {
			normal_location = 'http://xng.dyndns.biz/Veritran/site/index.php?section='+section+'&action='+action+'&current_page=1';
			seo_location = '<?php echo $root; ?>' + '/'+ section + '/'+ action+'.html';
		} else {
			normal_location = 'http://xng.dyndns.biz/Veritran/site/index.php?section='+section+'&current_page=1';
			seo_location = '<?php echo $root; ?>' + '/'+ section + '.html';
		}
	}
	
	if(section=="productos"){
		normal_location = normal_location+'&HID_Product='+action2;
	}

	document.location = normal_location;

	return true;
}
*/

function setBackgroundImage(id_producto, nro)
{   var base_path = document.getElementById('HID_Pth').value;
	var image_src = base_path+'files/products/'+id_producto+'/'+id_producto+'_'+nro+'.gif';
	document.getElementById('product_image_gallery').src = image_src;
}

var id_data ="";
function loadData(form_name, id_name, _url)
{
	id_data = id_name;
	
	if ( document.getElementById('numCli') == undefined ){// en caso de no estar en home	
		dojo.io.bind({
			sync: false,
			url: _url,
			handler: function (type, data, evt) {
						document.getElementById(id_name).innerHTML = data;
						if (type == 'error') {
							document.getElementById(id_name).innerHTML = "...";
						} else {
							document.getElementById(id_name).innerHTML = data;
						}
					},
			
				  content: {
					numNov: document.getElementById('numNov').value,
					//HID_Pos: document.getElementById('HID_Pos').value,
					direction: document.getElementById('direction').value,
					language: document.getElementById('languageNov').value
				},		
			
			error: function(type, error){
				document.getElementById(id_name).innerHTML = "..."+error;
			}
		});
	}else{
			dojo.io.bind({
				sync: false,
				url: _url,
				handler: function (type, data, evt) {
							document.getElementById(id_name).innerHTML = data;
							if (type == 'error') {
								document.getElementById(id_name).innerHTML = "...";
							} else {
								document.getElementById(id_name).innerHTML = data;
							}
						},
				
					  content: {
						numNov: document.getElementById('numNov').value,
						//HID_Pos: document.getElementById('HID_Pos').value,
						direction: document.getElementById('direction').value,
						numCli: document.getElementById('numCli').value,
						sentido: document.getElementById('sentido').value,
						language: document.getElementById('languageNov').value
					},		
				
				error: function(type, error){
					document.getElementById(id_name).innerHTML = "..."+error;
				}
			});
	      }
		
	/*
	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);
	*/
}
function openTarget (form, features, windowName) {
  if (!windowName)
    windowName = 'formTarget' + (new Date().getTime());
  form.target = windowName;
  open ('', windowName, features);
}
function hideDiv() {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById('hideShow').style.visibility = 'hidden';
	}
	else {
		if (document.layers) { // Netscape 4
			document.hideShow.visibility = 'hidden';
		}
		else { // IE 4
		document.all.hideShow.style.visibility = 'hidden';
		}
	}
}
function showDiv() {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById('hideShow').style.visibility = 'visible';
	}
	else {
		if (document.layers) { // Netscape 4
			document.hideShow.visibility = 'visible';
		}
		else { // IE 4
			document.all.hideShow.style.visibility = 'visible';
		}
	}
}

//valida los campos en el formulario de contacto
//validates the fields of the contact form
//Aviso: los espacios al inicio del campo no son considerados error
//si el texto que lo sigue es valido al campo correspondiente
function checkFormContact()
{
	var incomplete_fields = ""; // almacena el listado de campos obligatorios vacíos
        var invalid_fields = "";//almacena la lista de campos con datos invalidos
        
        //OK
        //checkeo si el campo nombres esta vacío   
        if( trim(document.getElementById('str_fname').value) == ""){ 
	  incomplete_fields = incomplete_fields + ' First Name' + '\n';}
	 //OK      
        //checkeo si el campo apellido esta vacío 
        if( trim(document.getElementById('str_lname').value) == ""){ 
          incomplete_fields = incomplete_fields + ' Last Name' + ' \n';
        }
        //OK
        //checkeo si campo mail esta vacio  verifico el dato ingresado
        if(trim(document.getElementById('str_email').value) == ""){
           incomplete_fields = incomplete_fields + ' E-Mail' + '\n';
         }else     if (!validateEmail(document.getElementById('str_email').value)){
                     invalid_fields= invalid_fields + '  E-mail \n';}
           
        //OK
        //checkeo si campo telefono esta vacío , verifico validez de datos
        if(trim(document.getElementById('str_phone').value)==""){ 
                incomplete_fields = incomplete_fields + ' Phone number ' + '\n';
        }else  if(!validateTelnr(document.getElementById('str_phone').value)){  
                 invalid_fields = invalid_fields + ' Phone Number (ej: 011-5555-6666 / 01155556666) \n'; }

       
            
      //Muestro mensajes de error en ventana emergente de campos o datos inválidos      
      if(incomplete_fields != ""){ //mensaje de alerta uno o mas campos vacios
	        incomplete_fields = 'Please, complete following fields:\n' + incomplete_fields;
		alert(incomplete_fields);
		return false;
	}else if  ( invalid_fields !=""){ 
                    invalid_fields= ' Following fields have wrong data \n' + invalid_fields;
                    alert (invalid_fields);
                    return false;

	}else return true; 
    
}

//********* CHECLFORMCONTACT VERSION EN ESPA�OL **********************
//valida los campos en el formulario de contacto
//validates the fields of the contact form
//Aviso: los espacios al inicio del campo no son considerados error
//si el texto que lo sigue es valido al campo correspondiente
function checkFormContactEsp()
{
	var incomplete_fields = ""; // almacena el listado de campos obligatorios vacíos
        var invalid_fields = "";//almacena la lista de campos con datos invalidos
        
        //OK
        //checkeo si el campo nombres esta vacío   
        if( trim(document.getElementById('str_fname').value) == ""){ 
	  incomplete_fields = incomplete_fields + ' Nombres' + '\n';}
	 //OK      
        /*
        //checkeo si el campo apellido esta vacío 
        if( trim(document.getElementById('str_lname').value) == ""){ 
          incomplete_fields = incomplete_fields + ' Apellido' + ' \n';
        }
        */
        //OK
        //checkeo si campo mail esta vacio  verifico el dato ingresado
        if(trim(document.getElementById('str_email').value) == ""){
           incomplete_fields = incomplete_fields + ' E-Mail' + '\n';
         }else     if (!validateEmail(document.getElementById('str_email').value)){
                    invalid_fields= invalid_fields + '  E-mail \n';}
                 
        //OK
        //checkeo si campo telefono esta vacío , verifico validez de datos
        if(trim(document.getElementById('str_phone').value)==""){ 
                incomplete_fields = incomplete_fields + ' Numero de telefono ' + '\n';
        }
        /*
        else  if(!validateTelnr(document.getElementById('str_phone').value)){  
                 invalid_fields = invalid_fields + ' Numero de telefono (ej: 011-5555-6666 / 01155556666) \n'; }
		*/
       
            
      //Muestro mensajes de error en ventana emergente de campos o datos inválidos      
      if(incomplete_fields != ""){ //mensaje de alerta uno o mas campos vacios
	        incomplete_fields = 'Por Favor complete los campos obligatorios:\n' + incomplete_fields;
		alert(incomplete_fields);
		return false;
	}else if  ( invalid_fields !=""){ 
                    invalid_fields= ' Los siguientes campos tienen datos invalidos \n' + invalid_fields;
                    alert (invalid_fields);
                    return false;

	}else return true; 
    
}



function clearForm(){
	document.formContact.reset();
}


