//
//  validlib.jav.
//  Copyright (c) 1997-2002 Digital Things, LLC.
//  Unlimited license granted for current application only.
//
//  Client-side validation routines.
//
//  Change history:
//  07/07/98 - Digital Things, LLC - Initial Release.
//  Standard JavaScript library of data validation functions.

var _dirty = false

var _string = 1
var _text = 1
var _hlink = 5
var _date = 2
var _smallDate = 8
var _int = 3
var _float = 4
var _ccDate = 7
var _email = 9
var _ssn = 10
var _fein = 10
var _phone = 11
var _zip = 12
var _cc = 13
var _textarea = 14
var _image = 100
var _inv = 101

//
//  AppDocDirty()
//  Sets/gets value of '_dirty' global variable.
//  Parameter value: 'true' = set, 'false' = get.
//
function AppDocDirty(action)
{
	_dirty = action == true ? true : _dirty
	return _dirty
}

//  AppRound()
//  Accurately rounds decimals.
function AppRound(value, decimals)
{
    return(
    		parseFloat(value) != "NaN" && parseInt(decimals) != "NaN"
    		? (Math.round(value * Math.pow(10, decimals))) / Math.pow(10, decimals)
    		: "NaN"
    	  )
}

//
//  AppValidateValue()
//  Returns true if the value passed on 'val' is not in the exception list passed in 'notIn'.
//
function AppValidValue(val, notIn)
{
	if (notIn.length)
	{
		if (notIn.indexOf(val) != -1)
			return false
	}

	for (var i=0; i < val.length; i++)
	{
		if (val.charAt(i) != " ")
			return true
	}

	return false
}

//
//  AppValueIn()
//  Returns false if the value passed on 'val' is not in 'valIn'.
//
function AppValueIn(val, valIn)
{
	if (valIn.length)
	{
		for (var i=0; i < valIn.length; i++)
		{
			if (valIn.indexOf(val.charAt(i)) == -1)
				return false
		}
	}

	return true
}

//
//  AppIsFloat()
//  Returns true if 'str' is a valid float value, false otherwise.
//
function AppIsFloat(str)
{
	var off
	var rc = false

	if (str.length == 0 || (str.length == 1 && str == "-"))
		return rc

	off = str.indexOf("-")

	if (off > 0)
		return rc
	else if (str.indexOf("-", off+1) > -1)
		return rc

	off = str.indexOf(".")

	if (str.indexOf(".", off+1) > -1)
		return rc

	for (var i=0; !rc && i < str.length; i++)
	{
		if ( !rc && (str.charAt(i) == '-' || str.charAt(i) == '.'))
			rc = false
		else
			rc = !rc && isNaN(parseFloat(str.charAt(i)))
	}

	return (!rc && i)
}

//
//  AppIsInt()
//  Returns true if 'str' is a valid integer value, false otherwise.
//
function AppIsInt(str)
{
	return (AppIsFloat(str) && str.indexOf(".") == -1)
}

//
//  AppValidateField()
//  Validates 'field' for specified data type.
//  Low and high ranges can be specified for numbers and dates, a default value can be set to
//  if validation fails.
//  Returns true if valid, false otherwise, null values are always valid.
//  Data types are:
//  1: String.
//  2: Date.
//  3: Integer.
//  4: Float.
//  7: Credit card expiration date.
//  8: E-mail address.
//
function AppValidateField(field, type, lowRange, hiRange, dflt)
{
	var v
	var leapError = false
	var rc = true
	var off
	var tmp = ""
	var i
	var focus = false

	//  (null) is always valid.
	if (field.value.length == 0)
		return true

	//  Determine browser and set indicator.
	//  Navigator processes the onBlur event (where this function is ususally called) in a different way
	//  than Explorer. Setting focus causes Navigator to enter an infinite event loop.
	if (navigator.appName.indexOf("etscape") == -1)
		focus = true

	//  String/Link/Text, etc...
	if (type == _string || type == _hlink|| type == 6 || type == _image || type == _textarea)
	{
		off = field.value.indexOf("\"")

		if (off != -1)
		{
			rc = false

			for (i = 0; i < field.value.length; i++)
			{
				if (field.value.charAt(i) != "\"")
					tmp = tmp + field.value.charAt(i)
			}

			field.value = tmp
			alert("Double quotes (\") are illegal in HTML text and were removed.\nPlease use single quotes (') instead.")
		}

		if (type == _textarea && (field.value.length > hiRange && hiRange > 0))
		{
			rc = false
			field.value = field.value.substring(0, hiRange)
			alert("This field should not exceed " + hiRange + " characters.\nText has been truncated.")
		}
	}
	// Date
	else if (type == _date)
	{
		v = field.value.split("/")

		if (v.length < 3)
			rc = false

		if (rc && (!AppIsInt(v[0]) || !AppIsInt(v[1]) || !AppIsInt(v[2])))
			rc = false
		else
		{
			vMth = parseInt(v[0])
			vDay = parseInt(v[1])
			vYr = parseInt(v[2])

			if (vYr < 1753 || vYr > 9999)
				rc = false

			if (
			   //===vYr < 1 ||
			   (vMth < 1 || vMth > 12) ||
			   ((vDay < 1 || vDay > 31) && vMth != 2) ||
			   ((vDay < 1 || vDay > 29) && vMth == 2) ||
			   (vMth == 2 && vDay == 29 &&
			      ((vYr % 4 != 0) || (vYr % 4 == 0 && vYr % 100 == 0 && vYr % 400 != 0)))
			   )
			{
				rc = false

				if (vMth == 2 && vDay == 29)
					leapError = true
			}
		}

		if (!rc)
		{
			if (leapError)
			{
				alert("Invalid date. '" + vYr + "' is not a leap year.")
				field.value = "2/28/" + vYr
			}
			else
			{
				alert("Invalid date. Date format: 'dd/mm/[yy]yy'.")

				if (dflt == 0)
					field.value = "01/01/1900"
				else
					field.value = dflt
			}
		}
	}
	// Credit card date
	else if (type == _ccDate)
	{
		v = field.value.split("/")

		if (v.length != 2)
			rc = false

		if (rc && (!AppIsInt(v[0]) || !AppIsInt(v[1])))
			rc = false
		else
		{
			vMth = parseInt(v[0])
			vYr = parseInt(v[1])

			if ( vYr < 0 || (vMth < 1 || vMth > 12) )
				rc = false
		}

		if (!rc)
		{
			alert("Invalid expiration date. Format: 'mm/yy'.")

			if (dflt == 0)
				field.value = "01/00"
			else
				field.value = dflt
		}
	}
	// Current Date
	else if (type == 8)
	{
		v = field.value.split("/")

		if (v.length < 2)
			rc = false

		if (rc && (!AppIsInt(v[0]) || !AppIsInt(v[1])))
			rc = false
		else
		{
			vMth = parseInt(v[0])
			vDay = parseInt(v[1])

			if (
			   (vMth < 1 || vMth > 12) ||
			   ((vDay < 1 || vDay > 31) && vMth != 2) ||
			   ((vDay < 1 || vDay > 29) && vMth == 2)
			   )
				rc = false
		}

		if (!rc)
		{
				alert("Invalid start date. Format: 'mm/dd'.")

				if (dflt == 0)
					field.value = "01/01"
				else
					field.value = dflt
		}
	}
	// Inventory control
	else if (type == _inv)
	{
		if ( !AppIsInt(field.value) )
		{
			alert("Please enter numeric values only.")
			field.value = dflt
			rc = false
		}
		else if ((hiRange || lowRange) && (parseInt(field.value) > hiRange || parseInt(field.value) < lowRange))
		{
			alert("Inventory control:\nPlease enter a value between '" + lowRange + "' and '" + hiRange + "'.")
			field.value = 1
			rc = false
		}
	}
	// Number
	else if (type == _int)
	{
		if ( !AppIsInt(field.value) )
		{
			alert("Please enter numeric values only.")
			field.value = dflt
			rc = false
		}
		else if ((hiRange || lowRange) && (parseInt(field.value) > hiRange || parseInt(field.value) < lowRange))
		{
			alert("Please enter a value between '" + lowRange + "' and '" + hiRange + "'.")
			field.value = dflt
			rc = false
		}
	}
	// Float
	else if (type == _float)
	{
		if ( !AppIsFloat(field.value) )
		{
			alert("Valid decimal value required.")
			field.value = dflt

			if (focus)
				field.focus()

			rc = false
		}
		else if ((hiRange || lowRange) && (parseInt(field.value) > hiRange || parseInt(field.value) < lowRange))
		{
			alert("Please enter a value between '" + lowRange + "' and '" + hiRange + "'.")
			field.value = dflt
			rc = false
		}
	}
	// E-Mail
	else if (type == _email)
	{
		var notThese = " <,>?\"'|\\}]{[=)(&^%#~`"
		var maxAt = 0
		rc = true

		for (var i=0; i < field.value.length; i++)
		{
			if (field.value.charAt(i) == '@')
				maxAt++

			if (notThese.indexOf(field.value.charAt(i)) != -1)
			{
				rc = false
				break
			}
		}

		if (!rc)
		{
			alert("Spaces or special characters\nare not allowed in E-Mail adresses.")
			field.value = ""
		}
		else if (field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1 || maxAt > 1)
		{
			rc = false
			alert("Invalid E-Mail address format.\nPlease use valid format: (eg: User@Domain.com).")
			field.value = ""
		}

	}
	//  SSN/FEIN
	else if (type == _fein)
	{
		var ssn = field.value

		if (ssn.length == 9 && AppValueIn(ssn, "0123456789"))
			rc = true
		else if(ssn.length == 11 && AppValueIn(ssn, "0123456789-") && ssn.charAt(3) == "-" && ssn.charAt(6) == "-")
			rc = true
		else if(ssn.length == 10 && AppValueIn(ssn, "0123456789-") && ssn.charAt(2) == "-")
			rc = true
		else
		{
			rc = false
			alert("Invalid Tax ID. Valid formats:\nSSN: '999-99-9999' or '999999999'.\nFEIN: '99-9999999' or '999999999'")
		}
	}
	//  US Phone Number
	else if (type == _phone)
	{
		if (field.value.length > 14 || !AppValueIn(field.value, "0123456789-() "))
		{
			rc = false
			alert("Invalid Phone Number format. Formats allowed:\n1: '(999) 999-9999'.\n2: '999-999-9999'.\n3: '999 999 9999'.")
		}
	}
	//  ZIP code
	else if (type == _zip)
	{
		var zip = field.value
		rc = false

		if (zip.length == 5 && AppValueIn(zip, "0123456789"))
			rc = true

		if (zip.length > 5)
		{
			xZip = zip.split("-")

			if (zip.length > 9 && (xZip.length == 2 && AppValueIn(xZip[1], "0123456789")))
				rc = true
			else if (zip.length == 6 && zip.indexOf(" ") == -1)
				rc = true
		}

		if (!rc)
			alert("Invalid ZIP code. Valid formats:\nUSA: '99999'.\nUSA: '99999-9999\[9\]'.\nWorld: '99999'.\nCN: 'A9A9A9'.\nGB: 'A99AAA'.")
	}
	//  Credit Card Number
	else if (type == _cc)
	{
		if (field.value.length < 15 || !AppIsInt(field.value))
		{
			rc = false
			alert("Invalid Credit Card Number.\nPlease enter digits only (no less than 15).")
		}
	}

	if (!rc)
	{
		if (focus)
			field.focus()

		field.select()
	}

	return rc
}

//
//  AppFieldIsEmpty()
//  Return true if the passed field is empty (null or spaces), false otherwise.
//
function AppFieldIsEmpty(field)
{
	var i = 0
	var n = 0

	if (field.value.length == 0)
		return true

	for (i = 0; i < field.value.length; i++)
	{
		if (field.value.charAt(i) == " ")
			n++
	}

	if (n && i == n)
		return true

	return false
}

//
//  AppAppCheckLimit()
//  Return true if the item count is OK, false otherwise.
//
function AppCheckLimit(fld, min, max, limit)
{
	if (limit > 0 && fld.value > limit)
	{
		alert("Quantity limit for this product: " + limit)
		fld.value = limit
		return false
	}

	AppValidateField(fld, _inv, min, max, 1)
	return true
}

//
//  AppShowDIV()
//  Utility function to display a particular division in a division set.
//  Inputs:
//  - divMask: the mask identifying a DIV set (example: myDiv).
//  - divID: the DIV id we want to make visible (example: myDiv1).
//  Output: none.
//
function AppShowDIV(divMask, divID)
{
	//  Enumerate DIVs on document, place them in an array, exit if none.
	var divs = document.getElementsByTagName("div"), elem

	if (divs.length == 0)
		return

	//  Disable all DIVs matching the mask then enable to requested DIV.
	for (var i = 0; i < divs.length; i++)
	{
		if (divs[i].id.substring(0, divMask.length) == divMask)
		{
			elem = document.getElementById(divs[i].id)
			elem.style.display = "none"
		}
	}

	elem = document.getElementById(divID)
	elem.style.display = "block"
}

//
//  AppImagePopup()
//  Utility function to open a product image pop-up.
//  Inputs:
//  - tnMask: the mask identifying a IMG set (example: myImg).
//  - imgID: the default image to display on the pop-up.
//  - wWidth: Pop-up window width.
//  - wHeight: Pop-up window height.
//  Output: none.
//
function AppImagePopup(tnMask, imgID, wWidth, wHeight)
{
	//  Enumerate IMG on document, place them in an array, exit if none.
	var imgs = document.getElementsByTagName("img")
	var param = imgID
	var tnPath = "/ezstore123/resources/thumbnails/"

	//  Exit if no images specified.
	if (imgs.length == 0)
		return

	//  Process images, build image list based on images matching 'tnMask'. Strip path from image list.
	for (var i = 0; i < imgs.length; i++)
	{
		if (imgs[i].id.substring(0, tnMask.length) == tnMask)
			param += "~" + imgs[i].src.substring(imgs[i].src.lastIndexOf("/")+1, 512)
	}

	//  Open pop-up window.
	window.open("/ezstore123/DTProductImagePopup.asp?param=" + param, "popup", "width=" + wWidth + ",height=" + wHeight + ",scrollbars=0, resizable=0,menu=0,address=0")
}

//
//  AppPopUp()
//  Utility for custom pop-ups.
//
function AppPopUp(_url, _width, _height, _resize, _scroll)
{ 
	//_resize = _resize == "Y" || _resize == "y" || _resize == "1" ? "1" : "0";
	//_scroll = _scroll == "Y" || _scroll == "y" || _scroll == "1" ? "1" : "0";
	var newWin = null;

	if (parseInt(_width) != "NaN" && parseInt(_height) != "NaN" )
	{
		newWin = window.open(_url, "appPopUp", "width=" + _width + ",height=" + _height + ",resizable=" + _resize + ",scrollbars=" + _scroll)

		if (newWin)
			parent.document.frm.termsAccepted.checked = true;
	}
	else
		alert("Pop-up(): Invalid parameters.")

	return
} 
