/** 
 * Check for invalid (email) characters are present.
 */
function validateString(string, returnInvalidChars)
{
	validChars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    invalidChars = '';
         
    if(string == null || string == '')
    	return(true);
         
    //For every character on the string.   
    for(index = 0; index < string.length; index++)
    {
    	char = string.substr(index, 1);                        
            
        //Is it a valid character?
        if(validChars.indexOf(char) == -1)
        {
        	//If not, is it already on the list of invalid characters?
            if(invalidChars.indexOf(char) == -1)
            {
            	//If it's not, add it.
            	if(invalidChars == '')
            		invalidChars += char;
                else
                	invalidChars += ', ' + char;
            }
        }
    }                     
            
    //If the string does not contain invalid characters, the function will return true.
    //If it does, it will either return false or a list of the invalid characters used
    //in the string, depending on the value of the second parameter.
    if(returnInvalidChars == true && invalidChars != '')
    {
    	lastComma = invalidChars.lastIndexOf(',');
        
        if(lastComma != -1)
        	invalidChars = invalidChars.substr(0, $lastComma) + 
            ' and ' + invalidChars.substr(lastComma + 1, invalidChars.length);
                      
    		return(invalidChars);
    }
    else
    	return(invalidChars == ''); 
}

/** 
 * Check if the input in a given form is valid.
 */

function validateForm(formName)
{
	var result = true;
	var form = document.getElementById(formName);

	//alert(form.elements.length);
	
	for (var i = 0; i < form.elements.length; i++)
	{
		// alert(form.elements[i].name);
		// check email
		if(form.elements[i].name == "email")
		{
			//alert('found it: ' + form.elements[i].value);
			if (!validateEmailAddress(form.elements[i].value))
				result = false;
		}
	}	

	return result;
}


/** 
 * Check if the email address in the newsletter registration input is valid.
 */
function validateEmailAddress(emailAddress)
{
	var result = true;

	//alert(emailAddress);
	
	//Assumes that valid email addresses consist of username@domain.tld
    at = emailAddress.indexOf('@');
    dot = emailAddress.lastIndexOf('.');
    
    if(at == -1 || dot == -1 || 
    	dot <= at + 1 || dot == 0 || 
    	dot == emailAddress.length - 1)
    	result = false;
            
    username = emailAddress.substr(0, at);
    domainName = emailAddress.substr(at + 1, emailAddress.length);                  
    
    if(validateString(username) === false || 
		validateString(domainName) === false)
        result = false;                     
	
	setErrorMessageVisibility(!result);
		
    return result;
}

/** 
 * Check if the email address in the newsletter registration input is valid.
 */
function setErrorMessageVisibility(isVisible)
{
	//alert(isVisible);
	
	var errorMsg = document.getElementById('newsletterFormErrorMessage');
	/*
	if (isVisible)
		errorMsg.style.visibility = "visible";
	else
		errorMsg.style.visibility = "hidden";
	
	
	if (isVisible)
		errorMsg.style.display = "block";
	else
		errorMsg.style.display = "none";
	*/
	
	if (isVisible)
		errorMsg.style.color = "red";
	else
		errorMsg.style.color = "white";
	
	
}

function setText(elementID)
{
	//alert('setText - start');
	
	var elem = document.getElementById(elementID);
	
	if (elem)
	{
		if (elementID == "welcomeText")
		{
			if (location.href.indexOf("dernieres-minutes.jsp") > 0)
				elem.innerHTML = "Bienvenue sur Promosejours, trouvez votre s&eacute;jour pas cher, m&ecirc;me a la derni&egrave;re minute ! Avec Promosejours.com, &eacute;vadez-vous en toute confiance !";
			else
				elem.innerHTML = "Bienvenue sur Promosejours, trouvez votre s&eacute;jour pas cher, r&eacute;servation simple <br /> 100% s&eacute;curis&eacute;e. Avec Promosejours.com, &eacute;vadez-vous en toute confiance !";
		}
	}
	
	//alert('setText - end');
}