// Function scripts
///////////////////////////
// uso geral 
////////////////////////////
function Trim(str){return str.replace(/^\s+|\s+$/g,"");}
////////////////////////////////////////////////////////


function conteudo(page,path_info) {
//  set_path_info (path_info);
  var serverPage = page;
  var areaID = "contentID";
  var str = "";
  processajax ("GET",serverPage,areaID, "");
}
function show_contato () {
 var serverPage = "contato.php";
  var areaID = "main";
  var str = "";
  processajax ("GET",serverPage,areaID, "");
}

//Conteudo para produto
function prodContent(page,path) {
  var serverPage = path+'conteudo/produtos/'+page+'.php?realod=true&ph='+path;
  var areaID = "contentID";
  var str = "";
  processajax ("GET",serverPage,areaID, "");
}
function show_acessorios (file) {
 var serverPage = "acessorios/"+file+".php";
  var areaID = "main";
  var str = "";
  processajax ("GET",serverPage,areaID, "");
}
function show_caracteristicas (file) {
 var serverPage = file+".php";
  var areaID = "main";
  var str = "";
  processajax ("GET",serverPage,areaID, "");
}
function show_catalogos (file) {
 var serverPage = "catalogos/"+file+".php";
  var areaID = "main";
  var str = "";
  processajax ("GET",serverPage,areaID, "");
}
function show_big (file) {
   var target = '../../includes/show_bigpicture.php?reload=true&file='+file;
   win = window.open(target,'PrintWindow','menubar=no,status=yes,location=no,width=700,height=400,resizable=yes,toolbar=no,scrollbars=yes');
   win.opener = self;
   win.window.focus();
}
function cotacao (CodProd) {
   var target = '../../contatoform1.php?reload=true&cot=1&cp='+CodProd;
   win = window.open(target,'WinCotacao','menubar=no,status=yes,location=no,width=480,height=520,resizable=yes,toolbar=no,scrollbars=yes');
   win.opener = self;
   win.window.focus();
}
function cotacao_produto (page) {
   var target = '../../contato/'+page;
   win = window.open(target,'WinCotacao','menubar=no,status=yes,location=no,width=480,height=580,resizable=yes,toolbar=no,scrollbars=yes');
   win.opener = self;
   win.window.focus();
}

function chgEspChar(valor){
	//Substituir caracteres especiais com acento para sem acento
var char_espin = ['à','è','ì','ò','ù','á','é','í','ó','ú','â','ê','î','ô','û','ä','ë','ï','ö','ü','ã','õ','ç','Ç','À','È','Ì','Ò','Ù','Á','É','Í','Ó','Ú','Â','Ê','Î','Ô','Û','Ä','Ë','Ï','Ö','Ü','Ã','Õ'];
var char_espout =['a','e','i','o','u','a','e','i','o','u','a','e','i','o','u','a','e','i','o','u','a','o','c','C','A','E','I','O','U','A','E','I','O','U','A','E','I','O','U','A','E','I','O','U','A','O'];
var retorno = valor;
for (i=0;i<46;i++){
	while(retorno.indexOf(char_espin[i]) >= 0) retorno = retorno.replace(char_espin[i],char_espout[i]);
}

return retorno;
}

function bookmark(tipo){
   var titulo = unescape(document.title);
   while(titulo.indexOf(" ") >= 0) titulo = titulo.replace(" ","+");
   titulo = chgEspChar(titulo);
   switch (tipo) {
	   case "google" :
			target = "http://www.google.com/bookmarks/mark?op=edit&amp;bkmk="+window.location+"&title="+titulo;
    		win = window.open(target,'Wingoogle','menubar=yes,status=yes,location=yes,resizable=yes,toolbar=yes,scrollbars=yes');
			break;			
	   case "yahoo" :
			target = "http://myweb2.search.yahoo.com/myresults/bookmarklet?t="+titulo+"&u="+window.location;
    		win = window.open(target,'WinYahoo','menubar=yes,status=yes,location=yes,resizable=yes,toolbar=yes,scrollbars=yes');
			break;
	   case "winlive" :
			target = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;mkt=en-us&amp;url="+window.location+"&amp;title="+titulo;
    		win = window.open(target,'WinLive','menubar=yes,status=yes,location=yes,resizable=yes,toolbar=yes,scrollbars=yes');
			break;
	   case "delicious" :
			target = "http://del.icio.us/post?url="+window.location+"&title="+titulo;
    		win = window.open(target,'WinDelicious','menubar=yes,status=yes,location=yes,resizable=yes,toolbar=yes,scrollbars=yes');
			break;
	   case "digg" :
			target = "http://digg.com/submit?phase=2&url="+window.location+"&title="+titulo;
    		win = window.open(target,'WinDigg','menubar=yes,status=yes,location=yes,resizable=yes,toolbar=yes,scrollbars=yes');
			break;
   }

    win.opener = self;
	win.window.focus();
	
}


//--------------------------------------
//  EP SERIES
//--------------------------------------
function activateWaitingMessage (areaID,path) {
  var obj = document.getElementById(areaID);
/*  obj.innerHTML = "<img src=\""+path+"imgs/ajax-loader.gif\" >Aguarde..."; */
  obj.innerHTML = "<img src=\"/imgs/ajax-loader.gif\" >Aguarde...";

}

function chgAjaxMnBottom(ref){
  document.getElementById('formquickcontactID').style.visibility = "hidden";
  var areaID = 'imgcentral';
  var page = "epseries/content.php?ref="+ref;
  if(document.getElementById("pnl001")) {
    closePanel('pnl001');
  }
    var str = "";
    activateWaitingMessage (areaID,"");
    processajax ("GET",page,areaID, "");


}

//-------------------------------------
// Mostrar acessorios
//-------------------------------------
function openViewAcessorio(ref){
  var areaID="pnl001";
      openPanel(376,225,areaID);
      page = "epseries/openViewAcessorios.php?ref="+ref;
      activateWaitingMessage (areaID,"");
      processajax ("GET",page,areaID, "");

}
//----------------------------------------------------
//  PopUp Panel
//----------------------------------------------------
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function pageWidth() {
  return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}
function pageHeight() {
 return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

////////////////////////////////////////////////////
//  Reduzir Window Panel
/////////////////////////////////////////////////////
function zoonInPanel (areaID,width,height){
 var theObject = document.getElementById(areaID);
 theObject.style.height = height + "px";
 theObject.style.width = width + "px";
}

//////////////////////////////////////////////////////
/////// Panel de edição
//////////////////////////////////////////////////////
function openPanel(panelWidth,panelHeight,areaID) {
 var theObject = document.getElementById(areaID);

 //////////// calcula posicionamento /////////////
 var xyscroll = getScrollXY();
 var offsetX = xyscroll[0];
 var offsetY = xyscroll[1];
 var posx = 0;
 var posy = 0;
 var larguraPagina = pageWidth();
 var alturaPagina = pageHeight();
 if (larguraPagina <= panelWidth) {
	posx = 0;
 }else {
    posx = larguraPagina - panelWidth;
    posx = ((posx/2) + offsetX);
 }
 posy = offsetY + 290;
 posx = posx + 150;
 //////////////////////////////////////

 theObject.style.visibility = "visible";
 theObject.style.height = panelHeight + "px";
 theObject.style.width = panelWidth + "px";

 theObject.style.left = posx + "px";
 theObject.style.top = posy + "px";

}

function guia_produto() {
   var target = 'ep450s-tour.htm';
   win = window.open(target,'guiaProduto','menubar=no,status=yes,location=no,width=770,height=620,resizable=yes,toolbar=no,scrollbars=yes');
   win.opener = self;
   win.window.focus();
}

function closePanel(areaID) {
 var theObject = document.getElementById(areaID);

 theObject.style.visibility = "hidden";
 theObject.style.height = "0px";
 theObject.style.width = "0px";
}

function chgphoto(ref){
 if (document.getElementById('phonetype').innerHTML == '1'){
    document.getElementById('photo-ep450s').innerHTML = '<img src="imgs/flash/ep450_'+ref+'.jpg" width="350" height="360" alt="" name="EP450S">';
 }else {
    document.getElementById('photo-ep450s').innerHTML = '<img src="imgs/flash/ep450s_sem_display_'+ref+'.jpg" width="350" height="360" alt="" name="EP450S">';
 }
 //-Limpara temporizador animacao
 clearTimeout(timeout_state);
 timeout_state=null;

}
function chgphonetype(ref){
  if (document.getElementById('phonetype').innerHTML != ref){
    document.getElementById('phonetype').innerHTML = ref;
    chgphoto(1);
    thumbphotos(ref);
  }
 //-Limpara temporizador animacao
 clearTimeout(timeout_state);
 timeout_state=null;
 imageLoad(ref);
}

function thumbphotos(ref){
   var serverPage = "includes/menuthumbs.php?ref="+ref;
   var areaID = 'thumbphotos';
   var str = "";
   processajax ("GET",serverPage,areaID, "");
}
//------------------------
// ANIMACAO
//------------------------
function imageLoad(ref) {
    frame=0;
    for(var i=1; i<7; i++) {
    anims[i-1]=new Image();
        if (ref == '1'){
            anims[i-1].src="imgs/flash/ep450_" + i + ".jpg";
        } else {
            anims[i-1].src="imgs/flash/ep450s_sem_display_" + i + ".jpg";
        }
    }
    animator();
}
function animator() {
    var tsegundos = 3;
    document.EP450S.src = anims[frame].src;
    frame=frame+1;
    if (frame>5) frame=0;
    timeout_state=setTimeout("animator()",500*tsegundos);
}
///////////////////////////
// News Letter
////////////////////////////
function cadastroNewsLetter(tipo){
  var newsname = Trim(document.fnewsletter.news_name.value);
  var newsemail = Trim(document.fnewsletter.news_email.value);
  var msg_usr = "";
  var erro = false;

  if ((newsname == "") || (newsemail == "")){
   erro = true; msg_usr = ">> Por favor preencher nome e email \n";

  }else {
      if (!(email_valido = is_email(newsemail))) { erro = true; msg_usr += ">> Email incorreto \n";}

  }
  if (!erro){
        document.fnewsletter.action.value = tipo;
		var serverPage = "newsletter/cadastonewsletter.php";
        var areaID = 'newsLetter';
		var str = getformvalues(document.getElementById('fnewsletter'));
		activateWaitingMessage (areaID,'');
		processajax ("POST",serverPage,areaID, str);
  }else {
    alert(msg_usr);
  }
}
