/*	Jim Mallmann
	Ascedia	
	Date: May 27, 2003	
	add trim functions to string object
*/
function strltrim()
{
	return this.replace(/^\s+/,'');
}

function strrtrim()
{
	return this.replace(/\s+$/,'');
}

function strtrim()
{
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

String.prototype.ltrim = strltrim;
String.prototype.rtrim = strrtrim;
String.prototype.trim = strtrim;

// end add trim functions to string object

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

// added code to accept display name for second field that is usually empty
// e.g. if required field name is name it is normally passed in as MM_validateForm('name','','R')
//      new code accept value for second field as follows: MM_validateForm('name','First and Last Name', 'R')
//      error will display as "First and Last Name is required" instead of "name is required"
function MM_validateForm() 
{ 
	var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
	for (i=0; i<(args.length-2); i+=3) 
	{ 
		test=args[i+2]; 
		val=MM_findObj(args[i]);
		if (val) 
		{ 
			//nm=val.name; 
			// changed name to take value between form field name 
			// and validation type --JM 3/11/2003
			nm = args[i+1];
			if ((val=val.value.trim())!="")
			{
				if (test.indexOf('isEmail')!=-1) 
				{ 
					p=val.indexOf('@');
					if (p<1 || p==(val.length-1)) 
						errors+='- '+nm+' must contain an e-mail address.\n';
					else
					{
						dot = val.indexOf('.',p+1)
						if(dot <= p+1 || dot >= val.length-2)
						{
							errors += '- ' + nm + ' must contain a valid e-mail address.\n';
						}
					}
				}
				else if (test!='R')
				{
					num = parseFloat(val);
					if (isNaN(val)) 
						errors+='- '+nm+' must contain a number.\n';
					if (test.indexOf('inRange') != -1) 
					{
						p=test.indexOf(':');
						min=test.substring(8,p); 
						max=test.substring(p+1);
						if (num<min || max<num) 
							errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
					}
				}
			}
			else if (test.charAt(0) == 'R') 
				errors += '- '+nm+' is required.\n'; 
		}
	}
	if (errors) 
		alert('The following error(s) occurred:\n'+errors);
	document.MM_returnValue = (errors == '');
}


/*	// Jim Mallmann
	// Ascedia, Inc.
	// November 1, 2002
	Using JM_copySameAs
	This function is used to copy one set of fields to another set, and is usually used to copy
	a billing address to a shipping address.
	All paired fields must be similarly named for this to work, such as billCity and shipCity.
	Also, it is expected that a checkbox is used to toggle the copy on or off.
	The function allows for a variable number of arguments, 
	but the first three arguments MUST be the name of the check box, the first field prefix,
	and the second field prefix.  
	For example, JM_copySameAs('copybilltoship', 'bill', 'ship', 'FirstName', 'LastName', 'Address', ...)
*/
function JM_copySameAs()
{
	args = JM_copySameAs.arguments;
	numArgs = args.length;
	if(numArgs < 4) return;
	else
	{
		check = args[0];
		c = MM_findObj(check);
		prefix1 = args[1];
		prefix2 = args[2];
		for(i = 3; i < numArgs; i++)
		{
			field1 = MM_findObj(prefix1 + args[i]);
			field2 = MM_findObj(prefix2 + args[i]);
			if(field1.type == "text")
			{
				if(c.checked)
				{
					field2.value = field1.value;
				}
				else
				{
					field2.value = "";
				}
			}
			else if (field1.type == "select-one")
			{
				if(c.checked)
				{
					field2.selectedIndex = field1.selectedIndex;
				}
				else
				{
					field2.selectedIndex = 0;
				}
			}
		}
	}
}

/*	// Jim Mallmann
	// Ascedia, Inc.
	// November 1, 2002
	Using JM_validateDropdowns
	Usually, this function will be used in conjunction with the MM_validateForm() function 
	that Dreamweaver creates to validate a form.
	In this case, alter the onSubmit statement that is created to look like the following:
		MM_validateForm(args); JM_validateDropdowns(fieldList); return (document.MM_returnValue && document.DD_returnValue);
	The fieldList should be replaced by a list of the dropdowns that need to be validated in alternating field name, display name fashion, 
	such as JM_validateDropdowns('state', 'State', 'country', 'Country', 'birthMonth', "Month of Birth', 'birthDay', 'Day of Birth', 'birthYear', 'Year of Birth');
*/
function JM_validateDropdowns()
{
	args = JM_validateDropdowns.arguments;
	errors = "";
	for(i = 0; i < args.length; i+=2)
	{
		dd = MM_findObj(args[i]);
		if(dd.selectedIndex == 0)
		{
			errors += "- selection of " + args[i+1] + " is required.\n";
		}
	}
	if(errors != "") alert("The following error(s) occurred:\n" + errors);
	document.DD_returnValue = (errors == "");
}

/*	// Jim Mallmann
	// Ascedia, Inc.
	// November 29, 2002
	Using JM_validateRadioButtons
	Usually, this function will be used in conjunction with the MM_validateForm() function 
	that Dreamweaver creates to validate a form.
	In this case, alter the onSubmit statement that is created to look like the following:
		MM_validateForm(args); JM_validateRadioButtons(fieldList); return (document.MM_returnValue && document.Radio_returnValue);
	The fieldList should be replaced by a list of the radio buttons that need to be validated, 
	such as JM_validateRadioButtons('q1', 'Question 1');
*/
function JM_validateRadioButtons()
{
	args = JM_validateRadioButtons.arguments;
	errors = "";
	numArgs = args.length;
	for(i = 0; i < numArgs; i+=2)
	{
		radio = MM_findObj(args[i]);
		numButtons = radio.length;
		thisGroupOK = false;
		for(j = 0; j < numButtons; j++)
		{
			if(radio[j].checked)
			{
				thisGroupOK = true;
				break;
			}
		}
		if(!thisGroupOK)
		{
			errors += "- one of the " + args[i+1] + " radio buttons must be selected.\n";
		}
	}
	if(errors != "") alert("The following error(s) occurred:\n" + errors);
	document.Radio_returnValue = (errors == "");
}

/* // Jim Mallmann
 // Ascedia, Inc.
 // November 29, 2002
 // rev. June 10, 2003
 Using JM_validateCheckboxGroup
 Usually, this function will be used in conjunction with the MM_validateForm() function 
 that Dreamweaver creates to validate a form.
 In this case, alter the onSubmit statement that is created to look like the following:
  MM_validateForm(args); JM_validateCheckboxGroup(fieldList); return (document.MM_returnValue && document.CG_returnValue);
 The fieldList should be replaced by a list of the checkbox groups that need to be validated.
 Each checkbox group will have four values associated with it.  
 In order they are minChecked, maxChecked, fieldName, displayName
 For example, JM_validateCheckboxGroup(1, 3, 'info', 'Information'); would force the user to check
 between 1 and three boxes.  For no min use 0, and for no max use -1.
*/
function JM_validateCheckboxGroup()
{
 	args = JM_validateCheckboxGroup.arguments;
 	errors = "";
	numArgs = args.length;
	if(numArgs % 4 != 0) return;
	for(i = 0; i < numArgs; i++)
	{
		minChecked = args[i];
		maxChecked = args[++i];
		checkbox = MM_findObj(args[++i]);
		numBoxes = checkbox.length;
		thisGroupMinOK = false;
		thisGroupMaxOK = false;
		if(maxChecked == -1) thisGroupMaxOK = true;
		numChecked = 0;
		for(j = 0; j < numBoxes; j++)
		{
			if(checkbox[j].checked)
			{
				thisGroupMinOK = true;
				if(thisGroupMaxOK) break;
				numChecked++;
			}
		}
		if(maxChecked != -1 && numChecked <= maxChecked) thisGroupMaxOK = true;
		displayName = args[++i];
		if(!thisGroupMinOK)
		{
			errors += "- at least " + minChecked + " of the " + displayName + " checkboxes must be checked.\n";
		}
		if(!thisGroupMaxOK)
		{
			errors += "- no more than " + maxChecked + " of the " + displayName + " checkboxes must be checked.\n";
		}
	}
	if(errors != "") alert("The following error(s) occurred:\n" + errors);
	document.CG_returnValue = (errors == "");
}

function IsNumeric(strString)
{
   	var strValidChars = "0123456789.-";
   	var strChar;
   	var blnResult = true;

   	if (strString.length == 0) return false;

   	//  test strString consists of valid characters listed above
   	for (i = 0; i < strString.length && blnResult == true; i++)
    {
      	strChar = strString.charAt(i);
      	if (strValidChars.indexOf(strChar) == -1)
        {
         	blnResult = false;
        }
     }
   	return blnResult;
}

function IsInteger(strString)
{
   	var strValidChars = "0123456789";
   	var strChar;
   	var blnResult = true;

   	if (strString.length == 0) return false;

   	//  test strString consists of valid characters listed above
   	for (i = 0; i < strString.length && blnResult == true; i++)
    {
      	strChar = strString.charAt(i);
      	if (strValidChars.indexOf(strChar) == -1)
        {
         	blnResult = false;
        }
     }
   	return blnResult;
}


