/**** FUNCIONES JAVASCRIPT PARA TIENDA VIRTUAL ****/

/* ------------------------------------------------------------------------- */
function muestraComp(){
    document.getElementById('comp').style.display = '';
}

/* ------------------------------------------------------------------------- */
function ocultaComp(){
    document.getElementById('comp').style.display = 'none';
    document.getElementById('inputComp').value = '';
}

/* ------------------------------------------------------------------------- */
function compruebaTipoCliente() {
    if (!document.purchaseForm.clientType[1].checked) {
        ocultaComp();
    }
}

/* ------------------------------------------------------------------------- */
function show(id, state) {
    document.getElementById(id).style.display= state ? '' : 'none';
    document.getElementById(id).style.visibility= state ? 'visible' : 'hidden';
}

function setFocus(id) {
    
    document.getElementById(id).focus();
}

/* ------------------------------------------------------------------------- */
function miError(quien){
    
    if(quien == 'invoicingData.address2' || quien == 'shippingData.address2'){
        document.getElementById(quien).style.background="#FBA09B";
    } else {
    document.getElementById(quien).style.background="#FBA09B";
}
setFocus(quien);
}

/* ------------------------------------------------------------------------- */

function countryError(quien, msg) {
    if(msg!='')
        alert(msg);  
    document.getElementById(quien).style.background="#FBA09B";
    setFocus(quien);
}

function phoneError(quien, msg) {
    if(msg!='')
        alert(msg);
    document.getElementById(quien).className = "in_error";
    setFocus(quien);
}

function stateError(code, quien, idCountry, msg) {
    
    
    if (idCountry=="US" || idCountry=="ES" || idCountry=="CA") {
        document.purchaseForm.state.style.background="#FBA09B";
        //document.purchaseForm.state_combo.className = "in_error_select";
        document.purchaseForm.state_combo.focus();
    }
    else {
        document.purchaseForm.state.style.background="#FBA09B";
        //document.purchaseForm.state.className="in_error";
        document.purchaseForm.state.focus();
    }
    
    if(code == 'COD_NOT_ALLOWED' || code=='ONLY_SPAIN')
        alert(msg);
}

/* ------------------------------------------------------------------------- */
function doProperSubmit(){
    
    if(document.getElementById("check_PP") && document.getElementById("check_PP").checked) {
        document.paypalForm.submit();
    } else {
    document.paymentForm.submit();
}

}

/* ------------------------------------------------------------------------- */
function checkPaypal(payment) {
    if (payment=="PP") {
        document.getElementById("check_PP").checked;
        document.paymentForm.submit();
    }
}

/* ------------------------------------------------------------------------- */
function checkPaypalCorrectCurrency(alertText, paypalRedirection) {
    alert(alertText);
    window.location=paypalRedirection;
}


/* ------------------------------------------------------------------------- */
function setShippingState(invoicingState, shippingState) {
    if(shippingState==null || shippingState=='') {
        document.purchaseForm.state.value=invoicingState;
    } else {
    document.purchaseForm.state.value=shippingState;
}
}

function showStates(idCountry,ent) {
    
    if (idCountry=="US" || idCountry=="ES" || idCountry=="CA") {
        insertStates(idCountry);
        show('state',false);
        show('state_combo',true);
        copyState(document.purchaseForm.state_combo[document.purchaseForm.state_combo.selectedIndex].text);
    } else {
    show('state',true);
    show('state_combo',false);
    if(ent==1) {
        copyState("");
    }
    
}

}

/* ------------------------------------------------------------------------- */
function insertStates(idCountry) {
    
    var sel = false;
    
    for(var i=0; i < document.purchaseForm.state_combo.length; i++) document.purchaseForm.state_combo.options[i]=null;
    
    if (idCountry=="US") {
        for(var i=0; i < EEUU_states.length; i++){
            if(EEUU_states[i]!=null) {
                var newOption = new Option(EEUU_states[i]);
                document.purchaseForm.state_combo.options[i] = newOption;
                if(document.purchaseForm.state.value!='' && document.purchaseForm.state.value==EEUU_states[i]) {
                    document.purchaseForm.state_combo.selectedIndex=i;
                    sel = true;
                }
            }
        }
    }
    else if (idCountry=="CA") {
        for(var i=0 ; i < Canada_states.length; i++){
            if(Canada_states[i]!=null) {
                var newOption = new Option(Canada_states[i]);
                document.purchaseForm.state_combo.options[i] = newOption;
                if(document.purchaseForm.state.value!='' && document.purchaseForm.state.value==Canada_states[i]) {
                    document.purchaseForm.state_combo.selectedIndex=i;
                    sel = true;
                }
            }
        }
    }
    else if (idCountry=="ES") {
        for(var i=0; i < Spain_states.length; i++){
            if(Spain_states[i]!=null) {
                var newOption = new Option(Spain_states[i]);
                document.purchaseForm.state_combo.options[i] = newOption;
                if(document.purchaseForm.state.value!='' && document.purchaseForm.state.value==Spain_states[i]) {
                    document.purchaseForm.state_combo.selectedIndex=i;
                    sel = true;
                }
            }
        }
    }
    
    if(sel==false) document.purchaseForm.state_combo.selectedIndex=0;
}

/* ------------------------------------------------------------------------- */
function copyState (stateName) {
    document.purchaseForm.state.value=stateName;
}

/* ------------------------------------------------------------------------- */
function compruebaThai(country) {
    if (country=='TH') {
        alert('\u0E40\u0E23\u0E35\u0E22\u0E19\u0E17\u0E48\u0E32\u0E19\u0E1C\u0E39\u0E49\u0E21\u0E35\u0E2D\u0E38\u0E1B\u0E01\u0E32\u0E23\u0E04\u0E38\u0E13\n\u0E17\u0E32\u0E07\u0E1A\u0E23\u0E34\u0E29\u0E31\u0E17\u0E02\u0E2D\u0E41\u0E08\u0E49\u0E07\u0E43\u0E2B\u0E49\u0E17\u0E23\u0E32\u0E1A\u0E27\u0E48\u0E32\u0E02\u0E13\u0E30\u0E19\u0E35\u0E49\u0E40\u0E23\u0E32\u0E44\u0E21\u0E48\u0E2A\u0E32\u0E21\u0E32\u0E23\u0E16\u0E17\u0E33\u0E01\u0E32\u0E23\u0E15\u0E2D\u0E1A\u0E04\u0E33\u0E16\u0E32\u0E21\u0E2B\u0E23\u0E37\u0E2D\u0E15\u0E34\u0E14\u0E15\u0E48\u0E2D\u0E01\u0E31\u0E1A\u0E25\u0E39\u0E01\u0E04\u0E49\u0E32\u0E40\u0E1B\u0E47\u0E19\u0E20\u0E32\u0E29\u0E32\u0E44\u0E17\u0E22\u0E44\u0E14\u0E49\u0E40\u0E19\u0E37\u0E48\u0E2D\u0E07\u0E08\u0E32\u0E01\u0E44\u0E21\u0E48\u0E21\u0E35\u0E40\u0E08\u0E49\u0E32\u0E2B\u0E19\u0E49\u0E32\u0E23\u0E31\u0E1A\u0E1C\u0E34\u0E14\u0E0A\u0E2D\u0E1A\u0E14\u0E49\u0E32\u0E19\u0E20\u0E32\u0E29\u0E32\u0E44\u0E17\u0E22\n\u0E14\u0E31\u0E07\u0E19\u0E31\u0E49\u0E19\u0E40\u0E23\u0E32\u0E08\u0E36\u0E07\u0E02\u0E2D\u0E23\u0E1A\u0E01\u0E27\u0E19\u0E43\u0E2B\u0E49\u0E17\u0E48\u0E32\u0E19\u0E1C\u0E39\u0E49\u0E21\u0E35\u0E2D\u0E38\u0E1B\u0E01\u0E32\u0E23\u0E04\u0E38\u0E13\u0E17\u0E38\u0E01\u0E17\u0E48\u0E32\u0E19\u0E2A\u0E48\u0E07\u0E2D\u0E35\u0E40\u0E21\u0E25\u0E4C\u0E2B\u0E23\u0E37\u0E2D\u0E15\u0E34\u0E14\u0E15\u0E48\u0E2D\u0E01\u0E31\u0E1A\u0E40\u0E23\u0E32\u0E40\u0E1B\u0E47\u0E19\u0E20\u0E32\u0E29\u0E32\u0E2D\u0E31\u0E07\u0E01\u0E24\u0E29\u0E19\u0E31\u0E1A\u0E41\u0E15\u0E48\u0E19\u0E35\u0E49\u0E40\u0E1B\u0E47\u0E19\u0E15\u0E49\u0E19\u0E44\u0E1B\u0E08\u0E19\u0E01\u0E27\u0E48\u0E32\u0E17\u0E32\u0E07\u0E1A\u0E23\u0E34\u0E29\u0E31\u0E17\u0E08\u0E30\u0E2B\u0E32\u0E1C\u0E39\u0E49\u0E23\u0E31\u0E1A\u0E1C\u0E34\u0E14\u0E0A\u0E2D\u0E1A\u0E04\u0E19\u0E43\u0E2B\u0E21\u0E48\u0E44\u0E14\u0E49\n\n\u0E02\u0E2D\u0E2D\u0E20\u0E31\u0E22\u0E43\u0E19\u0E04\u0E27\u0E32\u0E21\u0E44\u0E21\u0E48\u0E2A\u0E30\u0E14\u0E27\u0E01\u0E21\u0E32 \u0E13 \u0E42\u0E2D\u0E01\u0E32\u0E2A\u0E19\u0E35\u0E49\u0E14\u0E49\u0E27\u0E22\n\n\u0E02\u0E2D\u0E41\u0E2A\u0E14\u0E07\u0E04\u0E27\u0E32\u0E21\u0E19\u0E31\u0E1A\u0E16\u0E37\u0E2D');
    }
}
/* ------------------------------------------------------------------------- */

function compruebaSoloDigitos (cadena) {
    for(var i=0; i<cadena.length; i++){
        if(cadena.charCodeAt(i) < 48 || cadena.charCodeAt(i) > 57) return false;
    }
    return true;
}

/* ------------------------------------------------------------------------- */

function checkCountry (msg) {
    /*  var idCountry = document.getElementById("invoicingData.country").value;
    
    if(idCountry=="KW" || idCountry=="AE")
        alert(msg);
    else
        document.purchaseForm.submit();
    */
    document.purchaseForm.submit();
    
}

/* ------------------------------------------------------------------------- */



function initPaymentInfo(fp_sel) {
    
    var valor = 0;
    
    var fp = new Array('CC','CB','CR','MO','PP','BT','CA','PH');
    
    if(fp_sel!=null && fp_sel!='') {
        for(var p=0;p<fp.length;p++) {
            if(fp[p]==fp_sel) valor = p;
        }
    }
    
    var phone_countries = new Array("US","GB","JP","DE","NL","FR","IT","ES","MX","PT","CA");
    var phone_free      = new Array(1,1,1,1,1,1,1,0,1,1,1);
    var phone_numbers   = new Array("1 800 691 3143","0800 917 5201","00531 34 0002","0800 182 6905","0800 221 974","0 800 918 460","800 783 936","902 305 405","01 800 123 0146","800 834 127","01 866 749 4519");
    
    var found = false;
    
    var phone_number = document.getElementById("phone_number");
    var phone_header = document.getElementById("phone_header");

    for(var p=0;p<phone_countries.length && !found;p++) {
        
        if(document.getElementById("country_phone").value==phone_countries[p]) {
            phone_number.value = phone_numbers[p];
            phone_header.innerHTML = phone_headers[phone_free[p]];
            found = true;
        }
    }
    
    showPaymentInfo(valor);
}

function showPaymentInfo(valor) {   
    
    var fp = new Array('CC','CB','CR','MO','PP','BT','CA','PH','PC');
    
    var fp_divs = document.getElementById("main_cart_sin").getElementsByTagName('div');
    var selected;
    
    for(var f=0;f<fp_divs.length;f++) {
        var current = fp_divs[f];
        
        if(current.id!=null) {
            // si es la seleccionada
            if(current.id.indexOf(fp[valor])!=-1) {
                selected = current;
                // class para la cabecera
                if(current.id.indexOf('head')!=-1) {
                    current.className = 'barradio_active';
                }
                // visibilidad para el contenido
                if(current.id.indexOf('info')!=-1) {
                    showDiv(current.id, true);
                }
            }
            // todas las demas
            else {
                // class para la cabecera
                if(current.id.indexOf('head')!=-1) {
                    current.className = 'barradio';
                }
                // visibilidad para el contenido
                if(current.id.indexOf('info')!=-1) {
                    showDiv(current.id, false);
                }
            }
        }

    }

    if(selected && selected.id!=null && selected.id.indexOf('PH')!=-1) {
        
        var phone_countries = new Array("US","GB","JP","DE","NL","FR","IT","ES","MX","PT","CA");
        var phone_free      = new Array(1,1,1,1,1,1,1,0,1,1,1);
        var phone_numbers   = new Array("1 800 691 3143","0800 917 5201","00531 34 0002","0800 182 6905","0800 221 974","0 800 918 460","800 783 936","902 305 405","01 800 123 0146","800 834 127","01 866 749 4519");

        var found = false;

        var phone_number  = document.getElementById("phone_number");
        var phone_number2 = document.getElementById("phone_number2");
        var phone_text    = document.getElementById("phone_text");

        for(var p=0;p<phone_countries.length && !found;p++) {
            if(document.getElementById("country_phone").value==phone_countries[p]) {
                phone_number.value = phone_numbers[p];
                phone_number2.value = phone_numbers[p];
                phone_text.innerHTML = phone_texts[phone_free[p]];
                found = true;
            }
        }

        if(document.getElementById("hour").value>=9 && document.getElementById("hour").value<=20) {
            document.getElementById("online").value = "ON LINE";
        } else {
            document.getElementById("online").value = "OFF LINE";
        }

        document.getElementById("next").style.display='none';
    } else {
        document.getElementById("next").style.display='block';
    }
}



function showDiv(capa,valor){
    var divToShow = document.getElementById(capa);
    if(valor){
        divToShow.style.display='block';
    }else{
    divToShow.style.display='none';
}

}

/**
* Carga una sustituye una imagen por otra pasada por parametro
*/
function new_first_img(image, newimage, newImageBig, lightbox) {
    var acambiar = document.getElementById(image);
    var alightbox = document.getElementById(lightbox); 
    acambiar.src = newimage;
    alightbox.href = newImageBig; 
}

/**
* Para los overview
*/
function muestraCapa(capa){
    
    var tabs = new Array('results','use','guarantee','quality','support','shipment','spares');
    
    for(var t=0;t<tabs.length;t++) {
        if(capa==t) {
            document.getElementById('li'+tabs[t]).className = 'liactive';
            document.getElementById(tabs[t]+'info').style.display='block';
        } else {
        document.getElementById('li'+tabs[t]).className = '';
        document.getElementById(tabs[t]+'info').style.display='none';
    }
}

}

/**
* Funcion generica que muestra/oculta una capa en funcion de su estado anterior
*/
function visualDiv(capa) {
    if(document.getElementById(capa).style.display=='block')
        showDiv(capa, false);
    else
        showDiv(capa, true);
}

function ir(voy){
    if (valida()) document.location = voy;
}

function submitValidado(){
    document.productos.submit();
}
