/* This function checks to see that the var being passed in is a number */

function checkNum(strVar)
{
	var numOK = true;

	if(strVar != "")
	{
		var strNum = "0123456789";
		for(i = 0; i < strVar.length; i++)
		{
			if(strNum.indexOf(strVar.charAt(i)) < 0)
			{
				numOK = false;
				break;
			}
		}
	}
	return numOK;
}

/* This function checks to see that the var being passed in is a number, and falls within the product rating range 1 - 5*/

function checkRating(strVar)
{
	var numOK = true;

	if(strVar != "")
	{
		var strNum = "12345";
		for(i = 0; i < strVar.length; i++)
		{
			if(strNum.indexOf(strVar.charAt(i)) < 0)
			{
				numOK = false;
				break;
			}
		}
	}
	return numOK;
}

/* This function checks to see that the values of the rating from 1-5 are not duplicated.  In other words, a user cannot assign the same ranking value to more than one item.*/
function checkRatingSequence(arrValues)
{
var numOK = true;
var rating = "";
var strNum = "12345";
	for(i = 0; i < arrValues.length; i++)
	{
		rating = arrValues[i];
		if(strNum.indexOf(rating.charAt(0)) < 0)
		{
			numOK = false;
			break;
		}
		else
		{
			strNum = strNum.replace(rating, "");
		}
	}
	return numOK;
}

/* This function checks to see that the variable being passed in is comprise of only numbers and a dash */

function checkZip(strVar)
{
	var zipOK = true;

	if(strVar != "") {				
		//allow 5 digit zip or 5 digit zip +4 (separated by a space or hyphen or nothing)
		var regex = /(^\d{5}$)|(^\d{5}[ -]?\d{4}$)/;		
		if (!regex.test(strVar)) {
			zipOK = false;
		}
	}
	return zipOK;			
}

/* This function checks to see that the users address is not a PO Box.  Some promos do not accept PO Boxes */

function checkPOBox(strVar)
{
	var addressOK = 0;
	var i = 0;
	var poIndex;
	strVar = strVar.toUpperCase();
	// 20 Thompson Road
	arrayPOBox = new Array(/P\.O\./,"PO BOX","POSTOFFICE BOX","POST OFFICE BOX","POSTAL BOX");
	for(i = 0; i < arrayPOBox.length; i ++){
		
		poIndex = strVar.search(arrayPOBox[i]);
		if(poIndex > -1){
			//alert("match = " + arrayPOBox[i] + "\n index = " + poIndex);
			return false;
			break;
			//addressOK = 1;
		}
	}

	return true;
}

/* This function checks to see that the variable being passed in is comprise of only numbers and a dash */

function checkPercent(strVar)
{
	var zipOK = true;

	if(strVar != "")
	{
		var strNum = "0123456789%";
		for(i = 0; i < strVar.length; i++)
		{
			if(strNum.indexOf(strVar.charAt(i)) < 0)
			{
				zipOK = false;
				break;
			}
		}
	}
	return zipOK;
}

/* This function checks to see that the variable being passed is comprised of only numbers and letters for canadian postal validation */
function checkPostal(strVar)
{
	var postalOK = true;
	//Format is LNL NLN (L=Letter, N=Number and there is a space between them)
	var regex = /^[a-z]{1}\d{1}[a-z]{1}\s{1}\d{1}[a-z]{1}\d{1}/i;
	
	if (!regex.test(strVar)) {
		postalOK = false;
	}

	/* previous code
	if(strVar != "")
	{
		var strInvalid = "!@#$%^&*()[];:\,._+{}";
		for(i = 0; i < strVar.length; i++)
		{
			if(strInvalid.indexOf(strVar.charAt(i)) > 0)
			{
				postalOK = false;
				break;
			}
		}
	}*/
	return postalOK;
}

/* This function takes in an email string and checks it for format */
function checkEmail(strVar)
{

//	var emailReg = /^.+\@.+\..+$/;	OLD
//	var emailReg = /^(\w){1}(\w|-|\.)+\@(\w|-){2,}\.(\w|-)+$/i; old 1/24/2005 
	var emailReg = /^(\w){1}[^\*\$\^\&\!\#\%\(\)\+\`\~\,\/\?\:\;\\\"\|\[\]\{\}\@]+\@(\w){1}(\w|-|\.)+\.[a-z]{2,4}$/i;
	var emOK = true;
	var email_addys = strVar.split(';');
	
	for (i=0;i>email_addys.length;i++){
		if(strVar != ""){
			if(!emailReg.test(strVar)){
				emOK = false;
			}
		}
	}
	
	return emOK;
	
}

function checkCharacter(strStringToCheck)
{
	// this function will check the variable strStringToCheck and replace any single
	// quotes, or double quotes with either a \' or a \"
	var strNewString = strStringToCheck;
	for(i = 0; i < strStringToCheck.length; i++){
		if (strStringToCheck.charAt(i) =="'"){
			strNewString = strStringToCheck.replace("'","\'");
		}

	}
	return strNewString;
}

/* This function is required for all CLM campaigns */
function setContactedDate(){
	var today = new Date();
	var month = today.getMonth() + 1;
	var date = today.getDate();
	if (month < 10) { month = '0'+month; }
	if (date < 10) { date = '0'+date; }
	var contactedDate = month + "/" + date + "/" + today.getFullYear();
	return contactedDate;
}

/* This function will strip the dash from a phone number */
function stripPhoneString(strPhone)
{
	var strNewPhone = strPhone;

	if(strPhone.search("-") != -1){
		strNewPhone = strPhone.replace("-","");
	}else if(strPhone.search(" ") != -1){
		strNewPhone = strPhone.replace(" ","");
	}else if(strPhone.search(".") != -1){
		strNewPhone = strPhone.replace(".","");
	}
	return strNewPhone;
}

/* This function will assign rsc codes to products */
function assignRSC(strProduct,strQuantity)
{
	var rscCodes = new Array(24);
	var rscString = "";

	rscCodes["BUSINESS_PC"] = "A9W";
	rscCodes["SERVER"] = "A9S";
	rscCodes["NOTEBOOK_PC"] = "A9R";
	rscCodes["HANDHELD_PC"] = "A9X";
	rscCodes["STORAGE"] = "A9V";
	rscCodes["NETWORK"] = "A9K";
	rscCodes["BW_LASERJET"] = "A9M";
	rscCodes["BW_PRINTER"] = "A9M";
	rscCodes["BW_PRINTERS"] = "A9M";
	rscCodes["COLOR_PRINTER"] = "A9H";
	rscCodes["COLOR_PRINTERS"] = "A9H";
	rscCodes["IMAGING"] = "A9U";
	rscCodes["SCANNERS"] = "A9U";
	rscCodes["DESKJET"] = "A9H";
	rscCodes["INKJET"] = "A9L";
	rscCodes["DESIGNJET"] = "A9G";
	rscCodes["OFFICEJET"] = "A9Q";
	rscCodes["PHOTOSMART"] = "A9T";
	rscCodes["DEPT_MULTI_PRINTER_COPIER"] = "A9P";
	rscCodes["WORK_MULTI_PRINTER_COPIER"] = "A9P";
	rscCodes["PERS_MULTI_PRINTER"] = "A9P";
	rscCodes["LARGE_FORMAT_COLOR_PRINTER"] = "A9G";
	rscCodes["DIGITAL_PROJ"] = "A9J";
	rscCodes["LASERJET"] = "A9N";
	rscCodes["PROJECTOR_MOBILE"] = "GH3";
	rscCodes["PROJECTOR_ONSITE"] = "GH4";
	rscCodes["PROJECTOR_AUDITORIUM"] = "GH5";

	if(strQuantity!="0")
	{
		rscString = rscCodes[strProduct] + ":" + strQuantity;
	}
	else
	{
		rscString = strQuantity;
	}

	return rscString;
}

/* on forms where we have checkboxes, hp would like us to assign a value of NO to any checkboxes that are not selected.  this function will iterate through all checkboxes that are not selected and mark them as NO*/
function loopCheckboxes(strForm) {
	var no = "NO";
	var oForm = document.forms[strForm];
	var bIsChecked = false;
	var type;
	var value;
	for(var i=0; i<oForm.elements.length; i++) {
		type = oForm.elements[i].type;
		if (type == 'checkbox') {
			// value = oForm.elements[i].checked;
			// alert ("type = " + type + "(" + i + ")" + " value = " + value);
			if (oForm.elements[i].checked && oForm.elements[i].value!="NO") {
				oForm.elements[i].value="YES";
			} else {
				oForm.elements[i].checked = true;
				oForm.elements[i].value="NO";
			}
		}
	}
}