//poll related function
var ToShowPoll = 1

function SetNoShowPoll() {
	ToShowPoll = 0
}

//check whether passed parameter is a valid number
function notNumber(number) {
	number = number.toString()

	for (var i=0; i<number.length; i++) {
		if (number.charAt(i) > "9" || number.charAt(i) < "0") return true
	}

	return false
}


//adds digit grouping to the number
function formatNumber(number) {
	if (number.toString() == "") return "On Request"

	if (number < 0) {
		var sign = "-"
		number = -number
	} else{
		var sign = ""
	}

	number = number.toString()
	var tmpNumber = ""

	for (var i=0; i<number.length; i++) {
		if (i % 3 == 0 && i != 0) tmpNumber = "," + tmpNumber
		tmpNumber = number.substring(number.length - i - 1, number.length - i) + tmpNumber
	}

	return sign + tmpNumber
}


//check whether passed validated control contains any of passes invalid characters (case insensitive)
function invalidChars(validatedControl, validatedName, charString) {
	var validatedString = trim(validatedControl.value).toLowerCase()
	charString = charString.toLowerCase()

	for (var i=0; i<validatedString.length; i++) {
		for (var j=0; j<charString.length; j++) {
			if (validatedString.charAt(i) == charString.charAt(j)) {
				var alertString = charString.split("").join("  ")
				alertString = alertString.split("     ").join("  space  ")
				alert(validatedName + " cannot contain any of the following\nillegal characters:\n\n " + alertString)
				validatedControl.focus()
				return true
			}
		}
	}

	return false
}


//check whether text-box is empty
function isEmpty(field, fieldName) {
	if (trim(field.value) == "") {
		alert("Please enter " + fieldName + ".")
		field.focus()
		return true
	}

	return false
}


//check whether selection has been made in the select-box
function notSelected(field, fieldName) {
	if (field.selectedIndex == 0) {
		alert("Please select " + fieldName + ".")
		field.focus()
		return true
	}

	return false
}


//trim string
function trim(stringToTrim) {
	var trimmedString = ""

	//left trim
	for(var i=0; i<stringToTrim.length; i++) {
		if (stringToTrim.charAt(i) != " ") break
	}

	trimmedString = stringToTrim.substring(i)

	//right trim
	for(var i=trimmedString.length-1; i>=0; i--) {
		if (trimmedString.charAt(i) != " ") break
	}

	trimmedString = trimmedString.substring(0, i + 1)

	return trimmedString
}


//validate email address
function notEmail(field) {
	var email = trim(field.value)

	if (email == "") return false

	var at = false
	var dot = false

	for (var i=0; i<email.length; i++) {
		if (email.charAt(i) == "@") at = true
		if (email.charAt(i) == "." && at) dot = true
	}

	if (at && dot && email.length > 5) {
		return false
	} else {
		alert("The e-mail you entered is not a valid e-mail address.")
		field.focus()
		return true
	}
}


//validate ASCII Character Set 
function charCheck(field, message) {
	var txt = field.value

	for (var i=0; i<txt.length; i++) {
		if (txt.charCodeAt(i) >= 128) {	
			alert(message) 
			field.focus()
			return true
		}
	}

	return false
}


//checks whether selected option has invalid value and selects option with gotoIndex
function checkInvalVal(sel, invalVal, gotoIndex) {
	if (GetSelVal(sel) == invalVal) sel.selectedIndex = gotoIndex
}


//date validation
function validDate(date, ctlDay) {
	return date.getDate() == GetSelVal(ctlDay)
}


//set select box value
function SetSelVal(ctl, newVal) {
	for (var i=0; i<ctl.length; i++) {
		if (ctl[i].value == newVal) {
			ctl[i].selected = true
			ctl.selectedIndex = i
			break
		}
	}
}


//get select box value
function GetSelVal(ctl) {
	var selIdx = ctl.selectedIndex
	return selIdx == -1 ? "" : ctl[selIdx].value
}


//check length of text in the passed control
function invalLength(ctl, ctlName, maxLen) {
	var ctlLen = trim(ctl.value).length

	if (ctlLen > maxLen) {
		alert("Please limit " + ctlName + " to " + maxLen + " characters.\nYou have used " + ctlLen + " characters.")
		ctl.focus()
		return true
	}

	return false
}


//open new window
function openWnd(url, name, height, width, directories, location, menubar, resizable, scrollbars, status, toolbar) {
	wnd = window.open(url, name, "alwaysRaised=1,height=" + height + ",width=" + width + ",directories=" + directories + ",locaton=" + location + ",menubar=" + menubar + ",resizable=" + resizable + ",scrollbars=" + scrollbars + ",status=" + status + ",toolbar=" + toolbar)
	wnd.focus()
}


//unencode url-encoded string
function URLDecode(urlStr) {
	return unescape(urlStr.replace(/\+/g, " "))
}


//remove all name/value pairs with the passed name from url-encoded querystring
function remQStringName(qString, name) {
	var i
	var qStringNew = ""

	if (qString != "") {
		var curName
		var arrNameVal = qString.split("&")

		for (i in arrNameVal) {
			curName = URLDecode(arrNameVal[i].split("=")[0])
			if (curName.toLowerCase() != name.toLowerCase()) qStringNew += "&" + arrNameVal[i]
		}
	}

	return qStringNew.substr(1)
}


//replace or add a name/value pairs in url-encoded querystring
function setQStringName(qString, name, arrVal) {
	var qStringNew = remQStringName(qString, name)
	var start = qStringNew == "" ? 1 : 0
	for (var i in arrVal) qStringNew += "&" + escape(name) + "=" + escape(arrVal[i])
	return qStringNew.substr(start)
}


//extract (first!) value from querystring for the passed name
function GetQSVal(qsName) {
	var qsPair
	var qString = location.search.substr(1)
	var arrNameVal = qString.split("&")

	for (var i in arrNameVal) {
		qsPair = arrNameVal[i].split("=")
		if (URLDecode(qsPair[0]).toLowerCase() == qsName.toLowerCase()) return URLDecode(qsPair[1])
	}

	return ""
}
