function MM_preloadImages() {
}

function goBox(theItem) {
	destURL = trim(theItem.options[theItem.selectedIndex].value);
	theItem.options[0].selected=true;
	if (destURL!="") {
		top.location = destURL;
	}
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
}

/*** Used in sort functionality to move items up and down ***/
function moveOption(direction,field) {
  myField = eval(field);
	mySelectedVal1 = myField.selectedIndex;
  if (mySelectedVal1>-1) {  
  	if(direction == 'UP' && myField.selectedIndex > 0) {
  		mySelectedVal2 = mySelectedVal1 - 1;
  	}
  	else if(direction == 'DOWN' && myField.selectedIndex < myField.options.length-1) {
  		mySelectedVal2 = mySelectedVal1 + 1;
  	}
  	else return;
  	myTextVal1 = myField[mySelectedVal1].text;
  	myOptionVal1 = myField.options[mySelectedVal1].value;
  	myField.options[mySelectedVal1].value = myField.options[mySelectedVal2].value
  	myField.options[mySelectedVal2].value = myOptionVal1;
  	myField[mySelectedVal1].text = myField[mySelectedVal2].text;
  	myField[mySelectedVal2].text = myTextVal1;
  	myField.selectedIndex = mySelectedVal2;
  } else {
    alert("Please select an item before clicking the up/down buttons.");
    return false;
  }
}
/*** selects all items in a "select multiple" prior to submitting the form ***/
function saveOrder(theForm) {
	for(x=0; x<theForm.idField.options.length; x++) {
		theForm.idField.options[x].selected=true;
	}
}

function popUpWindow(url,iWidth,iHeight,scrolling) {
	WinName  = "popUpWindow";
  width    = iWidth;
  height   = iHeight;
  posX     = (screen.availWidth/2)-(width/2);
  posY     = (screen.availHeight/2)-(height/2);
  NewWinId = null;
  NewWinId = window.open(url,WinName,'width='+width+',height='+height+',resizable=yes,scrollbars='+scrolling+',menubar=no,toolbar=no,location=no,directories=no,status=no,top='+posY+',left='+posX);
  NewWinId.focus();
}



function setOtherInfo() {
	theField = document.forms[0].other_information;
	for (i=0; i<theField.length; i++) {
		for (j=0; j<=otherInfo.length; j++) {
			if ( theField[i].value==otherInfo[j] ) {
				theField[i].checked=true;
			}
		}
	}
}

function validate_registration(theForm) {
	//trim
	theForm.company_name.value     = trim(theForm.company_name.value);
	theForm.type_of_business.value = trim(theForm.type_of_business.value);
	theForm.phone_no.value         = trim(theForm.phone_no.value);
	theForm.fax_no.value           = trim(theForm.fax_no.value);
	theForm.web_site_address.value = trim(theForm.web_site_address.value);
	theForm.license_no.value       = trim(theForm.license_no.value);
	theForm.street_address1.value  = trim(theForm.street_address1.value);
	theForm.street_address2.value  = trim(theForm.street_address2.value);
	theForm.street_address3.value  = trim(theForm.street_address3.value);
	theForm.city.value             = trim(theForm.city.value);
	theForm.postal_code.value      = trim(theForm.postal_code.value);
	theForm.username.value         = trim(theForm.username.value);
	theForm.first_name.value       = trim(theForm.first_name.value);
	theForm.last_name.value        = trim(theForm.last_name.value);
	theForm.occupation.value       = trim(theForm.occupation.value);
	theForm.email_address.value    = trim(theForm.email_address.value);
	theForm.mobile_phone_no.value  = trim(theForm.mobile_phone_no.value);	
	theForm.veriCode.value         = trim(theForm.veriCode.value);
	theForm.services_offered.value = trim(theForm.services_offered.value);
	
	if ( theForm.company_name.value=="" ) {
		alert("Please enter your company name before proceeding.");
		theForm.company_name.focus();
		return false;
	}
	
	if ( theForm.type_of_business.value=="" ) {
		alert("Please enter the type of business before proceeding.");
		theForm.type_of_business.focus();
		return false;
	}
	
	if ( theForm.phone_no.value=="" ) {
		alert("Please enter your phone number before proceeding.");
		theForm.phone_no.focus();
		return false;
	} else {
		//check proper formatting
		phoneRE = /^\d{3}\-\d{3}\-\d{4}$/; //123-456-7890
		if ( !( phoneRE.test(theForm.phone_no.value) ) ) {
			alert("The phone number you entered is not properly formatted.\nIt needs to be in the \"123-456-7890\" format.");
			theForm.phone_no.focus();
			return false;
		}
	}
	
	if ( theForm.fax_no.value!="" ) {
		//check proper formatting
		phoneRE = /^\d{3}\-\d{3}\-\d{4}$/; //123-456-7890
		if ( !( phoneRE.test(theForm.fax_no.value) ) ) {
			alert("The fax number you entered is not properly formatted.\nIt needs to be in the \"123-456-7890\" format.");
			theForm.fax_no.focus();
			return false;
		}
	}
	
	if ( theForm.web_site_address.value!="" && theForm.web_site_address.value!="http://" ) {
	} else {
		//validate format
	}
	
	if ( theForm.street_address1.value=="" ) {
		alert("Please enter your street address before proceeding.");
		theForm.street_address1.focus();
		return false;
	}
	
	if ( theForm.city.value=="" ) {
		alert("Please enter your city before proceeding.");
		theForm.city.focus();
		return false;
	}
	
	if ( theForm.state.selectedIndex==0 ) {
		alert("Please select your state before proceeding.");
		theForm.state.focus();
		return false;
	}
	
	if ( theForm.country.selectedIndex==0 ) {
		alert("Please select your country before proceeding.");
		theForm.country.focus();
		return false;
	}
	
	if (theForm.postal_code.value=="") {
		alert("Please enter your postal code.");
		theForm.postal_code.focus();
		return false;
	} else {
		//check proper formatting
		if (theForm.country.value=="USA") {
			usZipRE = /(^\d{5}$)|(^\d{5}-\d{4}$)/; //08810[-2402]
			if ( !( usZipRE.test(theForm.postal_code.value) ) ) {
				alert("The US zip code you entered is not properly formatted.\nIt needs to be in the \"12345[-6789]\" format.");
				theForm.postal_code.focus();
				return false;
			}
		} else if (theForm.country.value=="CANADA") {
			canadaPostalCodeRE = /^\D{1}\d{1}\D{1}\ ?\d{1}\D{1}\d{1}$/; //L1H 8N9 or L1H8N9 
			if ( !( canadaPostalCodeRE.test(theForm.postal_code.value) ) ) {
				alert("The Canadian postal code you entered is not properly formatted.\nIt needs to be in the \"A5A 5A5\" format");
				theForm.postal_code.focus();
				return false;
			}
		}
	}
	
	if ( theForm.username.value=="" ) {
		alert("Please enter your desired username before proceeding.");
		theForm.username.focus();
		return false;
	}
	
	if ( theForm.first_name.value=="" ) {
		alert("Please enter your first name before proceeding.");
		theForm.first_name.focus();
		return false;
	}
	
	if ( theForm.last_name.value=="" ) {
		alert("Please enter your last name before proceeding.");
		theForm.last_name.focus();
		return false;
	}
	
	if ( theForm.occupation.value=="" ) {
		alert("Please enter your occupation before proceeding.");
		theForm.occupation.focus();
		return false;
	}
	
	if ( theForm.email_address.value=="" ) {
		alert("Please enter your e-mail address before proceeding.");
		theForm.email_address.focus();
		return false;
	} else {
		//validate format
		emailRE = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		if ( !( emailRE.test(theForm.email_address.value) ) ) {
			alert("The e-mail address you entered is not properly formatted.\nIt needs to be in the \"jsmith@aol.com\" format.");
			theForm.email_address.focus();
			return false;
		}
	}
	
	if ( theForm.mobile_phone_no.value!="" ) {
		//check proper formatting
		phoneRE = /^\d{3}\-\d{3}\-\d{4}$/; //123-456-7890
		if ( !( phoneRE.test(theForm.mobile_phone_no.value) ) ) {
			alert("The mobile phone number you entered is not properly formatted.\nIt needs to be in the \"123-456-7890\" format.");
			theForm.mobile_phone_no.focus();
			return false;
		}
	}
	
	if ( theForm.veriCode.value=="" ) {
		alert("Please enter the verification code before proceeding.");
		theForm.veriCode.focus();
		return false;
	}
	
	if ( theForm.services_offered.value.length>2000 ) {
		alert("Please limit your \"Services Offered\" description to 2,000 characters or less.");
		theForm.services_offered.focus();
		return false;
	}
	
}

function validate_signIn(theForm) {
	//trim fields
	theForm.username.value = trim(theForm.username.value);
	theForm.password.value = trim(theForm.password.value);
	//validate
	if ( theForm.username.value=="" ) {
		alert("Please enter your username before proceeding.");
		theForm.username.focus();
		return false;
	}
	if ( theForm.password.value=="" ) {
		alert("Please enter your password before proceeding.");
		theForm.password.focus();
		return false;
	}
}

function validate_changePassword(theForm) {
	theForm.password1.value = trim(theForm.password1.value);
	theForm.password2.value = trim(theForm.password2.value);
	if ( theForm.password1.value=="" ) {
		alert("Please enter your new password before proceeding.");
		theForm.password1.focus();
		return false;
	}
	if ( theForm.password2.value=="" ) {
		alert("Please re-enter your new password before proceeding.");
		theForm.password2.focus();
		return false;
	}
	if ( theForm.password1.value!=theForm.password2.value ) {
		alert("The passwords you entered do not match, please re-enter them before proceeding.");
		theForm.password1.value="";
		theForm.password2.value="";
		theForm.password1.focus();
		return false;
	}
}

function validate_productCategories(theForm) {
	theForm.category_name.value=trim(theForm.category_name.value);
	theForm.category_desc.value=trim(theForm.category_desc.value);
	if ( theForm.category_name.value=="" ) {
		alert("Please enter the category name before proceeding.");
		theForm.category_name.focus();
		return false;
	}
}

function loadCategoryEdit(rowId,numRows) {
	theForm = parent.document.forms[0];
	removeHighlight(numRows);
	//highlight selected row
	divName = "pos" + rowId;
	eval(divName).className = "highlightRowEdit";
	//populate form fields
	theForm.action.value        = "updateProductCategory";
	theForm.id.value            = category[rowId][0];
	theForm.category_name.value = category[rowId][1];
	theForm.category_desc.value = category[rowId][2];
	theForm.active_flag.checked = false;
	if ( category[rowId][3] ) {
		theForm.active_flag.checked = true;
	}
	theForm.submitBtn.value="Save Changes >";
	theForm.category_name.focus();
}
function removeHighlight(numRows) {
	//remove highlight from all rows
	for (i=1; i<=numRows; i++) {
		divName = "pos" + i;
		eval(divName).className = "";
	}
}

function validate_genericProduct(theForm) {
	theForm.product_name.value=trim(theForm.product_name.value);
	theForm.product_desc.value=trim(theForm.product_desc.value);
	if ( theForm.category_id.selectedIndex==0 ) {
		alert("Please select the category to which this generic product will belong.");
		theForm.category_id.focus();
		return false;
	}
	if ( theForm.product_name.value=="" ) {
		alert("Please enter the product name before proceeding.");
		theForm.product_name.focus();
		return false;
	}
}

function loadProductEdit(rowId,numRows) {
	theForm = parent.document.forms[0];
	removeHighlight(numRows);
	//highlight selected row
	divName = "pos" + rowId;
	eval(divName).className = "highlightRowEdit";
	//populate form fields
	theForm.action.value        = "updateGenericProduct";
	theForm.id.value            = product[rowId][1];
	for (j=0; j<theForm.category_id.length; j++) {
		if (theForm.category_id.options[j].value==product[rowId][0]) {
			theForm.category_id.options[j].selected=true;
		}
	}
	theForm.product_name.value = product[rowId][2];
	theForm.product_desc.value = product[rowId][3];
	theForm.active_flag.checked = false;
	if ( product[rowId][4] ) {
		theForm.active_flag.checked = true;
	}
	theForm.visible_flag.checked = false;
	if ( product[rowId][5] ) {
		theForm.visible_flag.checked = true;
	}
	theForm.customizable_flag.checked = false;
	if ( product[rowId][6] ) {
		theForm.customizable_flag.checked = true;
	}
	theForm.submitBtn.value="Save Changes >";
	theForm.product_name.focus();
}
function removeHighlight(numRows) {
	//remove highlight from all rows
	for (i=1; i<=numRows; i++) {
		divName = "pos" + i;
		eval(divName).className = "";
	}
}

function validate_imageUpload(theForm) {
	theForm.caption.value = trim(theForm.caption.value);
	theForm.file_path.value = trim(theForm.file_path.value);
	if (theForm.image_type_id.selectedIndex==0) {
		alert("You must select the image type you are uploading.");
		theForm.image_type_id.focus();
		return false;
	}
	if (theForm.file_path.value=="") {
		alert("You must select the location of the image you are uploading.");
		theForm.file_path.focus();
		return false;
	}
	if (theForm.caption.value=="") {
		alert("You must enter a description for the image you are uploading.");
		theForm.caption.focus();
		return false;
	}
}

function validate_newQuestion(theForm) {
	theForm.question_text.value      = trim(theForm.question_text.value);
	theForm.question_hint_text.value = trim(theForm.question_hint_text.value);
	if ( theForm.question_type_id.selectedIndex==0 ) {
		alert("You must select the question type before proceeding.");
		theForm.question_type_id.focus();
		return false;
	}
	if ( theForm.question_text.value=="" ) {
		alert("You must enter the question before proceeding.");
		theForm.question_text.focus();
		return false;
	}
	if ( theForm.question_hint_text.value=="" ) {
		alert("You must enter the hint before proceeding.");
		theForm.question_hint_text.focus();
		return false;
	}
}

function loadQuestionEdit(rowId,numRows) {
	theForm = parent.document.forms[0];
	removeHighlight(numRows);
	//highlight selected row
	divName = "pos" + rowId;
	eval(divName).className = "highlightRowEdit";
	//populate form fields
	theForm.action.value = "updateQuestion";
	theForm.id.value     = category[rowId][0];
	//set question type
	for (i=0; i<theForm.question_type_id.length; i++) {
		if (theForm.question_type_id.options[i].value==category[rowId][1]) {
			theForm.question_type_id.options[i].selected = true;
		}
	}
	theForm.question_text.value      = category[rowId][2];
	theForm.question_hint_text.value = category[rowId][3];
	theForm.active_flag.checked      = false;
	if ( category[rowId][4] ) {
		theForm.active_flag.checked = true;
	}
	theForm.submitBtn.value="Save Changes >";
	theForm.question_text.focus();
}

function validate_newAnswer(theForm) {
	theForm.answer_text.value = trim(theForm.answer_text.value);
	if (theForm.answer_text.value=="") {
		alert("You must enter the text of the answer before proceeding.");
		theForm.answer_text.focus();
		return false;
	}
}

function validate_quiz(theForm) {
	for (q=0; q<theForm.qi.length; q++) {
		
		_questionInfo = theForm.qi[q].value.split("|");
		_questionId   = _questionInfo[0];
		_questionType = _questionInfo[1];
		_questionNo   = q+1;
		_fieldName    = "qid_" + _questionId;
		_formField    = theForm[_fieldName];
		
		switch (_questionType) {
			
			case "1":
				//validates multiple dropdowns
				valid = 0;
				for (i=0; i<_formField.length; i++) {
					if (_formField.options[i].selected) {
						valid++;
					}
				}				
				if (!valid) {
					alert("You must select at least one answer for question # " + _questionNo +valid+ ".\n\n" + qt[q]);
					_formField.focus();
					return false;
				}
				break;
				
			case "2":
				//validates checkboxes
				valid = 0;
				if (_formField.length) {
					for (i=0; i<_formField.length; i++) {
						if (_formField[i].checked) {
							valid++;
						}
					}
				} else {
					if (_formField.checked) {
						valid++;
					}
				}				
				if (!valid) {
					alert("You must select at least one answer for question # " + _questionNo + ".\n\n" + qt[q]);
					return false;
				}
				break;
				
			case "3":
				//validates single dropdowns
				if (_formField.selectedIndex==0) {
					alert("You must select an answer for question # " + _questionNo + ".\n\n" + qt[q]);
					_formField.focus();
					return false;
				}
				break;
				
			case "4":
				//validates radio buttons
				valid = 0;
				if (_formField.length) {
					for (i=0; i<_formField.length; i++) {
						if (_formField[i].checked) {
							valid++;
						}
					}
				} else {
					if (_formField.checked) {
						valid++;
					}
				}
				
				if (!valid) {
					alert("You must select an answer for question # " + _questionNo + ".\n\n" + qt[q]);
					return false;
				}
				break;
				
		}
	}
	//return false;
}

function validate_newProduct(theForm) {
	theForm.item_number.value        = trim(theForm.item_number.value);
	theForm.upc_code.value           = trim(theForm.upc_code.value);
	theForm.product_color.value      = trim(theForm.product_color.value);
	theForm.packages_per_case.value  = trim(theForm.packages_per_case.value);
	theForm.case_weight_pounds.value = trim(theForm.case_weight_pounds.value);
	theForm.carton_size.value        = trim(theForm.carton_size.value);
	theForm.cube.value               = trim(theForm.cube.value);
	theForm.list_price.value         = trim(theForm.list_price.value);
	theForm.inventory_count.value    = trim(theForm.inventory_count.value);
	if (theForm.item_number.value=="") {
		alert("You must enter the item number before proceeding.");
		theForm.item_number.focus();
		return false;
	} else {
		itemNumberRE = /^\D{2}\.\d{6}\-\d{2}$/; //validate format AB.012345-67
		if ( !( itemNumberRE.test(theForm.item_number.value) ) ) {
			alert("The item number you entered is not formatted properly.\nA valid item number uses the format: AB.012345-67");
			theForm.item_number.focus();
			return false;
		}
	}
	if (theForm.package_size_id.selectedIndex==0) {
		alert("You must select the package size before proceeding.");
		theForm.package_size_id.focus();
		return false;
	}
	if (theForm.upc_code.value=="") {
		alert("You must enter the UPC code before proceeding.");
		theForm.upc_code.focus();
		return false;
	} else {
		upcCodeRE = /^\d{1}\-\d{5}\-\d{5}\-\d{1}$/; //validate format 0-12345-67890-1
		if ( !( upcCodeRE.test(theForm.upc_code.value) ) ) {
			alert("The UPC code you entered is not formatted properly.\nA valid UPC code uses the format: 0-12345-67890-1");
			theForm.upc_code.focus();
			return false;
		}
	}
	//packages_per_case as integer between 1-999
	if (theForm.packages_per_case.value!="") {
		pkgPerCaseRE = /^\d{1,3}$/;
		if ( !( pkgPerCaseRE.test(theForm.packages_per_case.value) ) ) {
			alert("You entered an invalid number of packages per case.\nA valid number of packages would be between 1 and 999.");
			theForm.packages_per_case.focus();
			return false;
		}
	}
	//case_weight_pounds as decimal max 99999.99
	if (theForm.case_weight_pounds.value!="") {
		caseWeightRE = /^\d{1,5}\.\d{2}$/;
		if ( !( caseWeightRE.test(theForm.case_weight_pounds.value) ) ) {
			alert("You entered an invalid case weight.\nA valid case weight would be between 0.01 and 99999.99");
			theForm.case_weight_pounds.focus();
			return false;
		}
	}
	//cube as decimal max 9.99
	if (theForm.cube.value!="") {
		cubeRE = /^\d{1}\.\d{2}$/;
		if ( !( cubeRE.test(theForm.cube.value) ) ) {
			alert("You entered an invalid cube number.\nA valid cube would be between 0.01 and 9.99");
			theForm.cube.focus();
			return false;
		}
	}
	//list_price as decimal max 99999999.99
	if (theForm.list_price.value!="") {
		listPriceRE = /^\d{1,8}\.\d{2}$/;
		if ( !( listPriceRE.test(theForm.list_price.value) ) ) {
			alert("You entered an invalid list price.\nA valid price would be between 0.01 and 99999999.99");
			theForm.list_price.focus();
			return false;
		}
	}
	//inventory_count as integer max 999999999999999999
	if (theForm.inventory_count.value!="") {
		inventoryCountRE = /^\d{1,18}$/;
		if ( !( inventoryCountRE.test(theForm.inventory_count.value) ) ) {
			alert("You entered an invalid inventory total.\nA valid inventory would be between 0 and 999999999999999999");
			theForm.inventory_count.focus();
			return false;
		}
	}
}


function loadSurveyQuestionEdit(rowId,numRows) {
	theForm = parent.document.forms[0];
	removeHighlight(numRows);
	//highlight selected row
	divName = "pos" + rowId;
	eval(divName).className = "highlightRowEdit";
	//populate form fields
	theForm.action.value = "updateSurveyQuestion";
	theForm.id.value     = category[rowId][0];
	//set question type
	for (i=0; i<theForm.question_type_id.length; i++) {
		if (theForm.question_type_id.options[i].value==category[rowId][1]) {
			theForm.question_type_id.options[i].selected = true;
		}
	}
	theForm.question_text.value      = category[rowId][2];
	theForm.active_flag.checked      = false;
	if ( category[rowId][3] ) {
		theForm.active_flag.checked = true;
	}
	theForm.submitBtn.value="Save Changes >";
	theForm.question_text.focus();
}

function validate_newSurveyQuestion(theForm) {
	theForm.question_text.value      = trim(theForm.question_text.value);
	if ( theForm.question_type_id.selectedIndex==0 ) {
		alert("You must select the question type before proceeding.");
		theForm.question_type_id.focus();
		return false;
	}
	if ( theForm.question_text.value=="" ) {
		alert("You must enter the question before proceeding.");
		theForm.question_text.focus();
		return false;
	}
}

function loadFileContentsEdit(rowId,numRows) {
	theForm = parent.document.forms[0];
	removeHighlight(numRows);
	//highlight selected row
	divName = "pos" + rowId;
	eval(divName).className = "highlightRowEdit";
	//populate form fields
	theForm.id.value              = category[rowId][0];
	theForm.file_content.value    = category[rowId][1];
	theForm.file_content.disabled = false;
	theForm.resetBtn.disabled     = false;
	theForm.submitBtn.disabled    = false;
	theForm.file_content.focus();
}

function validate_fileContents(theForm) {
	theForm.file_content.value = trim(theForm.file_content.value);
	if (theForm.file_content.value=="") {
		alert("\"" + theForm.id.value + "\" cannot be empty.");
		theForm.file_content.focus();
		return false;
	}
}

function validateNewFinalProduct(theForm) {
	
	theForm.item_number.value              = trim(theForm.item_number.value);
	theForm.upc_code.value                 = trim(theForm.upc_code.value);
	theForm.tws_cost.value                 = trim(theForm.tws_cost.value);
	theForm.list_price.value               = trim(theForm.list_price.value);
	theForm.list_price_points.value        = trim(theForm.list_price_points.value);
	theForm.inventory_count.value          = trim(theForm.inventory_count.value);
	theForm.welcome_package_quantity.value = trim(theForm.welcome_package_quantity.value);
	
	if ( theForm.item_number.value=="" ) {		
		alert("You must enter this product\'s Item Number before proceeding");
		theForm.item_number.focus();
		return false;		
	}
	
	if (theForm.product_type.selectedIndex==0) {
	
		alert("You must select a product type before proceeding");
		theForm.product_type.focus();
		return false;
		
	} else {
	
		selType = theForm.product_type.options[theForm.product_type.selectedIndex].value;
		if ( selType=="P" ) {
			//cost, price and points required
			if ( theForm.tws_cost.value=="" ) {
				alert("The cost is required for this type of product.\n\nThis value will never be displayed and will only be used by Taxware during checkout.");
				theForm.tws_cost.focus();
				return false;
			}
			if ( theForm.list_price.value=="" ) {
				alert("The list price is required for this type of product.\n\nThis value will always be displayed in the shopping section.");
				theForm.list_price.focus();
				return false;
			}
			if ( theForm.list_price_points.value=="" ) {
				alert("Please enter the Points Required.\nIt is the number of points required to purchase this item.");
				theForm.list_price_points.focus();
				return false;
			}
			//reset upc code
			theForm.upc_code.value = "";
		} else if ( selType=="R" ) {
			//upc code and points required
			if ( theForm.upc_code.value=="" ) {
				alert("The UPC code is required for this type of product");
				theForm.upc_code.focus();
				return false;
			}
			if ( theForm.list_price_points.value=="" ) {
				alert("Please enter the Points Required.\nIt is the number of points to be awarded.");
				theForm.list_price_points.focus();
				return false;
			}
			//reset list price
			theForm.list_price.value = "";
		}
		
	}
	
	if ( theForm.inventory_count.value=="" ) {	
		alert("You must enter this product\'s Inventory before proceeding.\n\nYou can enter -1 to indicate an unknown total.");
		theForm.inventory_count.focus();
		return false;		
	}
	
	if ( theForm.welcome_package_flag.checked ) {
		if ( theForm.welcome_package_quantity.value=="" ) {
			alert("When a product is selected as part of the welcome package, you must enter a quantity.");
			theForm.welcome_package_quantity.focus();
			return false;
		} else {
			quantityRE = /^\d{1,3}$/;
			if ( !( quantityRE.test(theForm.welcome_package_quantity.value) ) ) {
				alert("You entered an invalid welcome package quantity.\nA valid quantity would be between 0 and 999");
				theForm.welcome_package_quantity.focus();
				return false;
			}
		}
	} else {
		theForm.welcome_package_quantity.value = "0";
	}
	
	if ( theForm.welcome_package_quantity.value=="" || theForm.welcome_package_quantity.value=="0" ) {
		theForm.welcome_package_flag.checked   = false;
		theForm.welcome_package_quantity.value = "0";
	}
	
}

function loadFinalProductEdit(rowId,numRows) {
	theForm = document.forms[0];
	removeHighlight(numRows);
	//highlight selected row
	divName = "pos" + rowId;
	eval(divName).className = "highlightRowEdit";
	//populate form fields
	theForm.action.value        = "updateFinalProduct";
	theForm.active_flag.checked = false;
	if ( product[rowId][6] ) {
		theForm.active_flag.checked = true;
	}
	theForm.item_number.value = product[rowId][0];
	for (j=0; j<theForm.product_type.length; j++) {
		if (theForm.product_type.options[j].value==product[rowId][1]) {
			theForm.product_type.options[j].selected=true;
		}
	}
	theForm.upc_code.value          = product[rowId][2];
	theForm.tws_cost.value          = product[rowId][7];
	theForm.list_price.value        = product[rowId][3];
	theForm.list_price_points.value = product[rowId][4];
	theForm.inventory_count.value   = product[rowId][5];
	theForm.welcome_package_flag.checked = false;
	if ( product[rowId][8] ) {
		theForm.welcome_package_flag.checked = true;
	}
	theForm.welcome_package_quantity.value = product[rowId][9];
	theForm.item_number.focus();
}

function loadProductFeatureEdit(rowId,numRows) {
	theForm = document.forms[0];
	removeHighlight(numRows);
	//highlight selected row
	divName = "pos" + rowId;
	eval(divName).className = "highlightRowEdit";
	//populate form fields
	theForm.action.value        = "updateProductFeature";
	theForm.fid.value          = product[rowId][0];
	theForm.feature_name.value = product[rowId][1];
	theForm.feature_desc.value = product[rowId][2];
	theForm.feature_url.value  = product[rowId][3];
	theForm.active_flag.checked = false;
	if ( product[rowId][4] ) {
		theForm.active_flag.checked = true;
	}	
	theForm.feature_name.focus();
}

function validateNewProductFeature(theForm) {
	theForm.feature_name.value = trim(theForm.feature_name.value);
	theForm.feature_desc.value = trim(theForm.feature_desc.value);
	theForm.feature_url.value = trim(theForm.feature_url.value);
	
	if ( theForm.feature_name.value=="" ) {
		alert("You must enter the feature name before proceeding");
		theForm.feature_name.focus();
		return false;
	}
	if ( theForm.feature_desc.value=="" ) {
		alert("You must enter the feature description before proceeding");
		theForm.feature_desc.focus();
		return false;
	}
}

function validate_findContractor(theForm) {
	theForm.postal_code.value = trim(theForm.postal_code.value);
	if ( theForm.postal_code.value=="" ) {
		alert("Please enter the zip code to use in your contractor search.");
		theForm.postal_code.focus();
		return false;
	} else {
		usZipRE = /(^\d{5}$)/; //08810
		if ( !( usZipRE.test(theForm.postal_code.value) ) ) {
			alert("The zip code you entered is not properly formatted.\nIt needs to be in the \"01234\" format.");
			theForm.postal_code.focus();
			return false;
		}
	}
}

function validate_resetPassword(theForm) {
	theForm.email_address.value = trim(theForm.email_address.value);
	if ( theForm.email_address.value=="" ) {
		alert("Please enter your e-mail address before proceeding.");
		theForm.email_address.focus();
		return false;
	} else {
		//validate format
		emailRE = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		if ( !( emailRE.test(theForm.email_address.value) ) ) {
			alert("The e-mail address you entered is not properly formatted.\nIt needs to be in the \"jsmith@aol.com\" format.");
			theForm.email_address.focus();
			return false;
		}
	}
}















/*****************************************************
CHECKOUT FUNCTIONS
*****************************************************/
function toggleShippingAddress(box) {
	theForm = document.forms[1];
	if ( box.checked ) {
		copyBilling(theForm);
	} else {
		clearShipping(theForm);
	}
}
function copyBilling(theForm) {
	for (i=0; i<theForm.billing.length; i++) {
		if (i==4) {
			theForm.shipping[i].options[theForm.billing[i].selectedIndex].selected = true;
		} else {
			theForm.billing[i].value = trim(theForm.billing[i].value);
			theForm.shipping[i].value = theForm.billing[i].value;
		}
		theForm.shipping[i].disabled = true;
	}
}
function clearShipping(theForm) {
	for (i=0; i<theForm.shipping.length; i++) {
		theForm.shipping[i].disabled = false;
		theForm.shipping[i].value = "";
	}
	theForm.shipping_within_city_limits_flag.checked = false;
	theForm.shipping_residential_flag.checked        = false;
}
function validate_billingShippingForm(theForm) {
	//trim
	for (i=0; i<theForm.billing.length; i++) {
		theForm.billing[i].value = trim(theForm.billing[i].value);
	}
	for (i=0; i<theForm.shipping.length; i++) {
		theForm.shipping[i].value = trim(theForm.shipping[i].value);
	}
	//check billing required fields
	if ( theForm.billing[0].value=="" ) {
		alert("You must enter the name of the billing contact before proceeding");
		theForm.billing[0].focus();
		return false;
	}
	if ( theForm.billing[1].value=="" ) {
		alert("You must enter the billing street address line 1 before proceeding");
		theForm.billing[1].focus();
		return false;
	}
	if ( theForm.billing[2].value=="" ) {
		theForm.billing[2].value = "-";
	}
	if ( theForm.billing[3].value=="" ) {
		alert("You must enter the billing city before proceeding");
		theForm.billing[3].focus();
		return false;
	}
	if ( theForm.billing[4].selectedIndex==0 ) {
		alert("You must select the billing state before proceeding");
		theForm.billing[4].focus();
		return false;
	}
	if ( theForm.billing[5].value=="" ) {
		alert("You must enter the billing zip code before proceeding");
		theForm.billing[5].focus();
		return false;
	} else {
		usZipRE = /(^\d{5}$)|(^\d{5}-\d{4}$)/; //08810[-2402]
		if ( !( usZipRE.test(theForm.billing[5].value) ) ) {
			alert("The billing zip code you entered is not properly formatted.\nIt needs to be in the \"12345 or 12345-6789\" format.");
			theForm.billing[5].focus();
			return false;
		}
	}
	if ( theForm.billing[6].value=="" ) {
		alert("You must enter the billing phone no. before proceeding");
		theForm.billing[6].focus();
		return false;
	} else {
		//check proper formatting
		phoneRE = /^\d{3}\-\d{3}\-\d{4}$/; //123-456-7890
		if ( !( phoneRE.test(theForm.billing[6].value) ) ) {
			alert("The billing phone number you entered is not properly formatted.\nIt needs to be in the \"123-456-7890\" format.");
			theForm.billing[6].focus();
			return false;
		}
	}
	
	//check shipping required fields
	if (!theForm.shipping_same_as_billing_flag.checked) {
		//check shipping required fields
		if ( theForm.shipping[0].value=="" ) {
			alert("You must enter the name of the shipping contact before proceeding");
			theForm.shipping[0].focus();
			return false;
		}
		if ( theForm.shipping[1].value=="" ) {
			alert("You must enter the shipping street address line 1 before proceeding");
			theForm.shipping[1].focus();
			return false;
		}
		if ( theForm.shipping[2].value=="" ) {
			theForm.shipping[2].value = "-";
		}
		if ( theForm.shipping[3].value=="" ) {
			alert("You must enter the shipping city before proceeding");
			theForm.shipping[3].focus();
			return false;
		}
		if ( theForm.shipping[4].selectedIndex==0 ) {
			alert("You must select the shipping state before proceeding");
			theForm.shipping[4].focus();
			return false;
		}
		if ( theForm.shipping[5].value=="" ) {
			alert("You must enter the shipping zip code before proceeding");
			theForm.shipping[5].focus();
			return false;
		} else {
			usZipRE = /(^\d{5}$)|(^\d{5}-\d{4}$)/; //08810[-2402]
			if ( !( usZipRE.test(theForm.shipping[5].value) ) ) {
				alert("The shipping zip code you entered is not properly formatted.\nIt needs to be in the \"12345 or 12345-6789\" format.");
				theForm.shipping[5].focus();
				return false;
			}
		}
		if ( theForm.shipping[6].value=="" ) {
			alert("You must enter the shipping phone no. before proceeding");
			theForm.shipping[6].focus();
			return false;
		} else {
			//check proper formatting
			phoneRE = /^\d{3}\-\d{3}\-\d{4}$/; //123-456-7890
			if ( !( phoneRE.test(theForm.shipping[6].value) ) ) {
				alert("The shipping phone number you entered is not properly formatted.\nIt needs to be in the \"123-456-7890\" format.");
				theForm.shipping[6].focus();
				return false;
			}
		}
	}
}

function clearCreditCardInfo() {
	theForm = document.forms[1];
	theForm.cardholdername.value = "";
	theForm.card_number.value    = "";
	theForm.exp_month.value      = "";
	theForm.exp_year.value       = "";
}
function validate_paymentForm(theForm) {	
	if (theForm.grandTotal.value!="0.00") {
		
		//validate only when credit card is the selected method of payment
		if ( theForm.payment_option[0].checked ) {
			//trim
			theForm.cardholdername.value = trim(theForm.cardholdername.value);
			theForm.card_number.value    = trim(theForm.card_number.value);
			//go
			if (theForm.cardholdername.value=="") {
				alert("You must enter the name as it appears on the credit card before proceeding");
				theForm.cardholdername.focus();
				return false;
			}
			if (theForm.card_number.value=="") {
				alert("You must enter the credit card number before proceeding");
				theForm.card_number.focus();
				return false;
			} else {
				//don't allow spaces
				if ( theForm.card_number.value.indexOf(" ") > -1 ) {
					alert("The credit card number cannot contain spaces, please correct this before proceeding");
					theForm.card_number.focus();
					return false;
				}
			}
			if (theForm.exp_month.selectedIndex==0) {
				alert("You must select the card\'s expiration month before proceeding");
				theForm.exp_month.focus();
				return false;
			}
			if (theForm.exp_year.selectedIndex==0) {
				alert("You must select the card\'s expiration year before proceeding");
				theForm.exp_year.focus();
				return false;
			}
		} else {
			clearCreditCardInfo()
		}
		
	}
}


function loadUserEdit(rowId,numRows) {
	theForm = parent.document.forms[0];
	parent.document.getElementById("verified_on").innerHTML = user[rowId][10];
	parent.document.getElementById("created_on").innerHTML = user[rowId][11];
	
	removeHighlight(numRows);
	//highlight selected row
	divName = "pos" + rowId;
	eval(divName).className = "highlightRowEdit";
	//populate form fields
	theForm.action.value = "updateUser";
	theForm.id.value     = user[rowId][8];
	
	//set user_type_id
	for (i=0; i<theForm.user_type_id.length; i++) {
		if (theForm.user_type_id.options[i].value==user[rowId][1]) {
			theForm.user_type_id.options[i].selected = true;
		}
	}
	
	//select company
	theForm.contractor_id.disabled = false;
	for (i=0; i<theForm.contractor_id.length; i++) {
		if (theForm.contractor_id.options[i].value==user[rowId][0]) {
			theForm.contractor_id.options[i].selected = true;
		}
	}
	theForm.contractor_id.title    = "*** Cannot be changed.";
	theForm.contractor_id.disabled = true;
	
	theForm.username.disabled = false;
	theForm.username.value    = user[rowId][7];
	theForm.username.title    = "*** Cannot be changed.";
	theForm.username.disabled = true;
	
	theForm.last_name.value       = user[rowId][3];
	theForm.first_name.value      = user[rowId][2];
	theForm.email_address.value   = user[rowId][4];
	theForm.occupation.value      = user[rowId][5];
	theForm.mobile_phone_no.value = user[rowId][6];
	
	theForm.active_flag.checked = false;
	if ( user[rowId][9] ) {
		theForm.active_flag.checked = true;
	}

	theForm.submitBtn.value="Save Changes >";
	theForm.deleteUserBtn.disabled = false;
	//theForm.deleteCompanyBtn.disabled = false;
	window.status = "Editing " + theForm.first_name.value + " " + theForm.last_name.value + "...";
}

function validate_newUser(theForm) {
	
	theForm.first_name.value      = trim(theForm.first_name.value);
	theForm.last_name.value       = trim(theForm.last_name.value);
	theForm.email_address.value   = trim(theForm.email_address.value);
	theForm.occupation.value      = trim(theForm.occupation.value);
	theForm.mobile_phone_no.value = trim(theForm.mobile_phone_no.value);
	
	if (theForm.user_type_id.selectedIndex==0) {
		alert("You must select a user type before proceeding.");
		theForm.user_type_id.focus();
		return false;
	}
	if (theForm.id.value=="") {
		if (theForm.contractor_id.selectedIndex==0) {
			alert("You must select the company to which this user will belong before proceeding.");
			theForm.contractor_id.focus();
			return false;
		}
		if (theForm.username.value=="") {
			alert("You must enter this user\'s desired username before proceeding.");
			theForm.username.focus();
			return false;
		}
	}
	if (theForm.first_name.value=="") {
		alert("You must enter this user\'s first name before proceeding.");
		theForm.first_name.focus();
		return false;
	}
	if (theForm.last_name.value=="") {
		alert("You must enter this user\'s last name before proceeding.");
		theForm.last_name.focus();
		return false;
	}
	if (theForm.email_address.value=="") {
		alert("You must enter this user\'s email address before proceeding.");
		theForm.email_address.focus();
		return false;
	} else {
		//validate format
		emailRE = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		if ( !( emailRE.test(theForm.email_address.value) ) ) {
			alert("The e-mail address you entered is not properly formatted.\nIt needs to be in the \"jsmith@aol.com\" format.");
			theForm.email_address.focus();
			return false;
		}
	}
	if (theForm.occupation.value=="") {
		alert("You must enter this user\'s occupation before proceeding.");
		theForm.occupation.focus();
		return false;
	}
	
}

function handleDelete(action,theForm) {
	var userId = theForm.id.value;
	var contractorId = theForm.contractor_id.options[theForm.contractor_id.selectedIndex].value;
	var username = theForm.username.value;
	var areYouSure = "Are you sure you wish to delete the ";
	var actionDisplay = (action=="association") ? "company association" : "user account";
	var url = "?action=removeUser&type=" + action + "&id=" + userId + "&contractorId=" + contractorId;
	if ( confirm(areYouSure + actionDisplay + " for user \"" + username + "\"?" ) ) top.location = url;
}














/*****************************************************
MAP FUNCTIONS
*****************************************************/
function loadMap(id) {
	mapImg.src = mapArray[id];
	mapImg.alt = mapArray[id];
	setNext(id);
	setPrevious(id);
}
function setNext(id) {
	if (id++>=mapArray.length-1) {
		next = 0;
	} else {
		next = id++;
	}
}
function setPrevious(id) {
	if (id--<=0) {
		previous = mapArray.length-1;
	} else {
		previous = id--;
	}
}