/* VALIDAÇÃO DE FORMULÁRIOS */
var DOM = YAHOO.util.Dom;
var EVT = YAHOO.util.Event;

function validate_form(_obj,lg){
  var errors = false;
  if (lg=='pt'){
    var txt_erro = "Preenchimento obrigatório.";
    var txt_erro_email = "Email inválido.";
  } else {
    var txt_erro = "Mandatory field.";
    var txt_erro_email = "Invalid Email";
  }

	DOM.getElementsByClassName(
		'required',
		null,
		_obj,
		function(e){
  			var _tag  = e.tagName;
  			var _type = e.getAttribute('type');
  			var _emai = e.getAttribute('email');
  			var _fich = e.getAttribute('fich');
        var _sel  = e.getAttribute('selecta');
        
        
  			if(_tag == 'TEXTAREA' || _type == 'text' || _type == 'password'){
  				if ((e.value == '') || (e.value == txt_erro)){
            var _varid = e.getAttribute('id');
            e.style.border = '1px solid #635489';
  					e.style.color  = '#645589';
  					e.value = txt_erro;
            
  					EVT.addListener(e, "focus", function(){ e.value = ''; });
  					
  					if(errors == false) errors = true;
  					
  				} else {
  				  var idinput =  e.getAttribute('id');
  				  if (idinput=='confpass') {
  				    var pass1 = document.getElementById('password').value;
  
  				    if (pass1 != e.value){
                alert("As passwords introduzidas não são iguais.")
                
                var _varid = e.getAttribute('id');
                e.style.border = '1px solid #635489';
      					e.style.color  = '#645589';
      					e.value = txt_erro;
      					
      					document.getElementById('password').style.border = '1px solid #635489';
      					document.getElementById('password').style.color  = '#645589';
      					document.getElementById('password').value = txt_erro;
                
      					EVT.addListener(e, "focus", function(){ e.value = ''; });
      					
      					if(errors == false) errors = true;
              } else {
                var _varid = e.getAttribute('id');
      				  e.style.border = 'none';
      					e.style.color  = '#AAAAAA';
      					
      					document.getElementById('password').style.border = 'none';
      					document.getElementById('password').style.color  = '#AAAAAA';
      					
              }
  				  
            } else {
    				  var _varid = e.getAttribute('id');
    				  e.style.border = 'none';
    					e.style.color  = '#AAAAAA';
    				}
  				}
  				
  				if(_emai == 1){
  					if(verify_email(e.value) == false){
  						var _varid = e.getAttribute('id');
              e.style.border = '1px solid #635489';
    					e.style.color  = '#645589';
    					e.value = txt_erro_email;
              
  						EVT.addListener(e, "focus", function(){ e.value = ''; });
  						
  						if(errors == false) errors = true;
  						
  					} else {
  					  var _varid = e.getAttribute('id');
      				e.style.border = 'none';
    					e.style.color  = '#AAAAAA';
  					}
  				}
  			} else if(_fich == 1){
  			  if(e.value == ''){
            var _varid = e.getAttribute('id');
            e.style.border = '1px solid #635489';
  					e.style.color  = '#645589';
  					e.value = txt_erro;
  					EVT.addListener(e, "focus", function(){ e.value = ''; });
  					if(errors == false) errors = true;
          } else {
  					var _varid = e.getAttribute('id');
  				  e.style.border = 'none';
  					e.style.color  = '#AAAAAA';
  				}
  			} else if(_sel == 1) {
          if ((e.value=="") || (e.value==0)){
            var _varid = e.getAttribute('id');
            e.style.border = '1px solid #635489';
  					e.style.color  = '#645589';
						EVT.addListener(e, "focus", function(){ e.value = ''; });
						if(errors == false) errors = true;
          } else {
            var _varid = e.getAttribute('id');
  				  e.style.border = 'none';
  					e.style.color  = '#AAAAAA';
          }
  			} else if(_type == 'checkbox') {
  				if(e.checked == false){
  					alert('Tem de dizer se aceita os termos e condições de funcionamento do site.');
  					if(errors == false) errors = true;
  				}
  			}
  		
		}
	);
	
	if(errors == true){
		return false;
	} else {
		return true;
	}
	
}
/* VALIDAÇÃO DE FORMULÁRIOS */

function showimage(id)
{
  document.getElementById(id).style.display = "block";
}

function hideimage(id)
{
  document.getElementById(id).style.display = "none";
}


//Trocar area 1

function changeCat(imgBig, imgpeq1, imgpeq2, tit, subtit, text){

  document.getElementById('holderPicBig').src = imgBig;
  document.getElementById('imgpeqn1').src = imgpeq1;
  document.getElementById('imgpeqn2').src = imgpeq2;
  document.getElementById('titll').innerHTML = tit;
  document.getElementById('subtitll').innerHTML = subtit;
  document.getElementById('textoo').innerHTML = text;

}

//Trocar area 1

function changeCat2(imgBig, imgpeq1, imgpeq2){

  document.getElementById('holderPicBig').src = imgBig;
  document.getElementById('imgpeqn1').src = imgpeq1;
  document.getElementById('imgpeqn2').src = imgpeq2;

}

//Trocar area 2

function changeFOUR(imgtrocar){
  document.getElementById('trocaraimagem').src = imgtrocar;
}

/* fazer o render das fonts que nao sao do sistema */
function RenderTTF(){

  DATAFUNCS.RenderFonts('destaqueRTTF',{ 
     fontSize         : '13.5',
     backgroundColor  : '000000',
     color            : 'FFFFFF',
     fontFile         : './fonts/MUSEO500.TTF',
     transparent      : '1'
  });
  
  
  DATAFUNCS.RenderFonts('newsletterRTTF',{ 
     fontSize         : '13.5',
     backgroundColor  : 'FFFFFF',
     color            : '1D1D1D',
     fontFile         : './fonts/MUSEO500.TTF',
     transparent      : '1'
  });
  
  
  DATAFUNCS.RenderFonts('h1RTTF',{ 
     fontSize         : '17.25',
     backgroundColor  : 'FFFFFF',
     color            : '564444',
     fontFile         : './fonts/MUSEO300.TTF',
     transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('h3RTTF',{ 
     fontSize         : '17.25',
     backgroundColor  : 'FFFFFF',
     color            : '564444',
     fontFile         : './fonts/MUSEO300.TTF',
     transparent      : '1'
  });
  
  
  DATAFUNCS.RenderFonts('tituloRTTF',{ 
     fontSize         : '17.25',
     backgroundColor  : '1D1D1D',
     color            : 'FFFFFF',
     fontFile         : './fonts/MUSEO300.TTF',
     transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('subtituloRTTF',{ 
     fontSize         : '13.5',
     backgroundColor  : '000000',
     color            : '898B98',
     fontFile         : './fonts/MUSEO300.TTF',
     transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('formularioRTTF',{ 
     fontSize         : '13.5',
     backgroundColor  : 'FFFFFF',
     color            : '898B98',
     fontFile         : './fonts/MUSEO300.TTF',
     transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('tit_prod2',{ 
     fontSize         : '17.25',
     backgroundColor  : 'FFFFFF',
     color            : '564444',
     fontFile         : './fonts/MUSEO500.TTF',
     transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('stit_prod2',{ 
     fontSize         : '13.5',
     backgroundColor  : 'FFFFFF',
     color            : '898B98',
     fontFile         : './fonts/MUSEO300.TTF',
     transparent      : '1'
  });
  
}




// validacao do formulario

var EVT = YAHOO.util.Event;
var OBJ;
var total;
var elem = new Array();

function formErros(obj)
{
  OBJ=obj;
  total=obj.length;
   
  var frmObj=OBJ.form;
  objID=obj.id;
   
    
  elem = frmObj.elements;
 
  for(i = 0; i < elem.length; i++)
  {
  
    if(elem[i].id==objID)
    {
      
      if(objID =='nomeform' || objID =='emailform' || objID =='moradaform')
      {
       //document.getElementById(objID).value='Preenchimento Obrigatório';
       document.getElementById(objID).style.color='#645589';
       document.getElementById(objID).style.border='1px solid #635489';
       
       document.getElementById(objID).onfocus = function(){ document.getElementById(objID).value = ''; };
      }
      
      if(objID =='cpform' || objID =='cp2form' || objID =='localidadeform')
      {
       document.getElementById(objID).style.color='#645589';
       document.getElementById(objID).style.border='1px solid #635489';
      }
      
      
      /*
       if(document.getElementById(objID).value == 'Preenchimento Obrigatório')
       {
          EVT.addListener(document.getElementById(objID), "focus", function(){ document.getElementById(objID).value = ''; });
       }
      */  
    }
  
  }

}

// validacao do formulario de informacao

var EVT = YAHOO.util.Event;
var OBJ;
var total;
var elem = new Array();

function formErros2(obj)
{
  OBJ=obj;
  total=obj.length;
   
  var frmObj=OBJ.form;
  objID=obj.id;
   
    
  elem = frmObj.elements;
 
  for(i = 0; i < elem.length; i++)
  {
  
    if(elem[i].id==objID)
    {
      
      if(objID =='nomeform2' || objID =='emailform2' || objID =='assuntoform2')
      {
       //document.getElementById(objID).value='Preenchimento Obrigatório';
       document.getElementById(objID).style.color='#645589';
       document.getElementById(objID).style.border='1px solid #635489';
       
       document.getElementById(objID).onfocus = function(){ document.getElementById(objID).value = ''; };
      }
      
      
      
      
      /*
       if(document.getElementById(objID).value == 'Preenchimento Obrigatório')
       {
          EVT.addListener(document.getElementById(objID), "focus", function(){ document.getElementById(objID).value = ''; });
       }
      */  
 
    
    }
  
  }

}





//IMAGE TO GRAY
function prepareMouseOverImage(image, originalURL, button)
{
 button.mouseOverImage=originalURL;
 image.onload=function(){        
    return true;    
  };
 button.normalImage=grayscale(image, false);
 
 button.onmouseover=function()
 {
 //alert("a");
  image.src=this.mouseOverImage;
 }
 
 button.onmouseout=function()
 {
// alert(this.normalImage.src);
  image.src=this.normalImage;  
 }
 image.src=button.normalImage; 
}
 

function grayscale(image, bPlaceImage)
{
  var myCanvas=document.createElement("canvas");
  var myCanvasContext=myCanvas.getContext("2d");
 
  var imgWidth=image.width;
  var imgHeight=image.height;
  // You'll get some string error if you fail to specify the dimensions
  myCanvas.width= imgWidth;
  myCanvas.height=imgHeight;
//  alert(imgWidth);
  myCanvasContext.drawImage(image,0,0);
  // this function cannot be called if the image is not rom the same domain.  You'll get security error
  var imageData=myCanvasContext.getImageData(0,0, imgWidth, imgHeight);
  for (i=0; i<imageData.height; i++)
  {
    for (j=0; j<imageData.width; j++)
    {
   var index=(i*4)*imageData.width+(j*4);
   var red=imageData.data[index];   
   var green=imageData.data[index+1];
   var blue=imageData.data[index+2];   
   var alpha=imageData.data[index+3];  
   var average=(red+green+blue)/3;    
      imageData.data[index]=average;   
      imageData.data[index+1]=average;
      imageData.data[index+2]=average;
      imageData.data[index+3]=alpha;      
 }
  }
  myCanvasContext.putImageData(imageData,0,0,0,0, imageData.width, imageData.height);
 // myCanvasContext.drawIMage(imageData,0,0);//,0,0, imageData.width, imageData.height);  
  
  if (bPlaceImage)
  {  
   var myDiv=document.createElement("div");  
   myDiv.appendChild(myCanvas);
   image.parentNode.appendChild(myCanvas);//, image);
  }
  return myCanvas.toDataURL();
}
 
function imagetoGray (elemento){  
  elemento.style.filter = "gray";
}
 
function imagetoNormal (elemento){
  elemento.style.filter = "none";
}
 
function openVid(id,width,height){    
  window.open('http://www.redicom.pt/open_videos.php?id=' + id,'Redicom','width='+width+',height='+height+',toolbar=0,menubar=0,scrollbars=0,status=0,resizable=0,top=200,left=200');
}
 
function muda_pic(hide,show,peq){
  var pq1 = '';
  
  if(document.getElementById('dimg1').style.display=='block'){
    pq1 = 1;
    document.getElementById('dimg1').style.display = 'none';
  } else if (document.getElementById('dimg2').style.display=='block'){
    pq1 = 2;
    document.getElementById('dimg2').style.display = 'none';
  } else {
    pq1 = 3;
    document.getElementById('dimg3').style.display = 'none';
  }
  
  document.getElementById(hide).style.display = 'none';
  document.getElementById(show).style.display = 'block';
  document.getElementById(peq+pq1).style.display = 'block';
  
}

String.prototype.trim = function()
{
  return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function valida_newsl(){
  var u = document.getElementById('newsletter').value.trim();
  
  if ((u!='') && (verify_email(document.getElementById('newsletter').value)==true)) {
    return true;
  } else {
    alert('Tem que colocar um email válido.');
    return false;
  }
}

function verify_email(_mail){
	
  var status = false;     
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	
  if (_mail.search(emailRegEx) == -1) {
		status = false;
	} else {
		status = true;
	}
	return status;
}

function changeFOUR2(imgtrocar,imgpeq,bt,tot,browser,srcimgpeq){
  var i;
  //alert("teste 1");
  for(i=0; i<tot; i++){
    //alert('bt_'+i);
    if(document.getElementById('bt_'+i).className=='EssenciaMenuSel') {
      if(browser=='IE') {
        //alert('IE');
        document.getElementById('bt_'+i).onmouseover = function(){ imagetoNormal(document.getElementById('img_'+i)); };
        document.getElementById('bt_'+i).onmouseout = function(){ imagetoGray(document.getElementById('img_'+i)); };
        imagetoGray(document.getElementById('img_'+i));
      } else {
        //alert('FF');
        document.getElementById('bt_'+i).onmouseover = function(){  };
        document.getElementById('bt_'+i).onmouseout = function(){  };
        prepareMouseOverImage(document.getElementById('img_'+i), document.getElementById('img_'+i).src, document.getElementById('bt_'+i));
      }
      document.getElementById('bt_'+i).className='EssenciaMenu';
    }
  }
  //alert('teste 2')
  if(browser=='IE') {
    imagetoNormal(document.getElementById(imgpeq));
    document.getElementById(bt).onmouseover = function(){  };
    document.getElementById(bt).onmouseout = function(){  };
  } else {
    document.getElementById(bt).onmouseover = function(){  };
    document.getElementById(bt).onmouseout = function(){  };
    document.getElementById(imgpeq).src = srcimgpeq;
  }
  //alert("teste 3");
  document.getElementById(bt).className= 'EssenciaMenuSel';
  document.getElementById('trocaraimagem').src = imgtrocar;
  
}
