/* Liquitech API - Common **********************************
 *	
 *	This API contains all the common Javascript functions
 *	
 *********************************************************************/
 	
 	// Globals *************************************************
    var currentFocus = "";
    
 	// Broswer Detection ****************
 	var IE = document.all?true:false; // determine which browser 
	var thisBrowser = navigator.userAgent;
	var thisBrowserVersion = null;
	
	if (thisBrowser.indexOf("MSIE") != -1)
	{
		thisBrowser = "IE";
		
		if (thisBrowser.indexOf("MSIE 8") != -1)
		{
			thisBrowserVersion = 8;	
		}
		
	}
	else if (thisBrowser.indexOf("Firefox") != -1)
	{
		thisBrowser = "Firefox";	
	}
	else if (thisBrowser.indexOf("Safari") != -1)
	{
		thisBrowser = "Safari";	
	}
	else
	{
		thisBrowser = "Other";	
	}
	
	
	var mousePositionX = "";
	var mousePositionY = "";
	
	
	// Current Date and Time *****************************
	var d = new Date();
	var yearValue = d.getYear();
	var hourValue = d.getHours();
	var amPmValue = "AM";
	
	if (!IE)
	{
		yearValue += 1900;	
	}
	
	if (hourValue > 11)
	{
		amPmValue = "PM";	
	}
	
	var now = (d.getMonth()+1) + "/" + d.getDate() + "/" + yearValue + " " + hourValue + ":" + d.getMinutes() + ":" + d.getSeconds() + " " + amPmValue;
			
	
/*  Get Mouse Position * *******************************************************
 *
 *	TAKES:		Event Object
 * 	RETURNS:	NOTHING
 *	NOTES:		IE by default does not calculate the page scrolling in the
 *				mouse postion, but the screen position.  Since FF and NS include
 *				page scroll in the mouse position, we're adding to IE as well.
 *
 *************************************************************************/
	
	function getMousePosition(e)
	{
		var posX = "";
		var posY = "";
		
		if (IE)
		{ 
			// grab the y pos.s if browser is IE
			posX = event.clientX - 2 + getPageXOffset();
			posY = event.clientY - 2 + getPageYOffset();
	  	}
		else
		{ 
			// grab the y pos.s if browser is NS
			posX = e.pageX;
			posY = e.pageY;
		}  
	  
	   // catch possible negative values in NS4
	   if ((posX < 0)||(posY < 0))
	   {
		  posX = 0;
		  posY = 0;
	   }  
	  
	 	mousePositionX = posX;
	 	mousePositionY = posY;
		
	}
	
 
 /* Is Array * *******************************************************
 *
 *	TAKES:		Array Object
 * 	RETURNS:	True if the array object is actually an array. False if the 
 *				array object is not an array.
 *	NOTES:		Usage: [Array Obj].isArray();
 *
 *************************************************************************/
 
 	Object.prototype.isArray = function()
	{
		return this.constructor == Array;
	}
	
/* Data Is Valid **********************************************************
*
*	TAKES:		Data, Data Type
*	RETURNS:	True if the data is valid, False if the data is not valid
*				based on the data type. Data types include:
*
*					1. String	- Ex: "abc123"
*					2. Integer	- Ex: 123
*					3. Float	- Ex: 25.99
*					4. Email	- Ex: test@email.com
*					5. Password - Ex: *******
*					6. Phone	- Ex: 123-444-5555
*
*	NOTES:		This function genreally doesn't enforce the maximum number
*				of characters for given data, this should be handled with 
*				character limits using HTML forms.
*
**************************************************************************/

	function dataIsValid(data, dataType)
	{
		var regExp;
		var errorMessage
		data = String(data);
		
		switch(dataType)
		{
			case "string":
				
				regExp = /^(\w|\s)+$/;
				errorMessage = "Invalid characters were used. (Ex: <, >, %, \", ', #).";
				break;
				
			case "integer":
				
				regExp = /^\d+$/;
				errorMessage = "This field can only contain whole numbers (Ex: 1, 23, 675).";
				break;
				
			case "float":
			
				regExp = /^\d+(\.\d+|)$/;
				errorMessage = "This field can only contain decimal numbers (Ex: 51, 1.0, 23.5, 67.5).";
				break;
				
			case "fraction":
			
				regExp = /^(|[0-9]+ )[0-9]+\/[0-9]+$/;
				errorMessage = "This field can only contain fractions (Ex: 12 3/4, 1/2).";
				break;
				
			case "email":
			
				regExp = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
				errorMessage = "This field can only contain valid email addresses (Ex: johndoe@emailexample.com).";
				break;
				
			case "password":
			
				regExp = /^.{6}/
				errorMessage = "This field must be at least six characters long and not contain any invalid characters. (Ex: <, >, %, \", ', #)";
				break;
			
			case "phone":
			
				regExp = /^((\(\d{3}\)( |))|(\d{3}( |-|.|)))\d{3}( |-|.|)\d{4}(,| |x|e)*/;
				errorMessage = "This field can only contain phone numbers (Ex: 111-555-4444).";
				break;
			
			case "zip":
			
				regExp = /^\d{5}([\-]\d{4})?$/;
				errorMessage = "This field can only contain zip codes (Ex: 12345-5555).";
				break;
			
			case "text":
			
				regExp = /^.*$/;
				errorMessage = "Invalid characters were used. (Ex: <, >, %, \", ', #).";
				break;
			
			case "date":
			
				regExp = /^\d{2}\/\d{2}\/\d{4}$/;
				errorMessage = "This field can only contain dates in the format MM/DD/YYYY.";
				break;
				
			default:
				
				return false;
		}
		
		if ( (data.search(regExp) != -1) & isValid(data) )
		{
			return true;	
		}
		else
		{
			return errorMessage;	
		}
	}
	
/* Is Valid **********************************************************
*
*	TAKES:		Data
*	RETURNS:	True if the data is valid, False if the data is not valid
*				based on the data type. Data types include:
*
**************************************************************************/

	function isValid(data)
	{
		var regExp = /(<|>|"|%|'|#)/;
		
		if (data.search(regExp) != -1)
		{
			return false;	
		}
		else
		{
			return true;	
		}
		
	}
	
/*  Encode Query String * *******************************************************
 *
 *	TAKES:		Query String
 * 	RETURNS:	Encrypyed Query String
 *	NOTE:		Special characters that need to be encrypted:
 *
 *					- ;	%3B
 *					- ?	%3F
 *					- /	%2F
 *	 				- :	%3A
 *					- #	%23
 *					- &	%26
 *					- =	%3D
 * 					- +	%2B
 *					- $	%24
 *					- ,	%2C
 *					- <space>	%20
 *					- %	%25
 *					- <	%3C
 *					- >	%3E
 *					- ~	%7E
 *
 *************************************************************************/
 
	function encodeQueryString(queryString)
	{
		queryString = queryString.replace(/%/g, "%25");
		queryString = queryString.replace(/;/g, "%3B");
		queryString = queryString.replace(/\?/g, "%3F");
		//queryString = queryString.replace(/\//g, "%2F");
		queryString = queryString.replace(/:/g, "%3A");
		queryString = queryString.replace(/#/g, "%23");
		queryString = queryString.replace(/ /g, "%20");
		queryString = queryString.replace(/&/g, "%26");
		queryString = queryString.replace(/=/g, "%3D");
		queryString = queryString.replace(/\+/g, "%2B");
		queryString = queryString.replace(/\$/g, "%24");
		queryString = queryString.replace(/,/g, "%2C");
		queryString = queryString.replace(/</g, "%3C");
		queryString = queryString.replace(/>/g, "%3E");
		queryString = queryString.replace(/~/g, "%7E");
		queryString = queryString.replace(/\n/g, "<br />");
		
		return queryString;

	}
	
/* Right **********************************************************
*
*	TAKES:		str - the string we are RIGHTing
*               n - the number of characters we want to return
*	RETURNS:	n characters from the right side of the string
*
**************************************************************************/

	function Right(str, n)
    {
		if (n <= 0)     // Invalid bound, return blank string
		{
			return "";
		}
		else if (n > String(str).length)   // Invalid bound, return
		{
			return str;                     // entire string
		}
		else
		{ 
		   // Valid bound, return appropriate substring
		   var iLen = String(str).length;
		   return String(str).substring(iLen, iLen - n);
		}
    }
	
/* Left **********************************************************
*
*	TAKES:		str - the string we are LEFTing
*               n - the number of characters we want to return
*	RETURNS:	n characters from the left side of the string
*
**************************************************************************/
		
	function Left(str, n)
	{
			if (n <= 0)     // Invalid bound, return blank string
			{
				return "";
			}
			else if (n > String(str).length)   // Invalid bound, return
			{
				return str;                // entire string
			}
			else // Valid bound, return appropriate substring
			{
				return String(str).substring(0,n);
			}
	}

	
/* Trim **********************************************************
*
*	TAKES:		String
*	RETURNS:	String with the whitespaces on the left and right sides trimmed.
*
**************************************************************************/

	function Trim(stringToTrim)
	{
		return stringToTrim.replace(/^\s+|\s+$/g,"");
	}
	

	
/* Fraction To Decimal **********************************************************
*
*	TAKES:		Fraction Data string
*	RETURNS:	Decimal representation of the fraction
*
**************************************************************************/

	function fractionToDecimal(data)
	{
		if (dataIsValid(data, "fraction") == true)
		{
			// Convert fractions to decimal
			var wholeNum 	= 0;
			var fraction	= 0;
			var numerator 	= 0;
			var denominator	= 0;
			var decimalValue = 0;
			
			var fractionValues = data.split(" ");
			
			if (fractionValues.length > 1)
			{
				wholeNum = fractionValues[0];
				fraction = fractionValues[1];
			}
			else
			{
				fraction = fractionValues[0];
			}
			
			if (fraction.search(/\//) != -1)
			{
				fractionValues 	= fraction.split("/");
				numerator  		= fractionValues[0];
				denominator  	= fractionValues[1];
				
				if (denominator > 0)
				{
					decimalValue = (numerator/denominator);
				}
				else
				{
					decimalValue = 0;
				}
			}
			
			data = Number(wholeNum) + Number(decimalValue);
		}
		
		return data;
	}


	
/* Round Float **********************************************************
*
*	TAKES:		Float Value, Number of Significant Digits
*	RETURNS:	Truncated Float Value
*	NOTES:		
*
*********************************************************************************/

	function roundFloat(floatValue, numDigits)
	{
		if (numDigits == "")
		{
			numDigits = 0;	
		}
		
		var digitPosition;
		var multiplier = Math.pow(10, numDigits);
		
		floatValue = parseFloat(floatValue);
		floatValue = parseFloat(floatValue * multiplier);
		floatValue = Math.round(floatValue) / multiplier;
		
		if (String(floatValue).indexOf(".") > -1)
		{
			digitPosition = Right(String(floatValue), String(floatValue).length - (String(floatValue).indexOf(".") + 1));
			digitPosition = digitPosition.length;
		}
		else
		{
			digitPosition = 0;
			floatValue += ".";
		}
		
		if (digitPosition < numDigits)
		{
			for(var zeroCtr=digitPosition; zeroCtr<numDigits; zeroCtr++)
			{
				floatValue += "0";
			}
		}
		
		return floatValue;
	
	}

/* Pad Number **********************************************************
*
*	TAKES:		Number Value
*	RETURNS:	NOTHING
*	NOTES:		
*
*********************************************************************************/

	function padNumber(num)
	{
		if (num <= 9)
		{
			return "0" + num;
		}
	  
		return num;
	}


/* Set Department Cookie **********************************************************
*
*	TAKES:		Cookie Name, Cookie Value
*	RETURNS:	NOTHING
*	NOTES:		
*
*********************************************************************************/

	function setCookie(cookieName, cookieValue)
	{
		if (cookieName)
		{
			
			var expireDays 	= 365; // one year
			var expDate		= new Date();
			
			expDate.setDate(expDate.getDate() + expireDays);
			document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expDate.toGMTString();
			
		}
		
	}
	
/* Get Department Cookie **********************************************************
*
*	TAKES:		NOTHING
*	RETURNS:	Cookie Value
*	NOTES:		
*
*********************************************************************************/

	function getCookieValue(cookieName)
	{
		if (cookieName)
		{
			var cookieElements 	= document.cookie.split(";");
			var cookieElement 	= "";
			var cookieData		= "";
			var thisCookieName	= "";
			var thisCookieValue	= "";
			
			if (cookieElements.length > 0)
			{
				for(var ctr=0; ctr<cookieElements.length; ctr++)
				{
					cookieElement = cookieElements[ctr];
					
					cookieData = cookieElement.split("=");
					
					thisCookieName = cookieData[0];
					thisCookieValue = cookieData[1];
					
					if (thisCookieName == cookieName)
					{
						return (unescape(thisCookieValue));
					}
				}
			}
		}
		
		return false;
		
	}
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	
	
	
	
	
