// Common JavaScript functions
// Author: Gerd Riesselmann
// http://www.gerd-riesselmann.net
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//



// After loading page, do what should be done

addEvent(window, 'load', initFocus);

// Ads eventhandelr to given object
function addEvent(obj, evType, fn)
{
    if (obj.addEventListener)
    {
           obj.addEventListener(evType, fn, false);
           return true;
    } 
    else if (obj.attachEvent)
    {
           var r = obj.attachEvent("on"+evType, fn);
           return r;
     } 
     else 
     {
           return false;
     }
}

// Sets focus to first input with class "focus"
function initFocus()
{
    if (findFocus('input'))
        return;
        
    if (findFocus('select'))
        return;
        
    if (findFocus('textarea'))
        return;
}

function findFocus(elementName)
{
    var inputs = document.getElementsByTagName(elementName);
    for (var i = 0; i < inputs.length; i++)
    {
        if (inputs[i].className.indexOf('focus') != -1 )
        {
            inputs[i].focus();
            return true;            
        }
    }
    return false;
}
// Drop-in content box- By Dynamic Drive
// For full source code and more DHTML scripts, visit http://www.dynamicdrive.com
// This credit MUST stay intact for use

var ie=document.all
var dom=document.getElementById
var ns4=document.layers
var calunits=document.layers? "" : "px"

var bouncelimit=32 //(must be divisible by 8)
var direction="up"

function initbox(){
if (!dom&&!ie&&!ns4)
return
crossobj=(dom)?document.getElementById("dropin").style : ie? document.all.dropin : document.dropin
scroll_top=(ie)? truebody().scrollTop : window.pageYOffset
crossobj.top=scroll_top-250+calunits
crossobj.visibility=(dom||ie)? "visible" : "show"
dropstart=setInterval("dropin()",50)
}

function dropin(){
scroll_top=(ie)? truebody().scrollTop : window.pageYOffset
if (parseInt(crossobj.top)<80+scroll_top)
crossobj.top=parseInt(crossobj.top)+40+calunits
else{
clearInterval(dropstart)
bouncestart=setInterval("bouncein()",50)
}
}

function bouncein(){
crossobj.top=parseInt(crossobj.top)-bouncelimit+calunits
if (bouncelimit<0)
bouncelimit+=8
bouncelimit=bouncelimit*-1
if (bouncelimit==0){
clearInterval(bouncestart)
}
}

function dismissbox(){
if (window.bouncestart) clearInterval(bouncestart)
crossobj.visibility="hidden"
}

function truebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}



function doPopup() {
	//initbox();
	window.open ('http://www.tuttomod.com/land_natale.html', '_blank');
}

function maxLeng(field,maxChars,ev)
{
    if(window.event) // IE
    {
       if(field.value.length >= maxChars) {
          event.returnValue=false;
          return false;
          }
    }
    else  // Netscape/Firefox/Opera
    {
  
       var charCode = ev.charCode;
        if(field.value.length >= maxChars) 
        {
        //alert(ev.keyCode);
            if(ev.keyCode==13)
            {
                return false;
            }
            else if (ev.keyCode==8 || ev.keyCode==16 || ev.keyCode==17 || ev.keyCode==18 || ev.keyCode==19 || ev.keyCode==20
            || ev.keyCode==27 || ev.keyCode==33 || ev.keyCode==34 || ev.keyCode==35 || ev.keyCode==36 || ev.keyCode==37 
            || ev.keyCode==38 || ev.keyCode==39 || ev.keyCode==40 || ev.keyCode==45 || ev.keyCode==46) 
            {
               return true; 
            }              
            else
            {
                ev.preventDefault();
                return false;
            }
            } 
  
    }
}

 function maxLengthPaste(field,maxChars)
 {
// server a limitare  la len da clipboard 
       
    if(window.event) // IE
    {
       event.returnValue=false;
    
       if((field.value.length +  window.clipboardData.getData("Text").length) > maxChars) 
       {
         return false;
       }
       event.returnValue=true;
    }
    else 
    return false;
 }
function checkmax_word_wrapper(id,maxlen)
{

    var testo = document.getElementById(id).value;
    //document.getElementById("ctl00_ContentPlaceHolder1_
    if (testo == "" || testo == "undefined" || testo.charAt(0) == " ")
    {
        alert("Inserisci un testo");
        return false;
    }
    else
    {
       // VARIABILI PER:
       // IL CICLO
       // PER LA GESTIONE DEGLI ERRORI
       // PER IL MASSIMO NUMERO DI CARATTERI CONSECUTIVI CONSENTITI
        var i = 0;
        var errore = 0;
        var max_lunghezza = maxlen;
       // DIVIDO IL VALORE DEL CAMPO DEL FORM IN UN ARRAY DI STRINGA PER IL CARATTERE SPAZIO VUOTO
        var s = testo.split(" ");
       // ESEGUO UN CICLO PER CONTROLLARE TUTTI I VETTORI DELL'ARRAY CHE HO CREATO
        while (i<s.length && errore == 0)
        { 
           // VALORIZZO LA VARIABILE errore A SECONDA DELLA LUNGHEZZA DEI VETTORI
            s[i].length > max_lunghezza ? errore = 1 : errore = 0;
           // SE errore == 1 VUOL DIRE CHE C'E' UNA PAROLA TROPPO LUNGA
            if (errore == 1)
            {
                alert("Hai inserito una parola troppo lunga");
                return false;
            }
            i++; // INCREMENTO IL CONTATORE DEL CICLO
        }
       // SE NEL CICLO NON TROVO VETTORI TROPPO LUNGHI VUOL DIRE CHE L'ESITO E' POSITIVO E VEDO ANANTI
        if (errore == 0) 
        {
            //alert("OK");
            return true;
        }
    }
}
function popunder()
{
    var url = "http://www.tuttomod.com/chclose.aspx";
    //var url = "" +  BackOffice.ConfigurationManager.URLBASE + "/chclose.aspx";
    var HFSessionID=document.getElementById("ctl00_HFSessionID").value;
    //alert(HFSessionID);
    var ordine = false;
    if(getCookie('ordinepopunder')=="true")
    {
        // qui non devo far comparire nulla perchè l'utente ha fatto un ordine
        
    }
    else
    {
        if(getCookie('logged')==null)
        {
            setCookie("logged", HFSessionID,1);
            
            window.open(url, "_blank","width=0,height=0");
            this.focus();
            //alert('Questa è la prima volta che entri sul sito. Te lo ricorderemo la prossima volta.');
        }
        else
        {
            // questo vuol dire che non ho chiuso il browser corrente
            // quindi se il sessionid è rimasto invariato
            if(getCookie('logged')==HFSessionID)
            {
                
                window.open(url, "_blank","width=0,height=0");
                this.focus();
            }
            // qui invece sto aprendo un'altro browser
            else
            {

            }
        }
    }
}
function setCookie(name, value,days) {
   var expire = new Date();
   expire.setHours(24,60,60,1000);
   document.cookie = name + "=" + escape(value)
   + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
}

function getCookie(Name) {
   var search = Name + "="
   if (document.cookie.length > 0) { // if there are any cookies
      offset = document.cookie.indexOf(search) 
      if (offset != -1) { // if cookie exists 
         offset += search.length 
         // set index of beginning of value
         end = document.cookie.indexOf(";", offset) 
         // set index of end of cookie value
         if (end == -1) 
            end = document.cookie.length
         return unescape(document.cookie.substring(offset, end))
      } 
   }
}
function showOptionsAttr(layerId,layerTextId) {
	    var layer = document.getElementById(layerId);
	    var layerText = document.getElementById(layerTextId);
	    if (layer.style.display == 'none') 
	    {
		    layer.style.display = 'block';
		    layerText.innerHTML = '[-] Pi&#249; scelte...';
		
	    } 
	    else 
	    {
		    layer.style.display = 'none';
		    layerText.innerHTML = '[+] Pi&#249; scelte...';
		
	    }
	}
	
	function showTestoEstGaranzia(layerId,layerTextId) {
	    var layer = document.getElementById(layerId);
	    var layerText = document.getElementById(layerTextId);
	    if (layer.style.display == 'none') 
	    {
		    layer.style.display = 'block';
		    layerText.innerHTML = '[-]';
		
	    } 
	    else 
	    {
		    layer.style.display = 'none';
		    layerText.innerHTML = '[+] In questa pagina  trovi le condizioni di Garanzia convenzionale...(continua)';
		
	    }
	}			
