function isOnlyNumbers(textbox)
{	
	for (var i = 0 ; i<textbox.value.length ; i++)
	{
		var strValue = textbox.value.charAt(i);
		var retorno;
		
		switch(strValue)
		{
			case "0":
				retorno = true;
				break;
			case "1":				
				retorno = true;
				break;
			case "2":
				retorno = true;
				break;
			case "3":
				retorno = true;
				break;
			case "4":
				retorno = true;
				break;
			case "5":
				retorno = true;
				break;
			case "6":
				retorno = true;
				break;
			case "7":
				retorno = true;
				break;
			case "8":
				retorno = true;
				break;
			case "9":
				retorno = true;
				break;
			default:
				retorno = false;
				break;
		}	
		
		if (retorno == false)
			break;	
	}
	
	return retorno;
}

function isPhoneNumber(textbox)
{	
	for (var i = 0 ; i<textbox.value.length ; i++)
	{
		var strValue = textbox.value.charAt(i);
		var retorno;
		
		switch(strValue)
		{
			case "0":
				retorno = true;
				break;
			case "1":				
				retorno = true;
				break;
			case "2":
				retorno = true;
				break;
			case "3":
				retorno = true;
				break;
			case "4":
				retorno = true;
				break;
			case "5":
				retorno = true;
				break;
			case "6":
				retorno = true;
				break;
			case "7":
				retorno = true;
				break;
			case "8":
				retorno = true;
				break;
			case "9":
				retorno = true;
				break;
			case "-":
				retorno = true;
				break;
			default:
				retorno = false;
				break;
		}	
		
		if (retorno == false)
			break;	
	}
	
	return retorno;
}

function isOnlyCurrency(textbox)
{	
	for (var i = 0 ; i<textbox.value.length ; i++)
	{
		var strValue = textbox.value.charAt(i);
		var retorno;
		
		switch(strValue)
		{
			case "0":
				retorno = true;
				break;
			case "1":				
				retorno = true;
				break;
			case "2":
				retorno = true;
				break;
			case "3":
				retorno = true;
				break;
			case "4":
				retorno = true;
				break;
			case "5":
				retorno = true;
				break;
			case "6":
				retorno = true;
				break;
			case "7":
				retorno = true;
				break;
			case "8":
				retorno = true;
				break;
			case "9":
				retorno = true;
				break;
			case ".":
				retorno = true;
				break;
			default:
				retorno = false;
				break;
		}	
		
		if (retorno == false)
			break;	
	}
	
	return retorno;
}

function limitTextArea(textarea, limit)
{
	var strValue = textarea.value;
	if (strValue.length > limit)
		strValue = strValue.substring(0, limit - 1);
	textarea.value = strValue; 
}

function isOnlyText(textbox)
{
	for (var i = 0 ; i<textbox.value.length ; i++)
	{
		var strValue = textbox.value.charAt(i);
		var retorno;
		
		switch(strValue)
		{
			case "0":
				retorno = false;
				break;
			case "1":
				retorno = false;
				break;
			case "2":
				retorno = false;
				break;
			case "3":
				retorno = false;
				break;
			case "4":
				retorno = false;
				break;
			case "5":
				retorno = false;
				break;
			case "6":
				retorno = false;
				break;
			case "7":
				retorno = false;
				break;
			case "8":
				retorno = false;
				break;
			case "9":
				retorno = false;
				break;
			default:
				retorno = true;
				break;
		}	
		
		if (retorno == false)
			break;	
	}
	
	return retorno;
}

function isEmail(textbox)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var address = textbox.value;
	if(reg.test(address) == false) 
		return false;
	return true;
}

function confirmPassword(objOrig, objDest)
{
	if (objOrig.value == objDest.value)
		return true;
	return false;
}

function isDate(textbox)
{
	var value = textbox.value;
	
	var year = value.substring(0, value.indexOf("-"));
	year = parseInt(year);
	var month = value.substring(value.indexOf("-") + 1, value.lastIndexOf("-"));
	month = (month.charAt(0) == "0" ? month.charAt(1) : month);
	month = parseInt(month);
	var day = value.substring(value.lastIndexOf("-") + 1, value.length);
	day = (day.charAt(0) == "0" ? day.charAt(1) : day);
	day = parseInt(day);
	
	if ((month < 1) || (month > 12) || (day < 1) || (day > 31) || (year < 1900))
		return false;
	
	// Is it not february?
	if (month != 2)
	{
		// Month has 30 or 31 days
		
			if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
				if (day > 30)
					return false;		
	}
	else
	{
		if (year%4 == 0)
		{
			if (day > 29)
				return false;			
		}
		else
			if (day > 28)
				return false;
	}
	
	return true;
}


function trim(str)
{	
	// cleaning the begining	
	while(str.charAt(0) == " ")
		str = str.substring(1);
	//cleaning the end
	while(str.charAt(str.length - 1) == " ")
		str = str.substring(0, str.length - 1);	
	return str;
}

function isFilled(fieldValue)
{
	if ((fieldValue == null) || (trim(fieldValue) == ""))
	 return false;
	return true;
}

function remove(items, type, isMultiple)
{
	var result = confirm("Are you sure to remove this item(s)?");
	
	if (result)
	{
		if (!isMultiple)
			location.href = "remove_" + type + ".php?id=" + items;
		else
			location.href = "remove_" + type + ".php?id=" + escape(items) + "&m=1";
	}
}

function removeMultiple(type, quantity)
{
	var isValid = false; //no selected items	
	var items = "";
	var itemsSelected = 0;
	
	for (var i = 0 ; i<quantity ; i++)
	{
		if (document.getElementById("check" + i + "Check").checked)
		{			
			isValid = true;
			items += (items == "" ? "" : ",") + document.getElementById("check" + i + "Check").value;
			itemsSelected++;			
		}		
	}
	
	if (isValid)
	{		
		if (itemsSelected > 1)
		{	
			remove(items, type, true);
		}
		else
		{			
			remove(items, type, false);
		}
	}
	else
		alert("You have to select an item.");	
}

function checkAllItems(checkbox, quantity)
{	
	if (checkbox.checked)
		for (var i = 0 ; i<quantity ; i++)
			document.getElementById("check" + i + "Check").checked = true;
	else
		for (var i = 0 ; i<quantity ; i++)
			document.getElementById("check" + i + "Check").checked = false;	
}

function editItem(type, quantity)
{
	var isValid = false; //no selected items
	var itemsSelected = 0;
	var item;
	
	for (var i = 0 ; i<quantity ; i++)
	{
		if (document.getElementById("check" + i + "Check").checked)
		{
			if (!isValid)
			{
				isValid = true;
				item = document.getElementById("check" + i + "Check").value;
				itemsSelected++;
			}
			else
			{
				isValid = false;
				itemsSelected++;
				break;
			}
		}		
	}
	
	if (isValid)
		location.href = "edit_" + type + ".php?id=" + escape(item);
	else
	{
		if (itemsSelected == 0)
			alert("You have to select an item.");
		else
			alert("You have to select only one item.");
	}
}

function formatCurrency(textbox)
{
	var value = textbox.value;
	
	var i = parseFloat(value);
	
	if(isNaN(i)) { i = 0.00; }
	
	var minus = '';
	
	if(i <0) { minus = '-'; }
	
	i = Math.abs(i);
	
	i = parseInt((i + .005) * 100);
	
	i = i / 100;
	
	s = new String(i);
	
	if(s.indexOf('.') <0) 
	{ 
		s += ".00"; 
	}
	
	if(s.indexOf('.') == (s.length - 2)) { s += "0"; }
	
	s = minus + s;
	
	textbox.value=s;
		
}

function cleanField(field, type)
{
	switch (type)
	{
		case "n":
		{
			if (field.value == "Name")
				field.value = "";
			break;
		}
		case "e":
		{
			if (field.value == "E-mail")
				field.value = "";
			break;
		}
		case "p":
		{
			if (field.value == "     Email")
				field.value = "";
			break;
		}
		default:
			break;
	}
}

function isFloat(textbox)
{	
	var hasComma = false;
	for (var i = 0 ; i<textbox.value.length ; i++)
	{
		var strValue = textbox.value.charAt(i);
		var retorno;
		
		switch(strValue)
		{
			case "0":
				retorno = true;
				break;
			case "1":				
				retorno = true;
				break;
			case "2":
				retorno = true;
				break;
			case "3":
				retorno = true;
				break;
			case "4":
				retorno = true;
				break;
			case "5":
				retorno = true;
				break;
			case "6":
				retorno = true;
				break;
			case "7":
				retorno = true;
				break;
			case "8":
				retorno = true;
				break;
			case "9":
				retorno = true;
				break;
			case "-":
			{
				if (i == 0)
					retorno = true;
				else
					retorno = false;
				break;
			}
			case ".":
			{
				if ((i != 0) && (!hasComma))
				{
					retorno = true;
					hasComma = true;
				}
				else
					retorno = false;
				break;
			}
			default:
				retorno = false;
				break;
		}	
		
		if (retorno == false)
			break;	
	}
	
	return retorno;
}

function GetXmlHttpObject()
{	
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari		
		
		return new XMLHttpRequest();		
	}
	if (window.ActiveXObject)
	{
		// code for IE6, IE5		
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function setAsMyDefaultLocation(locationId, radius)
{	
	xmlhttp = GetXmlHttpObject();
		
	if (locationId != "")
	{
		if ((radius == "") || (radius == null))
			radius = 3;
		var url = "setDefaultLocation.php?l= " + escape(locationId);
		url += "&r=" + escape(radius);
		
		xmlhttp.onreadystatechange = responseDefaultLocation;
		
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	
}

function responseDefaultLocation()
{
	if (xmlhttp.readyState == 4)
	{		
		var responseValue = xmlhttp.responseText;		
		
		//document.getElementById("errorDinMsg").style.visibility = "";
		
		if ((responseValue == "0") || (responseValue.search(";") == -1))
		{		
			//alert(curDescription + " " + curValue + " already exists.");
			//document.getElementById(curField).focus();			
			alert("An error occurred.\nPlease, verify if cookies are enabled in you browser.");			
			//location.href = "#errorMessageAnchor";
		}
		else if (responseValue.search(";") != -1)
		{
			var values = responseValue.split(";");
			if (values.length != 3)
					alert("An error occurred.");
			else
			{
				alert("Your default location was successfully configured.\n\nLocation: " + values[0] + "\nRadius: " + values[1] + "km");
				goToProgramAgain(values[2]);
			}
		}
		/*else
		{			
			alert("An unespecified error occurred");
		}*/
	}
}

function handleEnter (field, event) 
{
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13) 
    {
        var i;
        for (i = 0; i < field.form.elements.length; i++)
            if (field == field.form.elements[i])
                break;
        i = (i + 1) % field.form.elements.length;
        field.form.elements[i].focus();
        return false;
    }
    else
    return true;
}      

function setLastSelectedSeasonProgram(programId, seasonName)
{	
	xmlhttp = GetXmlHttpObject();
	
	if ((programId != "") && (seasonName != ""))
	{
		var url = "setLastSelectedSeasonProgram.php?p= " + escape(programId);
		url += "&s=" + escape(seasonName);
		
		xmlhttp.onreadystatechange = responseLastSelectedSeasonProgram;
		
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	
}

function responseLastSelectedSeasonProgram()
{
	if (xmlhttp.readyState == 4)
	{
		return;
	}
}

function getSearchLocationDiv()
{	
	var url = "divSearchLocation.php?default-location=1";
	
	var width = 800;
	
	var height = 650;
	
	var left = (screen.width-width)/2;
	var top = (screen.height-height)/2;
	
	window.open(url, "", "directories=no, location=no, status=no, resizable=no, toolbar=no, width=" + width + ", height=" + height + ",left=" + left + ",top=" + top);
}


function responseSearchLocationDiv()
{	
	if (xmlhttp.readyState == 4)
	{
		var responseValue = xmlhttp.responseText;
		
		if (responseValue != "")
			drawSearchLocationDiv(responseValue);
	}
}