/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~~~~ NORTHSCAPING INC. ~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~ NetPS JavaScript Library Code ~~~~~~~~~~~~~'
~~~~~~~~~~~~ for the myPlants feature ~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~~~~~~ VERSION 2.1 ~~~~~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'

Release Date:  TBD

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~ Development Comments ~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'

Jan 20/09	- released V2.1
				- restructured layout for ease of programming
				- added robustness to plant quantity features
				- changed operation so that anything other than a number
				  in the PQT field deletes the entire plant from the list

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~ PLANT LIST FUNCTIONS ~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
*/

function mPModifyMPQTY(mPobj, plantID, cookieID) {
// This (poorly-named) function responds to a user's click on the checkbox
// next to a plant in either the Results Page, Plant Data Page or myPlants Page.
// A click means the user either wants to add the plant to their list, or remove
// the plant from their list; their intent is contained in the "checked" status
// of the mPobj (i.e. the DOM checkbox ID) passed to this function. It either adds
// or removes the plant, and then it modifies the mPQty header value accordingly.

// first check to see that cookies are enabled
	var cookieName = cookieID + "=";
	var cookieStartIndex = document.cookie.indexOf(cookieName);
	
	if (cookieStartIndex == -1) {
// cookies not enabled
		alert("You must have cookies enabled in your browser to use the myPlants feature!");
		}
	else {
// cookies are enabled, so retrieve the cookie value as "mPCookie"
		var mPCookie = readCookie(cookieName);
		if (mPCookie) {
// JS is only used to add or remove plantIDs from the cookie based on a user click of the checkbox
// so determine whether we're adding or removing a plantID
			if (mPobj.checked == true) {
// checkbox was selected
// find the end of the cookie string and add the new plantID with a forced quantity of 1
				var testStr = "PID" + plantID + ":";
				if (mPCookie.indexOf(testStr) == -1) {
					mPCookie = changeMPQTY(mPCookie, 1);
					mPCookie = mPCookie + "PID" + plantID + ":PQT1:"
					writeCookie(cookieName, mPCookie, 30);
					}
				}
			else {
// checkbox was deselected
// find the plantID within the cookie string and remove it and its associated quantity
				var testStr = "PID" + plantID + ":";
				if (mPCookie.indexOf(testStr) != -1) {
					var cutStart = mPCookie.indexOf(testStr);
					var cutEnd = mPCookie.indexOf(":", (cutStart + testStr.length + 3));				
					var newLeft = mPCookie.substring(0, cutStart);
					if (mPCookie.length > cutEnd + 1) {
						var newRight = mPCookie.substring(cutEnd + 1);
						}
					else {
						var newRight = "";
						}
					mPCookie = newLeft + newRight;
					mPCookie = changeMPQTY(mPCookie, -1);
					writeCookie(cookieName, mPCookie, 30);
					}
				}
			}
		}
}

function changeMPQTY(cString, change)	{
// This function modifies the mPQty header at the front of the cookie, which tells
// how many plants are in the cookie (i.e. in the user's plant list). It is modified
// whenever a plant is added or removed from the plant list.

	var firstMark = cString.indexOf(':');
	if (firstMark > 6) {
		var pQuantity = parseInt(cString.substring(5, firstMark));
		}
	else {
		var pQuantity = parseInt(cString.charAt(5));
		}
	if (cString.length > firstMark + 1) {
		var rightString = cString.substring(firstMark);
		return ("MPQTY" + String(pQuantity + change) + rightString);	
		}
	else {
		return ("MPQTY" + String(pQuantity + change) + ":");		
		}
}

function mPModifyPQT(mPobj, plantID, cookieID)	{
// This function is called when the user sets or changes the plant quantity for a
// given plant in their list. It is primarily a screening routine; most of the actual
// work is done by the "changePQT" function. It does, however, handle the responsibility
// of deleting a record if the "changePQT" function calls for it.

// first check to see that cookies are enabled
	var cookieName = cookieID + "=";
	var cookieStartIndex = document.cookie.indexOf(cookieName);
	
	if (cookieStartIndex == -1) {
// cookies not enabled
		alert("You must have cookies enabled in your browser to use the myPlants feature!");
		}
	else {
// cookies are enabled, so retrieve the cookie value as "mPCookie"
		var mPCookie = readCookie(cookieName);
		if (mPCookie) {
			var modString = changePQT(mPCookie, plantID, mPobj.value);
			if (modString != "zero") {
				mPCookie = modString;
				}
			else {
				var testStr = "PID" + plantID + ":";
				var cutStart = mPCookie.indexOf(testStr);
				var cutEnd = mPCookie.indexOf(":", (cutStart + testStr.length + 3));				
				var newLeft = mPCookie.substring(0, cutStart);
				if (mPCookie.length > cutEnd + 1) {
					var newRight = mPCookie.substring(cutEnd + 1);
					}
				else {
					var newRight = "";
					}
				mPCookie = newLeft + newRight;
				mPCookie = changeMPQTY(mPCookie, -1);
				}
			writeCookie(cookieName, mPCookie, 30);
			}
		}
}

function changePQT(cString, plantRef, qty)	{
// This function changes the user-set quantity for an individual plant in their plant list.
// It modifies the "PQT" part of the cookie for that particular plant. It is also responsible
// for managing "unexpected" values in the quantity field; it considers numeric values greater
// than zero to be intended quantities, zero or null to be a desire to remove the entire plant,
// and anything else to be an error, in which case it keeps the plant and resets the quantity
// to "1".

	var pidMarkStart = cString.indexOf("PID" + plantRef + ":");
	if (pidMarkStart != -1) {
		if ((qty != "") && (qty != "0") && (validInteger(qty))) {
			var modQty = qty;
			}
		else if ((qty == "") || (qty == "0")) {
			var modQty = "0";
			}
		else {
			var modQty = "1";
			}
		
		if (modQty != "0") {
			var pqtMarkStart = pidMarkStart + plantRef.length + 3 + 1 + 3;
			var pqtMarkEnd = cString.indexOf(":", pqtMarkStart);
			var leftString = cString.substring(0, pqtMarkStart);
			if (cString.length > pqtMarkEnd) {
				var rightString = cString.substring(pqtMarkEnd);
				return (leftString + modQty + rightString);
				}
			else {
				return (leftString + modQty + ":");
				}
			}
		else {
			return ("zero");
			}
		}
	else {
		return cString;
	}
}	

/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~~~ UTILITY FUNCTIONS ~~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
*/

function readCookie(name) {
// This function reads the value of the user's cookie into an array and pulls the
// correct cookie for processing by other routines.

	var cookieArray = document.cookie.split (';');
	for (var i = 0; i < cookieArray.length; i++) {
		var cTest = cookieArray[i];
		while (cTest.charAt(0)==' ') {
			cTest = cTest.substring(1, cTest.length);
			}
		if (cTest.indexOf(name) == 0) {
			return unescape(cTest.substring(name.length, cTest.length));
			}
		}
	return null;
}

function writeCookie(name, value, days) {
// This function writes a new or revised cookie to the user's computer.

	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days*24*60*60*1000));
		var expires = "; expires=" + date.toGMTString();
		}
	else var expires = "";
	document.cookie = name + escape(value) + expires + "; path=/";
}

function validInteger(intgr) {
// this function tests whether a string is numeric
	test1 = "0123456789";
	for (i=0;i<intgr.length;i++) {
		if (test1.indexOf(intgr.charAt(i)) == -1) return false;
		}
	return true;
}

/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~ E-MAIL FORM FUNCTIONS ~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
*/

function validEmail(email) {
	return (email.indexOf(".") > 2) && (email.indexOf("@") > 0);
}
	
function validateEmailForm(stateFlag) {

	test = document.EmailForm.mpename.value;
	if (test.length < 2) {
		alert ("Please enter your name");
		return false;
	}
		
	test = document.EmailForm.mpeaddress.value;
	if (test.length < 2) {
		alert ("Please enter your address");
		return false;
	}
		
	test = document.EmailForm.mpecity.value;
	if (test.length < 2) {
		alert ("Please enter your city");
		return false;
	}
		
	test = document.EmailForm.mpestate.value;
	if (test.length < 2) {
		if (stateFlag == "CAN") { alert ("Please enter your province"); }
		else { alert ("Please enter your state"); }
		return false;
	}

	test = document.EmailForm.mpezip.value;
	if (test.length < 5) {
		if (stateFlag == "CAN") { alert ("Please enter your postal code"); }
		else { alert ("Please enter your zip code"); }
		return false;
	}

	test = document.EmailForm.mpephone.value;
	if (test.length < 2) {
		alert ("Please enter your telephone number");
		return false;
	}

	if (document.EmailForm.mpeemail.value.length > 0) {
		if (validEmail(document.EmailForm.mpeemail.value) == false) {
			alert ("Please enter a valid e-mail address");
			return false;
		}
	}

	test = document.EmailForm.mpecomments.value;
	if (test.length > 5000) {
		alert ("Wow, you have a lot to say, but please make your comments a little shorter!");
		return false;
	}
	
	return true; 
}





function changePQTGOOD(cString, plantRef, qty)	{
// This is the original that works! Delete when the new version works!

// This function changes the user-set quantity for an individual plant in their plant list.
// It modifies the "PQT" part of the cookie for that particular plant. It is also responsible
// for managing "unexpected" values in the quantity field; it considers numeric values greater
// than zero to be intended quantities, zero or null to be a desire to remove the entire plant,
// and anything else to be an error, in which case it keeps the plant and resets the quantity
// to "1".

	var pidMarkStart = cString.indexOf("PID" + plantRef + ":");
	if (pidMarkStart != -1) {
//interpret a "null" or zero value as the user wanting to delete that entry
//if the value is anything other than an integer, delete the entry as well
		if ((qty != "") && (qty != "0") && (validInteger(qty))) {
			var modQty = qty;
			}
		else {
			var modQty = "1";
			}
		var pqtMarkStart = pidMarkStart + plantRef.length + 3 + 1 + 3;
		var pqtMarkEnd = cString.indexOf(":", pqtMarkStart);
		var leftString = cString.substring(0, pqtMarkStart);
		if (cString.length > pqtMarkEnd) {
			var rightString = cString.substring(pqtMarkEnd);
			return (leftString + modQty + rightString);
			}
		else {
			return (leftString + modQty + ":");
			}
		}
	else {
		return cString;
	}
}	



