var searchby="";
function checkForm(base,action) {
    var f=document.getElementById("searchFormAction");
    var textstring= f.query.value;
    var tmp = f.view;
    var target = tmp.options[tmp.selectedIndex].value;
    if(textstring == '') { alert('Inserire almeno una parola per la ricerca'); return false; }
    if (base.substring(base.length-1, base.length) =='/'){
        base=base.substring(0,base.length-1);
    }

    switch(target) { 
        case "web":
            window.location.href=base+"/web?q="+escape(textstring)+"&hl=it&start=0";
        break;
        case "shopping":
            window.location.href="http://shopping.repubblica.it/search.aspx?search="+escape(textstring);
        break;
        case "annunci":
            window.location.href="http://annunci.repubblica.it/italia/tutti/-/ricerca-"+escape(textstring)+"?ref=repsearch";
            break;
        default:
            window.location.href=base+"/"+action+"?query="+escape(textstring)+"&view="+target;
    }
}


var searchby="";
function checkFormBase(base,branduri) {
    var f=document.getElementById("searchFormAction");
    var textstring= f.query.value;
    var tmp = f.view;
    var target = tmp.options[tmp.selectedIndex].value;
    if(textstring == '') { alert('Inserire almeno una parola per la ricerca'); return false; }
    if (base.substring(base.length-1, base.length) =='/'){
        base=base.substring(0,base.length-1);
    }


    var replaceString="";
    for(count=0; count < textstring.length; count++){
        if (textstring.charAt(count)==' '){
            replaceString=replaceString+textstring.charAt(count).replace(" ","+");
        }else{
            replaceString=replaceString+textstring.charAt(count);
        }
    }
    
    textstring= replaceString;

    switch(target) { 
        case "web":
            window.location.href=base+"/web?q="+escape(textstring)+"&hl=it&start=0";
        break;
        case "shopping":
            window.location.href="http://shopping."+branduri+".repubblica.it/";
        break;
        case "annunci":
            window.location.href="http://annunci."+branduri+".it/italia/tutti/-/ricerca-"+escape(textstring)+"?ref=repsearch";
            break;
        default:
            // window.location.href=f.action+"?query="+escape(textstring)+"&view="+target;
	    var browserName = navigator.appName;
	    
	    if ( browserName !="Netscape" ) {
           if ( f.action.substring(0,1)=="/" ) {
                  window.location.href=f.action+"?query="+textstring+"&view="+target;
           }
           else {
                  window.location.href="/"+f.action+"?query="+textstring+"&view="+target;
           }
       } else {
            window.location.href=f.action+"?query="+textstring+"&view="+target;
       }
    }
}








function checkFormAdv(base,action)
{
    
    var f=document.getElementById("searchFormAction");
    
    // keyword search
    var query= f.query.value;
    // view
    var view = f.view.options[f.view.selectedIndex].value;
    
    
    // fromdate
    var fromDay= f.from_dd.options[f.from_dd.selectedIndex].value;
    var fromMonth= f.from_mm.options[f.from_mm.selectedIndex].value;
    var fromYear= f.from_yyyy.options[f.from_yyyy.selectedIndex].value;
    
    // todate
    var toDay= f.to_dd.options[f.to_dd.selectedIndex].value;
    var toMonth= f.to_mm.options[f.to_mm.selectedIndex].value;
    var toYear= f.to_yyyy.options[f.to_yyyy.selectedIndex].value;

    var fromdate= fromYear+"-"+fromMonth+"-"+fromDay
    var todate= toYear+"-"+toMonth+"-"+toDay


    var status= true;
    // search type 
    var mode="";
    var switchD="";
    if (todate < fromdate){
        switchD= fromdate;
        fromdate=todate;
        todate=switchD;
        //status= false;
    }
    
    /*
    if (todate== fromdate){
        fromdate="";
        todate="";
    }
    */
    
    // Skav modified
    if (todate == fromdate){
        fromdate=fromdate;
        todate=todate;
    }
    
    // Loop from zero to the one minus the number of radio button selections
    for (counter = 0; counter < f.mode.length; counter++)
    {
        // If a radio button has been selected it will return true
        // (If not it will return false)
        if (f.mode[counter].checked){
        mode = f.mode[counter].value; 
        }
    }

    
    /*
    if (base.lastIndexOf('/')!=-1){
        base=base.substring(0,base.length-1);
        }
     */
     
    
    if (base.substring(base.length-1, base.length) =='/'){
        base=base.substring(0,base.length-1);
    }
    // author
    var author = f.firma.value;
    
    if(query == '' && author== '') { alert('Inserire almeno una parola per la ricerca'); return false; }    
    
        
    switch(view) { 
        case "web":
            //window.location.href="http://test.ricerca.repubblica.it/web?q="+escape(query)+"&hl=it&start=0";
            window.location.href=base+"/web?q="+escape(query)+"&hl=it&start=0";
        break;
        case "shopping":
            window.location.href="http://shopping.repubblica.it/search.aspx?search="+escape(query);
        break;
        case "annunci":
            window.location.href="http://annunci.repubblica.it/italia/tutti/-/ricerca-"+escape(query)+"?ref=repsearch";
            break;
        default:
            window.location.href=base+"/"+action+"?query="+unescape(query)+"&view="+escape(view)+"&fromdate="+escape(fromdate)+"&todate="+escape(todate)+"&mode="+escape(mode)+"&author="+escape(author);
    }    
     
     
     return false; 
    

   
}



function checkFormAdvBase(base,branduri)
{
    
    var f=document.getElementById("searchFormAction");
    
    // keyword search
    var query= f.query.value;
    // view
    var view = f.view.options[f.view.selectedIndex].value;
    
    
    // fromdate
    var fromDay= f.from_dd.options[f.from_dd.selectedIndex].value;
    var fromMonth= f.from_mm.options[f.from_mm.selectedIndex].value;
    var fromYear= f.from_yyyy.options[f.from_yyyy.selectedIndex].value;
    
    // todate
    var toDay= f.to_dd.options[f.to_dd.selectedIndex].value;
    var toMonth= f.to_mm.options[f.to_mm.selectedIndex].value;
    var toYear= f.to_yyyy.options[f.to_yyyy.selectedIndex].value;

    var fromdate= fromYear+"-"+fromMonth+"-"+fromDay
    var todate= toYear+"-"+toMonth+"-"+toDay


    var status= true;
    // search type 
    var mode="";
    var switchD="";
    if (todate < fromdate){
        switchD= fromdate;
        fromdate=todate;
        todate=switchD;
        //status= false;
    }
    
    /*
    if (todate== fromdate){
        fromdate="";
        todate="";
    }*/
    
    // Skav modified
    if (todate == fromdate){
        fromdate=fromdate;
        todate=todate;
    }
    
    // Loop from zero to the one minus the number of radio button selections
    for (counter = 0; counter < f.mode.length; counter++)
    {
        // If a radio button has been selected it will return true
        // (If not it will return false)
        if (f.mode[counter].checked){
        mode = f.mode[counter].value; 
        }
    }

    
   
    if (base.substring(base.length-1, base.length) =='/'){
        base=base.substring(0,base.length-1);
    }
    // author
    var author = f.firma.value;
    
    if(query == '' && author== '') { alert('Inserire almeno una parola per la ricerca'); return false; }    
    
    
    var replaceString="";
    for(count=0; count < query.length; count++){
        if (query.charAt(count)==' '){
            replaceString=replaceString+query.charAt(count).replace(" ","+");
        }else{
            replaceString=replaceString+query.charAt(count);
        }
    }
    query= replaceString;
    
    
    var authorReplace="";
    for(count=0; count < author.length; count++){
        if (author.charAt(count)==' '){
            authorReplace=authorReplace+author.charAt(count).replace(" ","+");
        }else{
            authorReplace=authorReplace+author.charAt(count);
        }
    }
    author=authorReplace;
    
    
    //alert('action: '+f.action);
        
    switch(view) { 
        case "web":
            //window.location.href="http://test.ricerca.repubblica.it/web?q="+escape(query)+"&hl=it&start=0";
            window.location.href=base+"/web?q="+escape(query)+"&hl=it&start=0";
        break;
        case "shopping":
            window.location.href="http://shopping."+branduri+".repubblica.it/";
        break;
        case "annunci":
            //window.location.href="http://annunci.repubblica.it/italia/tutti/-/ricerca-"+escape(query)+"?ref=repsearch";
            window.location.href="http://annunci."+branduri+".it/italia/tutti/-/ricerca-"+escape(query)+"?ref=repsearch";
            break;
        default:
            window.location.href=f.action+"?query="+unescape(query)+"&view="+escape(view)+"&fromdate="+escape(fromdate)+"&todate="+escape(todate)+"&mode="+escape(mode)+"&author="+escape(author);
    }    
     
     
     return false; 
    

   
}



function orderForm()
{
    var f=document.getElementById("orderForm");
    var textstring= f.query.value;
    var tmp = f.sortby;
    var qs = f.qs.value;
    var target = tmp.options[tmp.selectedIndex].value;
    //f.submit();
}

function orderFormNew(base, action)
{
    var f=document.getElementById("orderForm");
    var qs = f.qs.value;
    var tmp = f.sortby;
    var target = tmp.options[tmp.selectedIndex].value;
    var indexsort = qs.indexOf('&sortby=');
    if (indexsort != -1){
        var qs_pre= qs.substring(0,indexsort);
        var qs_post= qs.substring(indexsort+8, qs.length);
        var indexpost= qs_post.indexOf('&');
        if (indexpost != -1){
            qs_post=qs_post.substring(indexpost+1, qs_post.length);
            qs= qs_pre+"&"+qs_post;
        }else{
            qs=qs_pre;
        } 
    }
    
    if (base.lastIndexOf('/')!=-1){
        base=base.substring(0,base.length-1);
    }
    window.location.href=base+"/"+action+"?"+qs+"&sortby="+target;
    
    
    //window.location.href=base+"/"+action+"?"+qs;
    
    //f.submit();
}

function newtoggle(targetId) {
    if (document.getElementById){
        if (document.getElementById( targetId ) != null){
            target = document.getElementById( targetId );
            if (target.style.display == "none"){
                // document.getElementById('debug1').innerHTML = "si: " + document.getElementById('networkpopup').style.display;
                target.style.display = "block";
                //TO=setTimeout('document.getElementById(\'networkpopup\').style.display=\'none\'',5000);
            } else {
                //document.getElementById('debug1').innerHTML = "no: " + document.getElementById('networkpopup').style.display;
                target.style.display = "none";
            }
        }
    }
}


function toggleMore(id, linkId, more, less) {
    var div = byId(id);
    var link = byId(linkId);
    if (div) {
        if (div.style.display == "none") {
            div.style.display = "block";
            if (link) {
                link.innerHTML = less;
            }
        } else {
            div.style.display = "none";
            if (link) {
                link.innerHTML = more;
            }
        }
    }
}


function byId(id) {
    return document.getElementById ? document.getElementById(id) : false;
}

var statusAdv=false;

/*
function searchadvanced(mostra) {
    var damostrare = byId(mostra);
    var submitsemplice = byId('searchformbutton');
    var testo = byId('searchformessage');
        if (damostrare) {
            if (damostrare.style.display == "none") {
                damostrare.style.display = "block";
                submitsemplice.style.display = "none";
                testo.innerHTML = "&ndash;";
                var statusAdv=true;
    
            } else {
                damostrare.style.display = "none";
                submitsemplice.style.display = "inline";
                testo.innerHTML = "+";
                var statusAdv=false;

            }
        }
}

*/
function searchadvancedInit(mostra) {
    var damostrare = byId(mostra);
    var submitsemplice = byId('searchformbutton');
    var testo = byId('searchformessage');
        if (damostrare) {
            if (damostrare.style.display == "none") {
                damostrare.style.display = "block";
                submitsemplice.style.display = "none";
                testo.innerHTML = "&ndash;";
                statusAdv=true;
                if(searchby) {
                    byId("searchform").className = "";
                    byId("searchform-advanced-button").className = "googleadv";
                }
                 return false;
            } else {
                damostrare.style.display = "none";
                submitsemplice.style.display = "inline";
                testo.innerHTML = "+";
                statusAdv=false;
                if(searchby) {
                    byId("searchform-advanced-button").className = "";
                    byId("searchform").className = "googlebase";
                }
                 return false;
                 
            }
            
        }
}




function setStartYear(divName1){
    var f=document.getElementById("searchFormAction");
    var fromDiv= byId(divName1);
    
    var target = f.view.options[f.view.selectedIndex].value;
    var start= '0';
    if (target== 'archivio'){
        start='1984';
    }else if(target== 'quotidiani'){
        start= '1997';
         
    }else if (target== 'foto'){
        start= '1999';
    }else if(target== 'video'){
        start='2005';
    }else if (target='repubblica'){
        start= '1999';
    }else{
        start='1984';
    }
    
    var time=new Date();
    var endYear = time.getFullYear();
    
    var options="";
    for(i =start; i<=endYear; i++){
        options=options+"<option value=\""+i+"\" >"+i+"</option>";
    }
    
    
    
    fromDiv.innerHTML = "<select class=\"sel sel-yy\" name=\"from_yyyy\">"+options+"</select>";
    
    
}



function onChangeFastBrand() {
    var f = document.getElementById("searchFormAction");
    var tmp  = f.view;
    var target = tmp.options[tmp.selectedIndex].value;
    //alert('target: '+target);
    var brand="";
    if (target.indexOf("locali.")!= -1){
        brand= target.substring(target.indexOf(".")+1,target.length);
        /*alert('brand pre: '+brand);*/
        if (brand =='il+Tirreno'){
            brand='iltirreno';
        }else if(brand=='la+Nuova+Sardegna'){
            brand='lanuovasardegna';
        }else if(brand=='Messaggero+Veneto'){
            brand='messaggeroveneto';
        }else if(brand=='il+Piccolo'){
            brand='ilpiccolo';
        }else if(brand =='il+Centro'){
            brand='ilcentro';
        }else if(brand=='la+Gazzetta+di+Mantova'){
            brand='gazzettadimantova';
        }else if(brand=='la+Gazzetta+di+Modena'){
            brand='gazzettadimodena';
        }else if(brand=='la+Nuova+di+Venezia'){
            brand='nuovavenezia';
        }else if(brand=='la+Provincia+Pavese'){
            brand='laprovinciapavese';
        }else if(brand=='il+mattino+di+Padova'){
            brand='mattinopadova';
        }else if(brand=='la+tribuna+di+Treviso'){
            brand='tribunatreviso';
        }else if(brand=='la+Nuova+Ferrara'){
            brand='lanuovaferrara';
        }else if(brand=='la+Gazzetta+di+Reggio'){
            brand='gazzettadireggio';
        }else if(brand=='Trentino'){
            brand='trentinocorrierealpi';
        }else if(brand=='Alto+Adige'){
            brand='altoadige';
        }else if(brand=='il+Corriere+delle+Alpi'){
            brand='corrierealpi';
        }else if(brand=='la+Citt�+di+Salerno'){
            brand='lacittadisalerno';
        }else if(brand=='la+Sentinella'){
            brand='lasentinella';
        }else {
            brand='repubblica';
        }
        f.action=brand;
        
    }else{
    brand='/repubblica';
    f.action=brand;
    }
    

    
    function changeLabel(classe,classeadv) {
        //byId("debug").innerHTML = classe + "  -  " +statusAdv;
            if(statusAdv == false) {
                byId("searchform").className = classe;
            } else {
                byId("searchform-advanced-button").className = classeadv;
            }
    }
    
    function changeSearch(parametro){
        window.location= parametro;
    }


    switch(target) { 
        case "web":
        changeLabel("googlebase","googleadv");
        searchby = true;
        return false;
        return searchby; 

        
        break;
        case "paginebianche":
            window.location= "http://paginebianche.repubblica.it?ref=repsearch";
        break;  
        case "paginegialle":
            window.location= "http://paginegialle.repubblica.it?ref=repsearch";
            break;  
        default:
        changeLabel("","");
        searchby = false;
        return false;
        return searchby;
        
    }

}

