var bGiaCliccatoGBL = false;
//online
var servletPath = "http://www.gatorade.it/newgatorade/";
//test
//var servletPath = "http://test.gatorade.it/newgtrd_stage/";

// lunghezza textarea
function Count(text,long,id_err) 
{
    var maxlength = new Number(long); // Change number to your max length.
    if (text.value.length > maxlength){
        text.value = text.value.substring(0,maxlength);
        document.getElementById(id_err).innerHTML = "Inserire al massimo "+ maxlength +" caratteri";
		text.style.background = "#BDE400";							    
		text.focus();  		
    }
}

// TRIM - start
// Removes leading whitespaces
function LTrim( value ) {	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");	
}

// Removes ending whitespaces
function RTrim( value ) {	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");	
}

// Removes leading and ending whitespaces
function trim( value ) {	
	return LTrim(RTrim(value));	
}
// TRIM - end




// funzione per prendere un elemento con id univoco
function prendiElementoDaId(id_elemento) {
	var elemento;
	if(document.getElementById)
		elemento = document.getElementById(id_elemento);
	else
		elemento = document.all[id_elemento];
	return elemento;
}

// funzione per assegnare un oggetto XMLHttpRequest
function assegnaXMLHttpRequest() {
	var
		XHR = null,
		browserUtente = navigator.userAgent.toUpperCase();
	if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
		XHR = new XMLHttpRequest();
	else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) {
		if(browserUtente.indexOf("MSIE 5") < 0)
			XHR = new ActiveXObject("Msxml2.XMLHTTP");
		else
			XHR = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return XHR;
}

function setOptionValueSelected(selectName, valueToSelect) {
	if (selectName) { // se esiste
		if (selectName.options) { // è una tendina
			for (var i=0;i<selectName.options.length;i++) {
				if (selectName.options[i].value == valueToSelect) {
					selectName.options[i].selected=true;
				}
			}
		} else { // è un input type
			selectName.value = valueToSelect;
		}
	}
}


// ------------------------------------------- funzioni date -------------------------------------------------------------------------
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isIntegerPrivate(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

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++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){

	dtStrVal = dtStr.value;
	
	var daysInMonth = DaysArray(12);
	var pos1=dtStrVal.indexOf(dtCh);
	var pos2=dtStrVal.indexOf(dtCh,pos1+1);
	var strDay=dtStrVal.substring(0,pos1);
	var strMonth=dtStrVal.substring(pos1+1,pos2);
	var strYear=dtStrVal.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);

	if (pos1==-1 || pos2==-1){
		alert("Inserire una data");
		dtStr.focus();
		return true;
	}

	if (strMonth.length<1 || month<1 || month>12){
		alert("Mese non valido");
		dtStr.focus();
		return true;
	}

	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Giorno non valido");
		dtStr.focus();
		return true;
	}

	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("L'anno deve essere compreso fra: "+minYear+" e "+maxYear);
		dtStr.focus();
		return true;
	}

	if (dtStrVal.indexOf(dtCh,pos2+1)!=-1 || isIntegerPrivate(stripCharsInBag(dtStrVal, dtCh))==false){
		alert("Data non valida");
		dtStr.focus();
		return true;
	}	

	return false;
}

function isDate3Field(dtStr1,dtStr2,dtStr3){

	var daysInMonth = DaysArray(12);
	
	var strDay=dtStr1.value;
	var strMonth=dtStr2.value;
	var strYear=dtStr3.value;	
	strYr=strYear;
	
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);

	if (strDay=="" || strMonth=="" || strYear == ""){
		return true;
	}

	if (strMonth.length<1 || month<1 || month>12){
		return true;
	}

	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return true;
	}

	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return true;
	}

	return false;
}

// ------------------------------------------- funzioni date FINE -------------------------------------------------------------------------
