function IsEmpty(aTextField)
{
	if (aTextField.value == "")
		return true;
	else 
		return false;
} 

function Decimal(sText, sSign)
{
	var ValidChars;
	var IsNumber;
	var Character;

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

function Integer(sText,sSign)
{
	var ValidChars;
	var IsNumber;
	var Character;

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

function ValidateAccess(control)
{
	var Access;

	IsAccess = true;
	if(IsEmpty(control))
     {
     	alert('You cannot leave the '+control.name+' field blank');
          control.focus();
		IsAccess = false;
	}
     if(!Integer(control.value, "+"))
     {
     	alert('The '+control.name+' field must contain a whole number between 0 and 255 inclusive');
          control.focus();
		IsAccess = false;
	}
     if(control.value>255)
     {
     	alert('The '+control.name+' field must contain a whole number between 0 and 255 inclusive');
          control.focus();
		IsAccess = false;
	}
     return IsAccess;
}

function ValidateCoefficient(control)
{
	var IsCoefficient;

	IsCoefficient = true;
	if(IsEmpty(control) || control.value == "")
	{
		alert('You cannot leave the '+control.name+' field blank');
		control.focus();
		IsCoefficient = false;
	}
	if(!Decimal(control.value,"+-"))
	{
		alert('The '+control.name+' field must contain a decimal number');
		control.focus();
		IsCoefficient = false;
	}
	return IsCoefficient;
}

function ValidateColor(control)
{
	var IsColor;

	IsColor = true;
	if(IsEmpty(control))
     {
     	alert('You cannot leave the '+control.name+' field blank');
          control.focus();
		IsColor = false;
	}
     if(!Integer(control.value, "+"))
     {
     	alert('The '+control.name+' field must contain a whole number between 0 and 255 inclusive');
          control.focus();
		IsColor = false;
	}
     if(control.value>255)
     {
     	alert('The '+control.name+' field must contain a whole number between 0 and 255 inclusive');
          control.focus();
		IsColor = false;
	}
     return IsColor;
}

function ValidateIntensity(control)
{
	var IsIntensity;

	IsIntensity = true;
	if(IsEmpty(control))
     {
     	alert('You cannot leave the '+control.name+' field blank');
          control.focus();
		IsIntensity = false;
	}
     if(!Decimal(control.value, "+"))
     {
     	alert('The '+control.name+' field must contain a positive non-zero number');
          control.focus();
		IsIntensity = false;
	}
     return IsIntensity;
}

function ValidatePassword(Password1, Password2)
{
	var IsPassword;

	IsPassword = true;
	if(IsEmpty(Password1) && IsEmpty(Password2))
     {
     	alert('Both Password fields must be filled in with matching values');
          Password1.focus();
		IsPassword = false;
	}
     else if(!(Password1.value == Password2.value))
     {
     	alert('Both Password fields must be filled in with matching values');
          Password1.focus();
		IsPassword = false;
	}
     return IsPassword;
}

function ValidateSize(control)
{
	var IsSize;

	IsSize = true;
	if(IsEmpty(control))
     {
     	alert('You cannot leave the '+control.name+' field blank');
          control.focus();
		IsSize = false;
	}
     if(!Integer(control.value, "+"))
     {
     	alert('The '+control.name+' field must be a whole number');
          control.focus();
		IsSize = false;
	}
     return IsSize;
}

function ValidateWeight(control)
{
	var IsWeight;

	IsWeight = true;
	if(IsEmpty(control))
     {
     	alert('You cannot leave the '+control.name+' field blank');
          control.focus();
		IsWeight = false;
	}
     if(Integer(control.value, "+")==0)
     {
     	alert('The '+control.name+' field must contain a positive whole number');
          control.focus();
		IsWeight = false;
	}
	return IsWeight;
}

//Webpage-called Functions 

function ValidateAccount(form)
{
	var GoAhead;

	GoAhead = true;
	if (IsEmpty(form.UserName))
	{
		alert('You must enter a value in the Name field');
     	GoAhead = false;
	}
	if (!ValidatePassword(form.Password1, form.Password2))
		GoAhead = false;
	if (!ValidateAccess(form.AccessRights))
		GoAhead = false;
	if (GoAhead)
		form.submit();
	return false;
}

function ValidateAdd(form)
{
	var GoAhead;
     
	GoAhead = true;    
	if (form.Created.value == "No")
	{
		if (!ValidateColor(form.IFSred))
     		GoAhead = false;
		if (!ValidateColor(form.IFSgreen))
     		GoAhead = false;
		if (!ValidateColor(form.IFSblue))
     		GoAhead = false;
		if (!ValidateIntensity(form.intensity))
     		GoAhead = false;
	}
	if (!ValidateCoefficient(form.A))
     	GoAhead = false;
	if (!ValidateCoefficient(form.B))
     	GoAhead = false;
	if (!ValidateCoefficient(form.E))
     	GoAhead = false;
	if (!ValidateCoefficient(form.C))
     	GoAhead = false;
	if (!ValidateCoefficient(form.D))
     	GoAhead = false;
	if (!ValidateCoefficient(form.F))
     	GoAhead = false;
	if (!ValidateWeight(form.P))
     	GoAhead = false;
	if (!ValidateColor(form.red))
     	GoAhead = false;
	if (!ValidateColor(form.green))
     	GoAhead = false;
	if (!ValidateColor(form.blue))
     	GoAhead = false;
	if (GoAhead)
	{
		form.Order.value = "Add";
		form.submit();
	}
	return false;
}

function ValidateCreateFile(form)
{
	var GoAhead;

	GoAhead = true;
	if(!ValidateSize(form.Size))
     	GoAhead = false;
	if(form.Filename.value == "")
	{
     	alert('Please enter a name for your file');
          form.Filename.focus();
		GoAhead = false;
	}
	if (GoAhead)
		form.submit();
	return false;
}

function ValidateDisplay(form)
{
	var GoAhead;

	GoAhead = true;
	if(!(IsEmpty(form.A)&&IsEmpty(form.B)&&IsEmpty(form.E)&&IsEmpty(form.C)&&IsEmpty(form.D)&&IsEmpty(form.F)&&IsEmpty(form.P)&&IsEmpty(form.red)&&IsEmpty(form.green)&&IsEmpty(form.blue)))
     {
		if (!ValidateCoefficient(form.A))
     		GoAhead = false;
		if (!ValidateCoefficient(form.B))
     		GoAhead = false;
		if (!ValidateCoefficient(form.E))
     		GoAhead = false;
		if (!ValidateCoefficient(form.C))
     		GoAhead = false;
		if (!ValidateCoefficient(form.D))
     		GoAhead = false;
		if (!ValidateCoefficient(form.F))
     		GoAhead = false;
		if (!ValidateWeight(form.P))
     		GoAhead = false;
		if (!ValidateColor(form.red))
     		GoAhead = false;
		if (!ValidateColor(form.green))
     		GoAhead = false;
		if (!ValidateColor(form.blue))
     		GoAhead = false;
	}
	if (GoAhead)
	{
		form.Order.value = "Display";
		form.submit();
	}
	return false;
}

function ValidateExtend(form)
{
	form.Order.value = "Extend";
	form.submit();
	return false;
}

function ValidateLoad(form)
{
	if(form.RecordID.value == 0)
     {
     	alert('Please select an IFS to load');
          form.RecordID.focus();
	}
	else
	{
		form.Order.value = "Load";
		form.submit();
	}
	return false;
}

function ValidateMutate(form)
{
	form.Order.value = "Mutate";
	form.submit();
	return false;
}

function ValidateNew(form)
{
	form.Order.value = "New";
	form.submit();
	return false;
}

function ValidateRandom(form)
{
	form.Order.value = "Random";
	form.submit();
	return false;
}

function ValidateRefresh(form)
{
	form.Order.value = "Refresh";
	form.submit();
	return false;
}

function ValidateSavetoDB(form)
{
	if(form.IFSName.value != "")
	{
		form.Order.value = "Save As";
		form.submit();
	}
	else
     {
     	alert('Please enter a name for your IFS');
          form.IFSName.focus();
	}
	return false;
}

function ValidateUpdateSession(form)
{
	var GoAhead;
	var ATs;
	var CurrentAT;
	var Field;

	GoAhead = true;
	ATs = form.ATs.value;    
	if (!ValidateColor(form.IFSred))
    		GoAhead = false;
	if (!ValidateColor(form.IFSgreen))
    		GoAhead = false;
	if (!ValidateColor(form.IFSblue))
    		GoAhead = false;
	if (!ValidateIntensity(form.Intensity))
    		GoAhead = false;
	for (CurrentAT = 0; CurrentAT < ATs; CurrentAT++) 
	{
		Field=document.getElementById(CurrentAT+"A");
		if (!ValidateCoefficient(Field))
	     	GoAhead = false;
		Field=document.getElementById(CurrentAT+"B");
		if (!ValidateCoefficient(Field))
	     	GoAhead = false;
		Field=document.getElementById(CurrentAT+"C");
		if (!ValidateCoefficient(Field))
	     	GoAhead = false;
		Field=document.getElementById(CurrentAT+"D");
		if (!ValidateCoefficient(Field))
	     	GoAhead = false;
		Field=document.getElementById(CurrentAT+"E");
		if (!ValidateCoefficient(Field))
	     	GoAhead = false;
		Field=document.getElementById(CurrentAT+"F");
		if (!ValidateCoefficient(Field))
	     	GoAhead = false;
		Field=document.getElementById(CurrentAT+"P");
		if (!ValidateWeight(Field))
	     	GoAhead = false;
		Field=document.getElementById(CurrentAT+"red");
		if (!ValidateColor(Field))
	     	GoAhead = false;
		Field=document.getElementById(CurrentAT+"green");
		if (!ValidateColor(Field))
	     	GoAhead = false;
		Field=document.getElementById(CurrentAT+"blue");
		if (!ValidateColor(Field))
	     	GoAhead = false;
	} 
	if (GoAhead)
	{
		form.Order.value = "Update";
		form.submit();
	}
	return false;
}


