﻿/*
 * Miscellaneous functions.
 */
// esta función es la que lee la cookie

function getCookieVal (offset)
{
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name)
{
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen)
    {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
    return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0)
    break;
    }
    return null;
}

// y esta es la que la escribe

function SetCookie (name, value)
{
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (2 < argc) ? argv[2] : null;
    var path = (3 < argc) ? argv[3] : null;
    var domain = (4 < argc) ? argv[4] : null;
    var secure = (5 < argc) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}
function DeleteCookie( name, path, domain ) 
{
    if ( GetCookie( name ) ) document.cookie = name + '=' +
    ( ( path ) ? ';path=' + path : '') +
    ( ( domain ) ? ';domain=' + domain : '' ) +
    ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}
function setFieldsFail()
{
    var wBehavior = wf.behaviors['validation'];		// shortcut
    if (getParameter('_13')!='')
    {
        var txtFotoLateral1 = document.getElementById('txtFotoLateral1');
        wBehavior.showError(txtFotoLateral1, 'El archivo Foto lateral supera el tamaño máximo.');
        txtFotoLateral1.focus();
    }
    if (getParameter('_14')!='')
    {
        var txtFotoLateral2 = document.getElementById('txtFotoLateral2');
        wBehavior.showError(txtFotoLateral2, 'El archivo Foto lateral 1 supera el tamaño máximo.');
        txtFotoLateral2.focus();
    }
    if (getParameter('_15')!='')
    {
        var txtFotoDelantera = document.getElementById('txtFotoDelantera');
        wBehavior.showError(txtFotoDelantera, 'El archivo Foto delantera supera el tamaño máximo.');
        txtFotoDelantera.focus();
    }
    if (getParameter('_16')!='')
    {
        var txtFotoTrasera = document.getElementById('txtFotoTrasera');
        wBehavior.showError(txtFotoTrasera, 'El archivo Foto trasera supera el tamaño máximo.');
        txtFotoTrasera.focus();
    }
    if (getParameter('_17')!='')
    {
        var txtFichaTecnica = document.getElementById('txtFichaTecnica');
        wBehavior.showError(txtFichaTecnica, 'El archivo Ficha técnica supera el tamaño máximo.');
        txtFichaTecnica.focus();
    }
    if (getParameter('_18')!='')
    {
        var txtHojaPedido = document.getElementById('txtHojaPedido');
        wBehavior.showError(txtHojaPedido, 'El archivo Hoja de pedido supera el tamaño máximo.');
        txtHojaPedido.focus();
    }
}
function getRespuesta()
{
    var respuesta = '';

    if (getParameter('_19')!=''){respuesta += '* '+getParameter('_19')+'<br />';}
    if (getParameter('_20')!=''){respuesta += '* '+getParameter('_20')+'<br />';}
    if (getParameter('_21')!=''){respuesta += '* '+getParameter('_21')+'<br />';}
    if (getParameter('_22')!=''){respuesta += '* '+getParameter('_22')+'<br />';}
    if (getParameter('_23')!=''){respuesta += '* '+getParameter('_23')+'<br />';}
    if (getParameter('_24')!=''){respuesta += '* '+getParameter('_24')+'<br />';}
    if (getParameter('_25')!=''){respuesta += '* '+getParameter('_25')+'<br />';}
    if (getParameter('_26')!=''){respuesta += '* '+getParameter('_26')+'<br />';}
    if (getParameter('_27')!=''){respuesta += '* '+getParameter('_27')+'<br />';}
    if (getParameter('_28')!=''){respuesta += '* '+getParameter('_28')+'<br />';}
    if (getParameter('_29')!=''){respuesta += '* '+getParameter('_29')+'<br />';}
    
    return respuesta;
    
}
function getParameter(parameter)
{
    var valor = ''
    // Obtiene la cadena completa de URL y la limpia
    var remplaza = /\+/gi;
    var url = window.location.href;

    url = unescape(url);
    url = url.replace(remplaza, " ");
//    url = url.toUpperCase();
    /* Obtiene la posicion donde se encuentra el signo ?,
    ahi es donde empiezan los parametros */
    var index = url.indexOf("?");
    /* Obtiene la posicion donde termina el nombre del parametro
    e inicia el signo = */
    index = url.indexOf(parameter,index) + parameter.length;
    /* Verifica que efectivamente el valor en la posicion actual
    es el signo = */
    if (url.charAt(index) == "=")
    {
        // Obtiene el valor del parametro
        var result = url.indexOf("&",index);
        if (result == -1){result=url.length;};
        // Despliega el valor del parametro
        valor = url.substring(index + 1,result);
    }
    return valor;
}
function ByID(elementID) 
{
  if (typeof(elementID) == 'string') {
    return document.getElementById(elementID);
  } else {
    return elementID;
  }
}
function visible(eltID, isVisible) 
{
  elt = ByID(eltID);

  if (elt) {
    if (isVisible) {
      elt.style.display = '';
    } else {
      elt.style.display = 'none';
    }
  }
}
/*
 * Contact functionality.
 */
function myCustomValidation (evt) 
{
    if (wf.formValidation(evt))
    {
        visible('sendstatus', false);
        sentTimer = setTimeout("showContactTimer();",100);
        varNombre = ByID('txtNombre');
        varEmail = ByID('txtEmail');
        varTelefono = ByID('txtTelefono');
        varFotoLateral1 = ByID('txtFotoLateral1');
        varFotoLateral2 = ByID('txtFotoLateral2');
        varFotoDelantera = ByID('txtFotoDelantera');
        varFotoTrasera = ByID('txtFotoTrasera');
        varFichaTecnica = ByID('txtFichaTecnica');
        varHojaPedido = ByID('txtHojaPedido');
        varComentario = ByID('txtComentario');

//  Almacenar valores en cookie
        SetCookie('txtNombre',varNombre.value);
        SetCookie('txtEmail',varEmail.value);
        SetCookie('txtTelefono',varTelefono.value);
        SetCookie('txtComentario',varComentario.value);
    }
    else
    {
    	var status = document.getElementById('sendstatus');
    	status.style.display = "block";
    	status.style.border = "#ff0000 2px solid";
        status.innerHTML = '<strong style="color: #ff0000;">El Email no se pudo enviar. Revise los datos introducidos y vuelva a intentarlo.</strong><br /><p style="color: #ff0000; font-weight: normal; padding: 5px;">'+getRespuesta()+'</p>';
    }
}
function showContactTimer () 
{
    jQuery.blockUI({ css: { backgroundColor: '#fff', color: '#f00', padding: 20 }, message: $('#loadBar') }); 
}

function formLoad () 
{
	var status = document.getElementById('sendstatus');
    
    if (getParameter('result') == 'SUCCESS') 
    {
    	status.style.display = "block";
    	status.style.border = "#008000 2px solid";
        status.innerHTML = '<strong style="color: #008000;">El Email ha sido enviado con éxito.</strong>';
    }
    else if (getParameter('result') == 'FAILED') 
    {
    	status.style.display = "block";
    	status.style.border = "#ff0000 2px solid";
        status.innerHTML = '<strong style="color: #ff0000;">El Email no se pudo enviar. Revise los datos introducidos y vuelva a intentarlo.</strong><br /><p style="color: #ff0000; font-weight: normal; padding: 5px;">'+getRespuesta()+'</p>';
        setFieldsFail();
//  Leer cookie y asignar valores de los campos
        varNombre = ByID('txtNombre');
        varEmail = ByID('txtEmail');
        varTelefono = ByID('txtTelefono');
        varComentario = ByID('txtComentario');

        varNombre.value = GetCookie('txtNombre');
        varEmail.value = GetCookie('txtEmail');
        varTelefono.value = GetCookie('txtTelefono');
        varComentario.value = GetCookie('txtComentario');
    }
    else
    {    
    	status.style.display = "none";
    }

//  Eliminar cookies
    DeleteCookie('txtComentario');
    DeleteCookie('txtNombre');
    DeleteCookie('txtEmail');
    DeleteCookie('txtTelefono');
}

wf.showAlertOnError = false;
wf.functionName_formValidation = "myCustomValidation";

addEvent(window, 'load', formLoad, false);
addEvent(window, 'unload', EventCache.flush, false);
