// Various functions
// this does not require findDOM.js

// Convert Decimal to Hex
function decToHex(decN) {
	var hexchart = '0123456789ABCDEF'
	var n = parseInt(decN/16)
	var hexN = hexchart.charAt(n)
	decN -= n*16
	hexN += hexchart.charAt(decN)
	return hexN
}

// Convert Hex to Decimal
function hexToDec(hexN) {
	var hexchart = '0123456789ABCDEF'
	var n = hexN.charAt(0)
	var decN = n.indexOf(n) * 16
	var n = hexN.charAt(1)
	decN += n.indexOf(n)
	return decN
}

// This simple encrytion takes a numeric seed value and 
// converts it to an alphanumeric code of equal length
function encrypt1(seed) {
	var encryptchart = 'ND3BUE9ZY7AR0JHS5GC2TWM6FP1VDQ8ILX4KO'
	var x = 0
	var code = ""
	seed = seed.toString()
	for(i=0;i < seed.length();i++) {
		digit = seed.charAt(i)
		x = x + parstInt(digit)
		char = ""
		while (char == "") {
			char = encryptchart.charAt(x)
			if (char == "") x = x - 36 }
		code = code + char }
	return code
}

// Find a Cookie value
function findCookie(cookieName) {
	if (document.cookie == "") return "NoCookies";
	var cookies = document.cookie
	var where = cookies.indexOf(cookieName,0)
	var i
	// the cookie is found so now find the value
	if (where == -1) return "NoCookie"; 
	var cookieArray = new Array()
	cookieArray = cookies.split("; ")
	for (i = 0; i < cookieArray.length; i++) {
		where = cookieArray[i].indexOf(cookieName,0)
		if (where != -1) {
			where = cookieArray[i].indexOf("=",0)
			// make sure you have the right cookie
			if (cookieName == cookieArray[i].substring(0,where)) {
				return cookieArray[i].substring(where + 1, cookieArray[i].length) } 
			}
		}
	return "NoCookie"
}

// Delete a Cookie
function deleteCookie(cookieName) {
	// change the exipery date and let the browser delete it
	var expireDate = new Date
	expireDate.setDate(expireDate.getDate()-1)
	if (document.cookie != "") { 
		// the cookies are found so now find the value
		var cookies = document.cookie
		var where = cookies.indexOf(cookieName,0)
		var i
		if (where != -1) {
			var cookieArray = new Array()
			cookieArray = cookies.split("; ")
			for (i = 0; i < cookieArray.length; i++) {
				where = cookieArray[i].indexOf(cookieName,0)
				if (where != -1) {
					where = cookieArray[i].indexOf("=",0)
					// make sure you have the right cookie
					if (cookieName == cookieArray[i].substring(0,where)) {
						document.cookie = cookieName + "=;expires=" + expireDate.toGMTString() } 
					}
				}
			}
		}
}

// Save a Cookie
function saveCookie(cookieName,cookieValue,validDays) {
	var expireDate = new Date()
	expireDate.setDate(expireDate.getDate() + validDays)
	document.cookie = cookieName + "=" + cookieValue + "; expires=" + expireDate.toGMTString() + "; path=/"
}

// Format amounts as currency
var WGdc=".";  var WGgc=",";  var WGnc="(";  var WGcs="$";
function FormatMoney(A,W) {
	if (A == 0) {
		return "FREE" }
	else {
	   var N=Math.abs(Math.round(A*100));
	   var S=((N<10)?"00":((N<100)?"0":""))+N;
		S=WGcs+((A<0)?WGnc:"")+WGgroup(S.substring(0,(S.length-2)))+WGdc+S.substring((S.length-2),S.length)+((A<0&&WGnc=="(")?")":"");
		return (S.length>W)?"Over":S; }
}
function WGgroup(S) {
	return (S.length<4)?S:(WGgroup(S.substring(0,S.length-3))+WGgc+S.substring(S.length-3,S.length));
}

// Convert a number to money format.  Takes a Number and returns a String.
function NumberToMoney(Amount, Free) {
	var x = new Number(Amount)
	var Decimal = "."
	var Seperator = ","
	var Symbol = "$"
	var NegStart = "("
	var NegEnd = ")"
	if (Free == "true" && x == 0) {
		return "FREE" }
	else {
		Neg = (x < 0)
		if (Neg) {
			x = Math.abs(x) }
		else {
			NegStart = ""
			NegEnd = "" }
		Frac = Math.round((x - Math.floor(x)) * 100)
		if (Frac == 0) {
			Frac = "00" }
		else if (Frac < 10) {
			Frac = "0" + Frac }
		Int = AddSep(Math.floor(x), Seperator)
		return Symbol + NegStart + Int + Decimal + Frac + NegEnd }
}		
function AddSep(Int, Seperator) {
	if (Int.length > 3) {
		AddSep(Int.substring(0,Int.length - 3),Seperator) + Seperator + Int.substring(Int.length-3,Int.length) }
	return Int 
}		

// Converts money format to a number.  Takes a String and returns a Number.
function MoneyToNumber(Amount) {
	var x = new String(Amount) 
	var Decimal = "."
	var Seperator = ","
	var Numbers = "0123456789" + Decimal
	if (x == "0" || x == "$0.00" || x.toLowerCase() == "free") {
		return 0 }
	else {
		// remove all charactors except the decimal
		var NewX = ""
		for (var i = 0; i < x.length; i++) {
			oneChar = x.charAt(i)
			if (Numbers.indexOf(oneChar) != -1) {
				NewX = NewX + oneChar }
		}		
		// find the sign
		if (x.indexOf("(") != -1 || x.indexOf("-") != -1) NewX = "-" + NewX
		return parseFloat(NewX)
	}
		
}

// finds the value of the checked option in an option group
function optionValue(optionObj) {
	optionVal = "none"
	for (i = 0; i < optionObj.length; i++) {
		if (optionObj[i].checked) {
			optionVal = optionObj[i].value } }
	return optionVal
}

/* Field validation functions.  These use a format string to do the 
validation.  The strings are processed from left to right.
Each charactor in the format string represents a charactor in the
test string.  Optional formats will not cause the function to 
return false. The format charators are as follows:
Number formats              Text Formats
. decimal                   A uppercase letter
, comma sep (optional)      a lowercase letter
0 number                    @ letter
# number (optional)         ? charactor
$ currency sign             * charactor (optional)
% percent sign              - dash
- negative sign (optional)  0 number
( negative sign (optional)  # number (optional)
) negative sign (optional)  S space
                            s space (optional)       */

function numberFormat(fldValue, fldFormat) {
	// Format number:.,0#$%-()
	i = fldFormat.length - 1
	j = fldValue.length - 1
	for (i; i > -1; i--) {
		switch (fldFormat.charAt(i)) {
			// check required items
			case ".":
				if (j < 0) return false
				else {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar == 46) j = j - 1
					else return false }
				break
			case "0":
				if (j < 0) return false
				else {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar > 47 && fldChar < 58) j = j - 1
					else return false }
				break
			case "$":
				if (j < 0) return false
				else {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar == 36) j = j - 1
					else return false }
				break
			case "%":
				if (j < 0) return false
				else {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar == 37) j = j - 1
					else return false }
				break
			// check optional items
			case ",":
				if (j != -1) {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar == 44) j = j - 1 }
				break
			case "(":
				if (j != -1) {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar == 40) j = j - 1 }
				break
			case ")":
				if (j != -1) {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar == 41) j = j - 1 }
				break
			case "#":
				if (j != -1) {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar > 47 && fldChar < 58) j = j - 1 }
				break
			case "-":
				if (j != -1) {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar == 45) j = j - 1 }
				break
			// other
			case "":
				return false
				break
			default:
				alert("Format is invalid") } }
	if (j != -1) return false
	else return true
}
function textFormat(fldValue, fldFormat) {
	// Format text:? *Aa@0
	x = fldFormat.length
	y = fldValue.length
	j = 0
	for (i = 0; i < x; i++) {
		switch (fldFormat.charAt(i)) {
			// check required items
			case " ":
				if (j == y) return false
				else {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar == 32) j = j + 1
					else return false }
				break
			case "?":
				if (j == y) return false
				else {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar > 31 && fldChar < 127) j = j + 1
					else return false }
				break
			case "-":
				if (j == y) return false
				else {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar == 45) j = j + 1
					else return false }
				break
			case "A":
				if (j == y) return false
				else {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar > 64 && fldChar < 91) j = j + 1
					else return false }
				break
			case "a":
				if (j == y) return false
				else {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar > 96 && fldChar < 123) j = j + 1
					else return false }
				break
			case "S":
				if (j == y) return false
				else {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar == 32) j = j + 1
					else return false }
				break				
			case "@":
				if (j == y) return false
				else {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar > 64 && fldChar < 91) j = j + 1
					else {
						if (fldChar > 96 && fldChar < 123) j = j + 1
						else return false } }
				break
			case "0":
				if (j == y) return false
				else {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar > 47 && fldChar < 58) j = j + 1
					else return false }
				break
			// check optional items
			case "*":
				if (j < y) {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar > 31 && fldChar < 127) j = j + 1 }
				break
			case "s":
				if (j < y) {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar == 32) j = j + 1 }
				break
			case "#":
				if (j < y) {
					fldChar = fldValue.charCodeAt(j)
					if (fldChar > 47 && fldChar < 58) j = j + 1 }
				break
			// other
			case "":
				return false
				break
			default:
				alert("Format is invalid") } }
	if (j != y) return false
	else return true
}

function datetimeFormat(fldValue, fldFormat) {
	// Format datetime:dmyhns
}
