/*
 * 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 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';
    }
  }
}
function myCustomValidation (evt) 
{
    if (wf.formValidation(evt))
    {
        visible('sendstatus', false);
        ContactName = ByID('ContactName');
        ContactEmail = ByID('ContactEmail');
        ContactSubject = ByID('ContactSubject');
        ContactMessage = ByID('ContactMessage');

//  Almacenar valores en cookie
        SetCookie('ContactName',ContactName.value);
        SetCookie('ContactEmail',ContactEmail.value);
        SetCookie('ContactSubject',ContactSubject.value);
        SetCookie('ContactMessage',ContactMessage.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.</strong><br /><strong style="color: #ff0000;">Revise los datos introducidos y vuelva a intentarlo.</strong><br />';
    }
}
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 exitosamente.</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.</strong><br /><strong style="color: #ff0000;">Revise los datos introducidos y vuelva a intentarlo.</strong><br />';
//  Leer cookie y asignar valores de los campos
        ContactName = ByID('ContactName');
        ContactEmail = ByID('ContactEmail');
        ContactSubject = ByID('ContactSubject');
        ContactMessage = ByID('ContactMessage');

        ContactName.value = GetCookie('ContactName');
        ContactEmail.value = GetCookie('ContactEmail');
        ContactSubject.value = GetCookie('ContactSubject');
        ContactMessage.value = GetCookie('ContactMessage');
    }
    else
    {    
    	status.style.display = "none";
    }

//  Eliminar cookies
    DeleteCookie('ContactName');
    DeleteCookie('ContactEmail');
    DeleteCookie('ContactSubject');
    DeleteCookie('ContactMessage');
}

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

