function isNumberKey(evt,type) {
	//Check for a decimal or integer
	if(!type) type = 'decimal';
	var typeResult = 1;
	var charCode = (evt.which) ? evt.which : event.keyCode;
	if(type == 'decimal' && charCode == 46) return true;
	if(type == 'date' && charCode == 47) return true; //allow forward slashes for dates
	if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;
	return true;
}

function isValidInt(s) {
	//Check for a valid integer
	var valid = false;
	if (s != null && s != "" && !isNaN(s)) valid = parseInt(s, 10) == s;
	return valid;
}

function roundNumber(number,decimals) {
	//if(!decimals) decimals = 2;
	if(typeof decimals == "undefined") decimals = 2;
	var newString;// The new rounded number
	decimals = Number(decimals);
	if (decimals < 1) {
		newString = (Math.round(number)).toString();
	} else {
		var numString = number.toString();
		if (numString.lastIndexOf(".") == -1) {// If there is no decimal point
			numString += ".";// give it one at the end
		}
		var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number
		var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we'll end up with
		var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want
		if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated
			if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point
				while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
					if (d1 != ".") {
						cutoff -= 1;
						d1 = Number(numString.substring(cutoff,cutoff+1));
					} else {
						cutoff -= 1;
					}
				}
			}
			d1 += 1;
		} 
		newString = numString.substring(0,cutoff) + d1.toString();
	}
	if (newString.lastIndexOf(".") == -1) {// Do this again, to the new string
		newString += ".";
	}
	var decs = (newString.substring(newString.lastIndexOf(".")+1)).length;
	for(var i=0;i<decimals-decs;i++) newString += "0";
	var newNumber = Number(newString);// make it a number
	//document.roundform.roundedfield.value = newString; // Output the result to the form field (change for your purposes)

	return newNumber;
}

// Limit by characters
function textCounter(field, countfield, maxlimit) {

if ( document.getElementById(field).value.length > maxlimit) // if the current length is more than allowed
document.getElementById(field).value =document.getElementById(field).value.substring(0, maxlimit); // don't allow further input
else
document.getElementById(countfield).value = maxlimit - document.getElementById(field).value.length;} // set the display field to remaining number

function textCounterWords(field, countfield, no_words) {
    var text=document.getElementById(field).value + " ";
    if(no_words>0)
    {
        var iwhitespace = /^[^A-Za-z0-9]+/gi; // remove initial whitespace
        var left_trimmedStr = text.replace(iwhitespace, "");
        var na = rExp = /[^A-Za-z0-9]+/gi; // non alphanumeric characters
        var cleanedStr = left_trimmedStr.replace(na, " ");
        var splitString = cleanedStr.split(" ");
        var word_count = splitString.length -1;
        document.getElementById(countfield).value=no_words-word_count;
    }
}

function slideDownCustom (targetID,duration,targetLinkID,text1,text2) {
	if(document.getElementById(targetID).style.display == 'none')
	{
	document.getElementById(targetLinkID).innerHTML = text1;
	Effect.SlideDown(targetID, { duration: duration, queue: 'end',queue: { position: 'end', scope: 'menuxscope', limit: 1 } });}
	else {

		document.getElementById(targetLinkID).innerHTML = text2;
	Effect.SlideUp(targetID, { duration: duration,queue: 'end',queue: { position: 'end', scope: 'menuxscope', limit: 1 } });
	}
	
	
}

function doClear(theText) {
     if (theText.value == theText.defaultValue) {
         theText.value = ""
     }
 }

// Creates a simulated event to handle ios touch events with standard mouse functions
function touchHandler(event)
{
    var touches = event.changedTouches,
        first = touches[0],
        type = "";
         switch(event.type)
    {
        case "touchstart": type = "mousedown"; break;
        case "touchmove":  type="mousemove"; break;        
        case "touchend":   type="mouseup"; break;
        default: return;
    }

             //initMouseEvent(type, canBubble, cancelable, view, clickCount, 
    //           screenX, screenY, clientX, clientY, ctrlKey, 
    //           altKey, shiftKey, metaKey, button, relatedTarget);

    var simulatedEvent = document.createEvent("MouseEvent");
    simulatedEvent.initMouseEvent(type, true, true, window, 1, 
                              first.screenX, first.screenY, 
                              first.clientX, first.clientY, false, 
                              false, false, false, 0/*left*/, null);

	first.target.dispatchEvent(simulatedEvent);
    event.preventDefault();
}

