/**** 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 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 show(id, status) {
    document.getElementById(id).style.display= status ? '' : 'none';
    document.getElementById(id).style.visibility= status ? 'visible' : 'hidden';
}

function setFocus(id) {
    document.getElementById(id).focus();
}

/* ------------------------------------------------------------------------- */
function miError(quien){
    
    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();
    }
    
    /*var opt=0;
    var enc=false;
    
    for (var i=0;i<document.paymentForm.paymentType.length;i++) {
        if (document.paymentForm.paymentType[i].checked) {
            opt=i;
            enc=true;
        }
    }
    
    if (enc) {
        switch (opt) {
            case 0:
            case 1:
                document.paymentForm.submit();
                break;
            case 2:
                document.paypalForm.submit();
                break;
                // Sólo en desarrollo !!! (quitar luego)
            case 3:
                document.paymentForm.submit();
                break;
            case 4:*/
                //document.paymentForm.submit();
                /*break;
        }
    }*/
}

/* ------------------------------------------------------------------------- */
function checkPaypal(payment) {
    if (payment=="PP") {
        document.paymentForm.paymentType[1].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 compruebaSoloDigitos(cadena) {
    for(var i=0; i<cadena.length; i++){
        if(cadena.charCodeAt(i) < 48 || cadena.charCodeAt(i) > 57) return false;
    }
    return true;
}
         
function initPaymentInfo(fp_sel) {
    
    var valor = 0;
    var fp = new Array('CC','CB','MO','PP','CR','BT');
    if(fp_sel!=null && fp_sel!='') {
        for(var p=0;p<fp.length;p++) {
            if(fp[p]==fp_sel) valor = p;
        }
    }
    showPaymentInfo(valor);
 }
 
function showPaymentInfo(valor){   
    
    var fp = new Array('CC','CB','MO','PP','CR','BT');
   
    var fp_divs = document.getElementById("main_cart_sin").getElementsByTagName('div');
    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) {
                // 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);
                }
            }
        }
    }
    
}
function showDiv(capa,valor){
    var divToShow = document.getElementById(capa);
    if(valor){
        divToShow.style.display='block';
    }else{
        divToShow.style.display='none';
    }
    
}


