var cookieName = "skipperarmatori";

function setCookie(cookieName, cookieValue, expires, path, domain, secure) {
	document.cookie = escape(cookieName) + '=' + escape(cookieValue)
	+ (expires ? '; EXPIRES=' + expires.toGMTString() : '')
	+ (path ? '; PATH=' + path : '')
	+ (domain ? '; DOMAIN=' + domain : '')
	+ (secure ? '; SECURE' : '');
}

function getCookie(cookieName) {
	var cookieValue = null;
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
	
	if (posName != -1) {
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);
		
		if (endPos != -1){
			cookieValue = unescape(document.cookie.substring(posValue, endPos));
		} else {
			cookieValue = unescape(document.cookie.substring(posValue));
		}
	}
	return cookieValue;
}

function invite() {
	$('#chatInvite').slideDown(250, function(){
		$('#questionCloud').animate({top: '-120px'}, 250)
	})
	setTimeout("hideChat()", 10000);
}

function hideChat() {
	$('#chatInvite').slideUp(250, function(){});
}

$(function(){
	
	if ( !getCookie(cookieName) ) {
		
		setTimeout("invite()", 5000);
		
		$('#inviteClose').bind('click', function(){
			$('#chatInvite').slideUp(250, function(){
				setCookie(cookieName, '1');
			});
		})
		setCookie(cookieName, '1');
	}
})
// ---=======[. FORM .]========---

// number
var digits = "0123456789";

// whitespace characters
var whitespace = " \t\n\r";

// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "+()-/ ";



// CONSTANT STRING DECLARATIONS
// (grouped for ease of translation and localization)

// m is an abbreviation for "missing"

var mPrefix = "Il campo ";
var mSuffix = " e' obbligatorio.";

// i is an abbreviation for "invalid"

var iPrefix = "Il campo ";
var iSuffix = " contiene un valore non valido.";

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)
{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

// isInteger (STRING s)
// 
// Returns true if all characters in string s are numbers.

function isInteger (s)
{   var i;

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


// isEmail (STRING s)
// 
// Email address must be of form a@b.c -- in other words:

// Removes all characters which appear in string bag from string s.

function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

// Notify user that required field theField is empty.

function warnEmpty (theField, s)
{	
	theField.focus()
    alert(mPrefix + s + mSuffix)
    return false
}

// Notify user that contents of field theField are invalid.

function warnInvalid (theField, s)
{
	theField.focus()
    theField.select()
    alert(iPrefix + s + iSuffix)
    return false
}

// checkPhone (TEXTFIELD theField, STRING s [, BOOLEAN emptyOK])

function checkPhone (theField, s, emptyOK)
{	// Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkPhone.arguments.length == 2) emptyOK = false;
	
    if ((!emptyOK) && (isWhitespace(theField.value))) return warnEmpty (theField, s);
    else
    {  var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters)
       if (!isInteger(normalizedPhone, false)) 
          return warnInvalid (theField, s);
       else return true;
    }
}

// ---=======[. FUNZIONE PER LA VALIDAZIONE .]========---

function validatePersonalInfo(form) {
	form.recipient.value = "segreteria@skipperarmatori.com";
	return checkPhone(form.elements["telefono"],"telefono");
}
