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;
	}
}
