// JavaScript Document


function checkAll(field)
{
	for (i = 0; i < field.length; i++)
		field[i].checked = true ;
}

function uncheckAll(field)
{
	for (i = 0; i < field.length; i++)
		field[i].checked = false ;
}


//form validation
var msg = "Please enter a valid \n";
var msgPass = "Please re-enter the password correct";
var msgPhoto = "No other format than JPEG is allowed in photo location field";
var msgEmail = "Please enter a valid email address";
var msgFile = "Please enter a valid file";

function validate(form)
{
	var e = '';
	
	firstField = true;
	for (var i = 0; i < form.elements.length; i++)
	{
		var j = form.elements[i];

		j.style.border='1px solid #9EB3C3';
		
		var strID = new String(j.id);

		switch (j.type)
		{
			case 'text':

			if (strID.indexOf("email") != -1)
			{
				if (j.value == '')
				{
					e += msgEmail+"<br/>";
					throwMsg(j, "");
				}
				else if (!isEmail(j.value))
				{
					e += throwMsg(j, msg);
				}
			}

			if (strID.indexOf("emailNull") != -1)
			{
				if (!isEmail(j.value))
				{
					e += throwMsg(j, msg);
				}
			}

			if (strID.indexOf("text") != -1)
			{				
				if (j.value == '')
				{
					e += throwMsg(j, msg);
				}
				else if (!isString(j.value))
				{
					e += throwMsg(j, msg);
				}
			}

			if (strID.indexOf("textNull") != -1)
			{
				if (!isString(j.value))
				{
					e += throwMsg(j, msg);
				}
			}

			if (strID.indexOf("number") != -1)
			{
				if (j.value == '')
				{
					e += throwMsg(j, msg);
				}
				else if (!isNumeric(j.value))
				{
					e += throwMsg(j, msg);
				}
			}
		
			if (strID.indexOf("numberNull") != -1)
			{
				if (!isNumeric(j.value))
				{
					e += throwMsg(j, msg);
				}
			}
			
			break;
			
			case 'password':

			if (strID.indexOf("password") != -1)
			{
				if(strID == "password") {
					var elemPasRe = document.getElementById("retype");
					if(elemPasRe != null)
						if ((j.value == "") && (elemPasRe.value == "")) {							
							e += throwMsg(j, msg);
							elemPasRe.value = "";
							j.value = "";
						}
						else if (j.value != elemPasRe.value) {
							e += msgPass+"<br/>";
							throwMsg(j, "");
							j.value = "";
							elemPasRe.value = "";
						}
					else if (j.value == '')
					{
						e += throwMsg(j, msg);
					}
					else if (!isString(j.value))
					{
						e += throwMsg(j, msg);
					}
					else if (j.value.length < 6)
					{
						e += throwMsg(j, msg);
					}
				}
			}
			
			break;
			
			case 'file':
			
			if (strID.indexOf("file") != -1)
			{
				if ((isEmpty(j.value) || !isString(j.value)) && isEmpty(form.fileID.value))
				{
					e += msgFile+"<br/>";
					throwMsg(j, msg);
				}
			}
			
			if (strID.indexOf("filePhoto") != -1)
			{
				if (!isPhotoLocation(j.value))
				{
					e += msgPhoto+"<br/>";
					throwMsg(j, msg);
				}
			}
			
			break;
			
			case 'textarea':
				if (strID.indexOf("text") != -1)
				{
					if (j.value == '')
					{
						e += throwMsg(j, msg);
					}
					else if (!isString(j.value))
					{
						e += throwMsg(j, msg);
					}
				}
			
			case 'select-one':	
				if (strID.indexOf("text") != -1)
				{				
					if (j.value == '')
					{
						e += throwMsg(j, msg);
					}
					else if (!isString(j.value))
					{
						e += throwMsg(j, msg);
					}
				}
			break;

			/*case 'select-one':

			if (j.name == 'zip' && j.selectedIndex == 0)
			{
				e += "Please select a valid zip code\n";
			}

			break;

			case 'checkbox':

			if (j.type == 'checkbox' && !j.checked)
			{
				e += "You must agree to our rules\n";
			}

			break;*/
		}

		if (e && firstField)
			focusControl = j;
		if (e)
			firstField = false;
	}
	
	if ( e.length > 0 )
	{
		document.getElementById("messages").innerHTML = e;
		document.getElementById("messages").style.display = 'block';
		window.location.href = "#msg";
		focusControl.focus();
		return false;
	}
	else
	{
		document.getElementById("messages").style.display = 'none';
		/*form.submit();*/
		return true;
	}
}

function throwMsg(obj, msg)
{
	obj.style.border='1px solid red';
	return msg + obj.name.replace("_"," ") + "<br/>";
}


//field validation
var database_varchar_length = 210;


function isEmpty(s)
{ 
	return ((s == null) || (s.length == 0)) 
}

function isString(s)
{
	return (s.length < database_varchar_length);
}

function isNumeric(sText)
{
   var ValidChars = "0123456789-";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   if (sText.length == 0)
   	IsNumber = true;
	
   return IsNumber;
}


function isPhotoLocation(loc)
{
	var isLoc = false;
	loc = loc.toLowerCase();
	if (loc == "")
		isLoc = true;
	if (loc.indexOf(".jpg") != -1)
		isLoc = true;
	if (loc.indexOf(".jpeg") != -1) 
		isLoc = true;

	return isLoc;
}


function isEmail(e)
{
	var teste = false;
	var strtest = new String(e);
	var index = strtest.indexOf("@");

	if (index > 0)
	{
		var eid = strtest.indexOf(".",index);

		if (eid > index+1 && strtest.length > eid+1)
		{
			teste = true;
		}

		return teste;
	}
}
