//********** Utilities used with MarketNet Inc.   ***************
//********** Written by Michael Lucas, 3rd Qtr 98 ***************
//********** Purpose:  Field validation and value ***************
//********** checking.				  ***************


// The following function trims all non number characters from a string.
function trim_all_chars (input)
{	
	var pointer;
	var howlong;

	howlong = input.length;
	pointer = 0;
	
	while (pointer <= howlong)
	{
		if ( isNaN(input.charAt(pointer))|| input.charAt(pointer) == " ")
		{
			input = input.substring(0,pointer) + input.substring(pointer+1, input.length);
			howlong = howlong - 1;
		}
		else
		{
			pointer = pointer + 1;
		}
	}
	return input;
}

// This function takes some input, the name of the input and it's required length.
// It returns a message if the input's length does not meet the requirement
function validate_specific_length (input,input_name, req_length)
{
	var msg = "";
	if (input.length != req_length)
		msg = "The "+input_name+" field is not "+req_length +" long. \n";
	return msg;
}

// This function validates some input for whether it meets a minimum length.
// It returns a message if the input's length does not meet the requirement
function validate_minimum_length (input, input_name, min_length)
{
	var msg = "";
	if (input.length < min_length)
		msg = "The "+input_name+" field is not at least "+min_length +" long. \n";
	return msg;
}

// This function validates some input for whether it has an @ in it.
// It returns a message if the input does not contain and @
function validate_email (input)
{
	var msg = "";
	if (input.indexOf("@",0) == -1)
		msg = "The e-mail address doesn not contain a '@' symbol \n";
	return msg;
}

// This function validates a date input to make sure it conforms to 
// dd/mm/yyyy format.  Returns True/False.

function IsValidDate( fld )
{
	var f = (typeof fld == "object") ? fld.value : fld;
	var y, m, d, cy, cm, cd;
	var dd;
	var c;
	var Ok = true
	
	//Verify correct length.
	if (f.length != 10)
	{
		return false;
	}

	//Verify date format is 99/99/9999
	for ( var i=0; i<10 && Ok; i++ )
	{
		c = f.substring(i,i+1);
		if ( i==2 || i==5 )
		{
			//Check for date separator character.
			Ok = ( c == '/' );
		}
		else
		{
			//Check for a number.
			Ok = ( c >= '0' && c <= '9' );
		}
	}
	if ( !Ok )
	{
		return false;
	}

	//The date entered may not be correct.  For example 1997/06/32 is actually 1997/07/02.
	//Create a date with the date inputted and compare the input date with the created date.
	//The created date adjusts for incorrect dates.
	
	m = f.substring(0,2);
	d = f.substring(3,5);
	y = f.substring(6,10);

	dd = new Date(y,m-1,d);
	cm = dd.getMonth()+1;
	cd = dd.getDate();
	cy = dd.getYear();
	cy = cy<1000 ? cy + 1900 : cy;  //Adjust year due to Netscape and Microsoft's handling of the getYear method.

	//alert ("y="+y+" m-1="+m+" d="+d+" cd="+cd+" cm="+cm+" cy="+cy);
	if ( y!=cy || m!=cm || d!=cd )
	{
		return false;
	}
		
	return true;
}

// This function validates a date input to make sure it conforms to 
// dd/mm/yyyy format.  It returns the date with the appropriate length
// if it is not the right length.

function MakeValidDate (input)
{
	var occurance = "";
	occurance = input.indexOf("/", 0);
	if (occurance == 1)
	{
		input = "0" + input;
	}
	occurance = input.indexOf("/", 3);
	if (occurance == 4)
	{
		input = input.substring(0,3) + "0" + input.substring (3,input.length+1);
	}
	return input;
}

function validnumber (input, input_name)
{
	var msg = "";
	input = trim_all_chars(input)
	if (input == "")
	{
		return input_name + " is not a valid number. \n";	
	}	 
	if (input.length < 1)
	{
		return input_name + " is not a valid number. \n";
	}
	if (isNaN((input+1-1)*1))
	{
		return input_name + " is not a valid number. \n";
	}
	return msg;
}
