/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


// xGetElementById r2, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function validateEmail(email_address){
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(email_address.match(emailRegEx)){
		return false;		
	}else{
		return true;
	}
}

function xGetElementById(e)
{
    if(typeof(e)=='string') {
        if(document.getElementById) e=document.getElementById(e);
        else if(document.all) e=document.all[e];
        else e=null;
    }
    return e;
}


function onLoad()
{
    document.contact.department.focus();
}


function YourCommentValidation(comments)
{
    var validation = new Array();
    if(document.forms[0].your_name.value == "") {
        document.getElementById("your_name_validation").style["display"] = "block";
        validation.push(0);
    }
    if(document.forms[0].your_comments.value == "") {
        document.getElementById("your_comments_validation").style["display"] = "block";
        validation.push(1);
    }

    if(setFocus(validation))
        return true;
    else
        return false;

}

function ContactValidation(contact)
{
    var validation = new Array();
    if(document.forms[0].your_name.value == "") {
        document.getElementById("your_name_validation").style["display"] = "block";
        validation.push(0);
    }
    if((document.forms[0].email.value == "") || (validateEmail(document.forms[0].email.value) == true)) {		
        document.getElementById("email_validation").style["display"] = "block";
        validation.push(1);
    }
	if(document.forms[0].telephone.value == "") {
        document.getElementById("telephone_validation").style["display"] = "block";
        validation.push(2);
    }
	if(document.forms[0].rooms.value == "") {
        document.getElementById("rooms_validation").style["display"] = "block";
        validation.push(3);
    }
	if(document.forms[0].adults.value == "") {
        document.getElementById("adults_validation").style["display"] = "block";
        validation.push(4);
    }
	if(document.forms[0].children.value == "") {
        document.getElementById("children_validation").style["display"] = "block";
        validation.push(5);
    }
    if(document.forms[0].enquiry.value == "") {
        document.getElementById("enquiry_validation").style["display"] = "block";
        validation.push(6);
    }

    if(setFocus(validation))
        return true;
    else
        return false;

}

function BookingValidation(contact)
{
    var validation = new Array();
    if(document.forms[0].your_name.value == 0) {
        document.getElementById("your_name_validation").style["display"] = "block";
        validation.push(0);
    }
    if((document.forms[0].email.value == "") || (validateEmail(document.forms[0].email.value) == true)) {
        document.getElementById("email_validation").style["display"] = "block";
        validation.push(1);
    }
    if(document.forms[0].telephone_no.value == "") {
        document.getElementById("telephone_no_validation").style["display"] = "block";
        validation.push(2);
    }
	if(document.forms[0].adults.value == 0) {
        document.getElementById("adults_validation").style["display"] = "block";
        validation.push(3);
    }
	if(document.forms[0].children.value == 0) {
        document.getElementById("children_validation").style["display"] = "block";
        validation.push(4);
    }
    if(document.forms[0].room_type.value == 0) {
        document.getElementById("room_type_validation").style["display"] = "block";
        validation.push(5);
    }
    if(document.forms[0].breakfast.value == 0) {
        document.getElementById("breakfast_validation").style["display"] = "block";
        validation.push(6);
    }
    if(document.forms[0].dietary.value == "") {
        document.getElementById("dietary_validation").style["display"] = "block";
        validation.push(7);
    }
    if(setFocus(validation))
        return true;
    else
        return false;

}

function QuoteValidation(quote)
{
    var validation = new Array();
    if(document.forms[0].department.value == 0) {
        document.getElementById("department_validation").style["display"] = "block";
        validation.push(0);
    }
    if(document.forms[0].your_name.value == "") {
        document.getElementById("your_name_validation").style["display"] = "block";
        validation.push(1);
    }

    if(document.forms[0].email.value == "") {
        document.getElementById("email_validation").style["display"] = "block";
        validation.push(2);
    }

    if(document.forms[0].tel.value == "") {
        document.getElementById("tel_validation").style["display"] = "block";
        validation.push(3);
    }

    if(document.forms[0].call_back_time.value == "") {
        document.getElementById("call_back_time_validation").style["display"] = "block";
        validation.push(4);
    }

    if(document.forms[0].quanity_of_pages.value == "") {
        document.getElementById("quanity_of_pages_validation").style["display"] = "block";
        validation.push(5);
    }
    if(document.forms[0].project_details.value == "") {
        document.getElementById("project_details_validation").style["display"] = "block";
        validation.push(6);
    }


    if(setFocus(validation))
        return true;
    else
        return false;

}


//function to check valid email address
function isValidEmail(strEmail){
    validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;

    // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1)
    {
        alert('A valid e-mail address is required.\nPlease amend and retry');
        return false;
    }
    return true;
}



//ESTA FUNCION OCULTA LOS MENSAJES QUE HAN SIDO ENVIADOS POR EL SERVIDOR
function displayMessage()
{
    document.getElementById("server1").style["display"] = "none";
    document.getElementById("server2").style["display"] = "none";
}


//ESTA FUNCION NOS VALE PARA ASIGNAR EL FOCO AL FORMULARIO
//DEPENDIENDO DEL VALOR DEL ARRAY QUE LE VIENE DE LA FUNCION
//QUE LA LLAMA, PONE EL FOCO EN EL PRIMER ELEMENTO DEL ARRAY

function setFocus(validation)
{

    var position = -1;

    if( validation.length > 0)
    {

        for (i=0;i<validation.length;i++)
        {
            document.getElementById("id_" + validation[i]).focus();
            return false;
            position++;
        }
        return false;
    }
    else
        return true;
}


function hideMessage(id_element)
{
    var numItems = document.forms[0].elements.length;

    for(i=0; i<numItems; i++)
    {
        //Esto nos vale para comprobar el tipo de un elemento en el formulario
        //los input son de tipo text y habria que comprobar los demas elementos
        //sacando un mensaje de alert!!!LA LINEA ESTA AKI DEBAJO
        //document.forms[i].elements[i].type;
        document.getElementById(id_element).style["display"] = "none";

    /*
     *PARA RECORRER LOS ELEMENTOS HARIAMOS LO SIGUIENTE
    if(document.forms[i].elements[i].value != "" )
     {
         if(document.getElementById(id_element).style["display"] == "block")
            document.getElementById(id_element).style["display"] = "none";
     } */
    }
}
