function CheckLeadingZero(currentval,thisname,message)
{//This function checks for any leading zero's, not used at the moment
	var checktext = "";
	var thisvalue = "";

	checktext = currentval;		
	thisvalue = checktext.charAt(0);

	if (currentval.length > 1)
	{
		//I had to use variable here as SText.charAt() wasn't working in the if.
		if (thisvalue == '0')
		{			
			alert(message)
			//document.getElementById(thisname).focus()
		}		
	}
}

function FormLeadingZero(sText)
{//Checks if there are any leading zero's and stops form load if there are
  	var checktext = sText;	
	var currentstring = "";
	var thisvalue = "";

	thisvalue = checktext.charAt(0);
	
if (sText.length > 1)
	{
		//I had to use variable here as SText.charAt() wasn't working in the if.
		if (thisvalue == 0)
		{			
			alert("Please remove all leading zero(s) before submitting form")
			return false;
		}	
	}	 
}


function IsTelephone(sText)
{//This function checks if the string is a number with spaces allowed like a telephone no.
   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) 
         {
         return false;
         }
      }     
}

function RemoveComma(sText)
{//This function removes any commas from a string, currently used by other validation funcs
	var checktext = sText;	
	var currentstring = "";
	var thisvalue = "";

//Check is done here to see if comma exists checking each character
	for (i=0;i<checktext.length;++i)
	{		
		thisvalue = checktext.charAt(i);
	
		//I had to use variable here as SText.charAt() wasn't working in the if.
		if (thisvalue != ',')
		{			
			currentstring = currentstring + checktext.charAt(i);
		}
	}
	
	return currentstring;
}

function emailvalidation(entered, alertbox)
{//This function just checks if the entered string is a valid email
with (entered)
{
apos=value.indexOf("@"); 
dotpos=value.lastIndexOf(".");
lastpos=value.length-1;
if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) 
{if (alertbox) {alert(alertbox);} return false;}
else {return true;}
}
}

function valuevalidation(entered, min, max, alertbox, datatype)
{//This function does a range check
var currentvalue = "";
with (entered)
{
if (value==null || value =="")
{ return true;}
//Comma's removed from string before check
currentvalue = RemoveComma(value)
checkvalue=parseFloat(currentvalue);

if (datatype)
{smalldatatype=datatype.toLowerCase();
if (smalldatatype.charAt(0)=="i") {checkvalue=parseInt(currentvalue)};
}
if ((parseFloat(min)==min && checkvalue<min) || (parseFloat(max)==max && checkvalue>max) || currentvalue!=checkvalue)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}

//Older validation used for required fields
function emptyvalidation(entered, alertbox)
{//Checks if the field is required
with (entered)
{
if (value==null || value=="")
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}

//Currently used OR validtor, selecting one field or the other
function orvalidation(entered1,entered2,alertbox)
{//Takes names of two text boxes only one of which you have to fill out

if (entered1.value !="" && entered2.value !="")
{
alert(alertbox);
return false;
}
else
{ return true;
}
}


function GET_HTTP_QueryString(Query_String_Name) 
{//Checks the query string for any id's
 var i, pos, argname, argvalue, queryString, pairs; 
 queryString = location.href.substring(location.href.indexOf("?")+1);
 // split parameters into pairs, assuming pairs are separated by ampersands
 pairs = queryString.split("&");
 // for each pair, we get the name and the value
        for (i = 0; i < pairs.length; i++) { 
	  pos = pairs[i].indexOf('='); 
	  if (pos == -1) {
	  continue; 
	  }
	  argname = pairs[i].substring(0,pos);
	  argvalue = pairs[i].substring(pos+1); 
	// Replaces "Google-style" + signs with the spaces they represent
	    if (argname == Query_String_Name) {
	       return unescape(argvalue.replace(/\+/g, " "));
	    }
	}
	return false;
}

function trim(thisString)
{//Trims spaces within the string, not working at the moment
	var newString = thisString;
	while (newString.charCodeAt(0) < 33)
	{
		newString = newString.substring(1,newString.length);
	}
	
	while (newString.charCodeAt(newString.length - 1) < 33)
	{
		newString = newString.substring(0, newString.length - 1);
	}
	return newString;
}


function ValidateIntegerFields(thisform,Fields,Desc,AllowComma,AllowNeg)
{//Currently used Integer validation

	var tmpVal='';
	var FieldArray=new Array();
	var DescArray=new Array();
	var tmp=0;
	var tmpMsg='';
	// Create array for Field list
	tmpVal=Fields;
	do
	{
		tmp=tmpVal.indexOf('|');
		if (tmp == -1)
		{
			if (tmpVal != '')
			{
				FieldArray[FieldArray.length]=tmpVal;
				tmpVal='';
			}
		} else {
			FieldArray[FieldArray.length]=tmpVal.substring(0,tmp);
			tmpVal=tmpVal.substring(tmp + 1);
		}
	}
	while (tmpVal != '');
	// Create array for Desc list
	tmpVal=Desc;
	do
	{
		tmp=tmpVal.indexOf('|');
		if (tmp == -1)
		{
			if (tmpVal != '')
			{
				DescArray[DescArray.length]=tmpVal;
				tmpVal='';
			}
		} else {
			DescArray[DescArray.length]=tmpVal.substring(0,tmp);
			tmpVal=tmpVal.substring(tmp + 1);
		}
	}
	while (tmpVal != '');
	// Check to see if passed strings are of equal length
	if (FieldArray.length != DescArray.length)
	{
		alert('Fatal error: ValidateInteger - Passed lists do not have the same length');
		return false;
	}
	// Validate fields
	for (i=0; i<FieldArray.length;i++)
	{
		tmpMsg='';
		eval('tmpVal=thisform.' + FieldArray[i] + '.value');
		if (!AllowNeg && tmpVal < 0)
		{
			alert(DescArray[i] + ' may not be a negative value.');
			// Fix for IWOF NSO
			if (FieldArray[i].substring(0,4) == 'Hold')
			{
				eval('thisform.Qty_' + FieldArray[i].substring(5) + '.select()');
			} else {
				eval('thisform.' + FieldArray[i] + '.select()')
			}
			return false;
		}
		if (!ValInt(tmpVal,AllowComma))
		{
			if (AllowComma != 1 && tmpVal.indexOf(',') != -1) tmpMsg=' without commas';
			if (tmpVal.indexOf('.') != -1) tmpMsg=' (no decimal fraction)';
			alert(DescArray[i] + ' must be an integer value' + tmpMsg + '.');
			// Fix for IWOF NSO
			if (FieldArray[i].substring(0,4) == 'Hold')
			{
				eval('thisform.Qty_' + FieldArray[i].substring(5) + '.select()');
			} else {
				eval('thisform.' + FieldArray[i] + '.select()')
			}
			return false;
		}
	}
	return true;
}




function ValidateFloatFields(thisform,Fields,Desc,AllowComma)
{//Currently used Float validation

	var tmpVal='';
	var FieldArray=new Array();
	var DescArray=new Array();
	var tmp=0;
	var tmpMsg='';
	// Create array for Field list
	tmpVal=Fields;
	do
	{
		tmp=tmpVal.indexOf('|');
		if (tmp == -1)
		{
			if (tmpVal != '')
			{
				FieldArray[FieldArray.length]=tmpVal;
				tmpVal='';
			}
		} else {
			FieldArray[FieldArray.length]=tmpVal.substring(0,tmp);
			tmpVal=tmpVal.substring(tmp + 1);
		}
	}
	while (tmpVal != '');
	// Create array for Desc list
	tmpVal=Desc;
	do
	{
		tmp=tmpVal.indexOf('|');
		if (tmp == -1)
		{
			if (tmpVal != '')
			{
				DescArray[DescArray.length]=tmpVal;
				tmpVal='';
			}
		} else {
			DescArray[DescArray.length]=tmpVal.substring(0,tmp);
			tmpVal=tmpVal.substring(tmp + 1);
		}
	}
	while (tmpVal != '');
	// Check to see if passed strings are of equal length
	if (FieldArray.length != DescArray.length)
	{
		alert('Fatal error: ValidateFloat - Passed lists do not have the same length');
		return false;
	}
	// Validate fields
	for (i=0; i<FieldArray.length;i++)
	{
		tmpMsg='';
		eval('tmpVal=thisform.' + FieldArray[i] + '.value');
		if (!ValFloat(tmpVal,AllowComma))
		{
			if (AllowComma != 1 && tmpVal.indexOf(',') != -1) tmpMsg=' without commas';
			alert(DescArray[i] + ' must be a numeric value' + tmpMsg + '.');
			eval('thisform.' + FieldArray[i] + '.focus()')
			return false;
		}
	}
	return true;
}


//Used by currently used integer validation
function ValInt(ObjectValue,AllowComma)
{
	// Used internally by VaidateInteger

	//Returns true if value is a number or is NULL
	//otherwise returns false	
	
	if (ObjectValue.length == 0) return true;
	
	//Returns true if value is an integer defined as
	//   having an optional leading + or -.
	//   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;
	
	//The first character can be + -  blank or a digit.
	check_char = ObjectValue.indexOf(decimal_format)
	//Was it a decimal?
	if (check_char < 1) return ValFloat(ObjectValue,AllowComma); else return false;
}

//Used by currently used float validation
function ValFloat(ObjectValue,AllowComma)
{
	// Used Internally by ValidateInt, ValidateFloat

	//Returns true if value is a number or is NULL
	//otherwise returns false	
	
	if (ObjectValue.length == 0) return true;
	
	//Returns true if value is a number defined as
	//   having an optional leading + or -.
	//   having at most 1 decimal point.
	//   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	if (AllowComma == 1) number_format=number_format + ',';
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;
	
	//The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(ObjectValue.charAt(0))
	//Was it a decimal?
	if (check_char == 1)
		decimal = true;
	else if (check_char < 1)
		return false;
	
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < ObjectValue.length; i++)
	{
		check_char = number_format.indexOf(ObjectValue.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits) trailing_blank = true;
			// ignore leading blanks
		}
		else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
	//All tests passed, so...
	return true
}



