//Author:       Steve Eidemiller
//Date:         06/28/2007
//Description:  Generic JavaScript functions common to the entire site

/*********************************************************************************************************************
Revision History:
00/00/2007 - Who - What
*********************************************************************************************************************/

//String trim() function
function strtrim(str)
{
	return str.replace(/^\s+/, "").replace(/\s+$/, "");
}

//Validate a text field to make sure it has some data
function niValidateTextField(elementID, fieldName)
{
	//If (the text field is basically empty)...
	var txt = document.getElementById(elementID);
	if (strtrim(txt.value).length == 0)
	{
		alert("Please enter a value for '" + fieldName + "'.");
		txt.focus();
		return false; //Validation failure
	}

	//Validation success
	return true;
}

//Validate an email field
function niValidateEmailField(elementID, fieldName)
{
	//
	var txt = document.getElementById(elementID);
	var email = txt.value.match(/[\w-_\.]+@[\w-\.]*\.[a-zA-Z]{2,}/);
	if (email == null || email.index != 0)
	{
		alert("Please enter a valid email address for '" + fieldName + "'.");
		txt.focus();
		return false; //Validation failure
	}

	//Validation success
	return true;
}

//Validate a 10-digit phone number field
function niValidatePhoneField(elementID, fieldName)
{
	//Extract all digits such that "(651) 555-1212" becomes "6515551212", effectively stripping off any markup or formatting.
	var txt = document.getElementById(elementID);
	var phone = txt.value.replace(/[^0-9]/gi, ""); //Replace all non-digit characters with an empty string, including any whitespace

	//Ignore leading 1's for 11-digit numbers
	if (phone.length == 11 && phone.charAt(0) == '1')
	{
		return true; //These phone numbers are OK
	}

	//Validate the correct number of digits
	if (phone.length != 10)
	{
		alert("Please enter a 10-digit phone number for '" + fieldName + "'.");
		txt.focus();
		return false; //Validation failure
	}

	//Validation success
	return true;
}

//Validate a 5-digit zip code field
function niValidateZipCodeField(elementID, fieldName)
{
	//Extract all digits
	var txt = document.getElementById(elementID);
	var phone = txt.value.replace(/[^0-9]/gi, ""); //Replace all non-digit characters with an empty string, including any whitespace

	//Validate the correct number of digits, and ensure that the the five digits are the only text in the field
	if (phone != txt.value || phone.length != 5)
	{
		alert("Please enter a 5-digit zip code for '" + fieldName + "'.");
		txt.focus();
		return false; //Validation failure
	}

	//Validation success
	return true;
}

//Validate a 4-digit year field
function niValidateYearField(elementID, fieldName)
{
	//Extract all digits
	var txt = document.getElementById(elementID);
	var phone = txt.value.replace(/[^0-9]/gi, ""); //Replace all non-digit characters with an empty string, including any whitespace

	//Validate the correct number of digits, and ensure that the the four digits are the only text in the field
	if (phone != txt.value || phone.length != 4)
	{
		alert("Please enter a 4-digit year for '" + fieldName + "'.");
		txt.focus();
		return false; //Validation failure
	}

	//Validation success
	return true;
}

//Display a help message for the SMS/WAP format selection drop-downs
function niMobileCouponFormatHelp(lstSelectTag)
{
	//If ('Help' was selected)...
	if (lstSelectTag.value == -1)
	{
		//Display the help message in an alert() prompt
		alert("SMS Format\n" +
		      "========\n" +
		      "Selecting this option will cause a simple text message\n" +
		      "containing a coupon to be sent to your phone number.\n" +
		      "\n" +
		      "\n" +
		      "WAP Format\n" +
		      "========\n" +
		      "This option will also send a text message, but it will\n" +
		      "contain a link that you can click-on or select that will\n" +
		      "bring up the coupon in the web browser on your phone.\n" +
		      "You will need to have web browsing enabled on your\n" +
		      "phone as well as a data plan in place with your wireless\n" +
		      "carrier.\n" +
		      "\n");

		//Select 'SMS' again so that 'Help' isn't selected
		lstSelectTag.value = 0;
	}
}

