/*basic page operation fundtions for all pages*/
function disable(id) {
	var control = window.document.getElementById(id);
	if(control)
		control.disabled = true;
}

function popWin(url, name, features) {
	var win = window.open(url, name, features);
	win.focus();
}

function popGeneric(url) {
	popWin(url, "generic", "height=420,width=500,resizable=1,scrollbars=1,menubar=1,toolbar=1,location=1,status=1");
}

function popForgot(url) {
	popWin(url, "forgot", "resizable,height=160,width=310");
}

function say(s) {
	alert(s);
	return false;
}

function swapOrgImg(src) {
	document.orgImg.src = src;
}

function swapFrImg(src) {
	document.frImg.src = src;
}

function checkOtherRadio() {
	var el = document.getElementById("otherRadio");
	el.checked = true;
}

function otherRadioIsChecked() {
	return document.getElementById("otherRadio").checked;
}

function toggleChecks() {
	var el = document.donForm.checks;
	if (el) {
		var toggles = document.donForm.toggles;
		var status = (toggles.value % 2 == 0) ? true : false;
		for (var i = 0; i < el.length; i++) el[i].checked = status;
		toggles.value++;
	}
}

function getAnswerEl(x) {
	var el = document.getElementById("answer" + x);
	return el;
}

function getFocus(el) {
	if (el.type == "text" || el.type == "textarea" || el.type == "password" || el.type == "file") el.select(); 
	if (el.type != "hidden") el.focus();
}

function toggleFAQ(el) {
	var x = 1;
	var ans = getAnswerEl(x);
	if (el.checked) {
		while(ans) {
			ans.style.display = "inline";
			ans = getAnswerEl(++x);
		}
	}
	else {
		while(ans) {
			ans.style.display = "none";
			ans = getAnswerEl(++x);
		}	
	}
}

function toggleAnswer(x) {
	var el = getAnswerEl(x);
	if (el.style.display == "inline") el.style.display = "none";
	else el.style.display = "inline";
}

function submitForm(action) {
	document.donForm.action.value = action;
	document.donForm.submit();
}

function getCheckedValues() {
	var el = document.donForm.checks;
	var s = "";
	if(el.length) {
		for (var i = 0; i < el.length; i++) {
			if (el[i].checked) s += "," + el[i].value;
		}
	} else {
		if (el.checked) s += "," + el.value;
	}
	return s.substring(1);
}

//basic check fundtions for all pages like validation*

var whitespace = " \t\n\r";
var keyExp = /^[a-zA-Z0-9_\-]*$/;
var daysInMonth = new Array();

daysInMonth[1] = 31;
daysInMonth[2] = 29;
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

function leftTrim(s) {
	var i = 0;
	while (i < s.length && whitespace.indexOf(s.charAt(i)) != -1) i++;
	return s.substring(i, s.length);
}

function rightTrim(s) {
	var i = s.length;
	while (i > 0 && whitespace.indexOf(s.charAt(i - 1)) != -1) i--;
	return s.substring(0, i);
}

function trim(s) {
	return rightTrim(leftTrim(s));
}

function isEmpty(s) {
	return trim(s).length == 0;
}

function isNumeric(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK; 
	return !isNaN(s);
}

function isInteger(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
    return !isNaN(s) && s.indexOf(".") == -1;
}

function isPositive(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	return !isNaN(s) && s > 0;
}

function isNegative(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	return !isNaN(s) && s < 0;
}

function isNonNegative(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	return !isNaN(s) && s >= 0;
}

function isNonPositive(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	return !isNaN(s) && s <= 0;
}

function isInRange(s, min, max, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	return !isNaN(s) && s >= min && s <= max;
}

function isYear(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	return isInteger(s) && isInRange(s, 1753, 9999);
}

function isMonth(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	return isInteger(s) && isInRange(s, 1, 12);
}

function isDay(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	return isInteger(s) && isInRange(s, 1, 31);
}

function isDate(year, month, day) {
	if (!isYear(year) || !isMonth(month) || !isDay(day)) return false;  
    if (day > daysInMonth[month]) return false;
    if (month == 2 && day > daysInFebruary(year)) return false;
    return true;
}

function isFutureDate(year, month, day) {
	if (!isDate(year, month, day)) return false;  
	var now = new Date();
	var then = new Date(year, month - 1, day);
	if (then.getTime() < now.getTime()) return false;
	return true;
}

function isStringDate(s) {
	var datetime = s.split(" ");
	var date = datetime[0].split("/");
	return isDate(date[2], date[0], date[1]);
}

function daysInFebruary(year) {
    return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 29 : 28;
}

function isChecked(el) {
	if(el.length) {
		for (var i = 0; i < el.length; i++) {
			if (el[i].checked) return true;
		}
	} else {
		if (el.checked) return true;
	}
	return false;
}

function isURL(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	s = trim(s);
	if (s.length < 8 || s.substring(0, 7) != "http://") return false;
	return true;
}

function isEmail(s, emptyOK) {
	var REG = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$/;
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
		s = trim(s);
	if (s.indexOf(" ") != -1) return false;
		var i = 1;
		while (i < s.length && s.charAt(i) != "@") i++;
		if (i >= s.length || s.charAt(i) != "@") return false;
			i += 2;
		while (i < s.length && s.charAt(i) != ".") i++;
			if (i >= s.length - 1 || s.charAt(i) != ".") return false;
			if (s.match(REG))  {
			    return true;
			} //end if
			else { 
				return false;
		    } //end if	
	
	return true;
}

function isEmailList(s, field) {
	s = trim(s);
	var a = new Array();
	var cList = s.split(",");
	var sList = s.split(";");
	var rList = s.split("\r\n");
	if (cList.length > sList.length && cList.length > rList.length) a = cList;
	else if (rList.length > cList.length && rList.length > sList.length) a = rList;
	else a = sList;
	var b = removeEmptyElements(a);
	for (var i in b) {
		if (!isEmail(b[i])) return false;
	}
	if (field) field.value = b.join(";");
	return true;
}

function isZip(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	if ((s.length == 5 && isInteger(s)) || (s[5]=='-'&& isInteger(s.substring(0,5))))
    return true;
	return false;
}

function isKey(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	return keyExp.test(s);
}

function isLongerThan(s, c) {
	if (s.length > c) return true;
	return false;
}

function removeEmptyElements(a) {
	var b = new Array();
	for (var i in a) {
		if (!isEmpty(a[i]))
			b[b.length] = trim(a[i]);
	}
	return b;
}

//included in campaign_display plugin.*********
//Supplimentary function for check functions like **local
//Ensures the user enters a valid numeric value that can include decimals ie (4.52)
function CheckDecimal(number, field) {
	var REG = /^(\d\d*|\d\d*\.\d\d?)$/;
	if (number.match(REG))
	{
		return true;
	}		 //end if
	else
	{
		alert('You have entered an invalid number.\r\nValid input is in format: 10, 10.5 or 10.50');
		return false;
	} //end else
} //end CheckDecimal

function doContinue() {
	if (validateForm()) {
		submitForm("continue");
	return true;
	}
	return false;
}

function checkChecked() {
	var form = document.donForm;
	var checked = false;
	for(i = 0; i < form.sug.length; i++) {
		if(form.sug[i].checked) {
			checked = true;
		}
	}
	return checked;
}

function validateForm() {
var bOK = false;
var REG = /^(\d\d*|\d\d*\.\d\d?)$/;
	var form = document.donForm;

	if (form.sug.length > 0 ) 
	{
        if (!checkChecked())
        {
			alert("Please input an amount to proceed!");
			getFocus(form.sug[0]);
			return false;
        }
		if (form.sug[form.sug.length - 1].checked && !isPositive(form.amt.value))
		{
			alert("Please ensure your donation to be a positive dollar amount!");
			getFocus(form.amt);
			return false;
		}
	}

	if ((form.sug.length == 0 || form.sug.value=='other') && !isPositive(form.amt.value)) 
	{
		alert("Please ensure your donation to be a positive dollar amount!");
		getFocus(form.amt);
		return false;
	}
	return true;
}

function SelectFundraiser(field) {
	window.open("/donate/" + field.options[field.selectedIndex].value,"win");
}	

function focusontext()
{
	document.getElementById("amt").focus();
	return;
} 

