// JavaScript Document


function MM_jumpMenu(targ,selObj,restore){ //v3.0
	if(selObj == "--") return false;
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->


//Macromedia Dreamweaver Functions
function MM_findObj(n, d) { //v4.01
var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}




// all browser print //
function print_page(where){
var is_mac=(navigator.platform.indexOf("ac") != -1);
(document.all &&  is_mac)?
alert("Select  \"Print\" from the menu") : where? where.window.print() :
window.print();
}


function custom_print() {
    if (document.all) {
        if (navigator.appVersion.indexOf("5.0") == -1) {
            var OLECMDID_PRINT = 6;
            var OLECMDEXECOPT_DONTPROMPTUSER = 2;
            var OLECMDEXECOPT_PROMPTUSER = 1;
            var WebBrowser = "<OBJECT ID=\"WebBrowser1\" WIDTH=0 HEIGHT=0 CLASSID=\"CLSID:8856F961-340A-11D0-A96B-00C04FD705A2\"></OBJECT>";
            document.body.insertAdjacentHTML("beforeEnd", WebBrowser);
            WebBrowser1.ExecWB(6, 2);
            WebBrowser1.outerHTML = "";
        } else {
            self.print();
        }
    } else {
        self.print();
    }
}

function isblank(s)
{
	for (var i=0; i<s.length; i++)
	{
		var c = s.charAt(i);
		if ((c!= ' ') && (c!= '\n') && (c != '\t')) return false;
	}
	return true;
}

function isphonenumber(s)
{

	for (var i=0; i<s.length; i++)
	{
		var c = s.charAt(i);

		if (s.length > 12)
		{
			return false;
		}

		if ((c!= '(') && (c!= ')') && (c != '-') && ((c < '0') || (c > '9')))
		{
			return false;
		}
	}
	return true;
}

function iszipcode(s)
{
	for (var i=0; i<s.length; i++)
	{
		var c = s.charAt(i);
		if ((s.length < 5) || (s.length > 10))
		{
			return false;
		}
		if (s.length == 5)
		{
			if ((c < '0') || (c > '9'))
			{
				return false;
			}
		}
		else
		{
			if ((c!= '-') && ((c < '0') || (c > '9')))
			{
				return false;
			}
		}

	}
	return true;
}

function isemail(s)
{
	for (var i=0; i<s.length; i++)
	{
		var c = s.charAt(i);
		if ((c == '@')
			|| (c == '.')
			|| (c == '_')
			|| (c == '-')
			|| ((c.toUpperCase() >= 'A') && (c.toUpperCase() <='Z'))
			|| ((c >= '0') && (c <='9')))
		{
			continue;
		}
		else
		{
			return false;
		}
	}
	return true;
}

function iswebsite(s)
{
	for (var i=0; i<s.length; i++)
	{
		var c = s.charAt(i);
		if ((c == ':')
			|| (c == '.')
			|| (c == '/')
			|| (c == '_')
			|| (c == '-')
			|| ((c.toUpperCase() >= 'A') && (c.toUpperCase() <='Z'))
			|| ((c >= '0') && (c <='9')))
		{
			continue;
		}
		else
		{
			return false;
		}
	}
	if(s.length > 10)
	{
		return true;
	}
	else
	{
		return false;
	}
}


function isccnumber(s)
{
	if ((s.length < 16) && (s.length > 20))
	{
		return false;
	}
	for (var i=0; i<s.length; i++)
	{
		var c = s.charAt(i);
		if (((c < '0') || (c > '9')) && (c != ' ') && (c != '-'))
		{
			return false;
		}
	}
	return true;
}

function isvalidexpiration(s)
{
	today = new Date();
	expiredate = new Date();
	expiredate.setmonth(parseInt(s));
	expiredate.setdate(1);


	alert(today);
	alert(expiredate);
	if (today.getTime() < expiredate.getTime())
	{
		return false;
	}
	return true;
}


// This is the function that verifies all the fields on a form.

function validateFoundation(f)
{
	var msg;
	var empty_fields = "";
	var errors = "";
	var shipdif =0;
	this.nrselectones = 0;
	var funding = 0;
	var excluded = 0;


	//Loop through the elements of the form, looking for all
	//text and textarea elements that dont have an optional property
	//defined.  Then, check for fields that are empty and make a list of them.
	//Also, if any of these elements have a min or a max property defined,
	//put together error messages for fields that are wrong.
	//alert("in validateFoundations");
	for (var i=0; i< f.length; i++)
	{
		var e = f.elements[i];
		if (e.name == "tfFunding[]")
		{
				if (e.selectedIndex > 0)

				{

					funding++;

				}

		}

		if (e.type == "select-one")

		if (e.type == "select-one")
		{	this.nrselectones++;


	//alert(nrselectones+"---"+e.selectedIndex);


			//alert(e.selectedIndex);
			if (e.selectedIndex > 0)
			{
				continue;
			}
			else
			{
				if (this.nrselectones == 1)
				{
					empty_fields += "\n\tCounty";
				}
				else
				{
					if ((this.nrselectones > 1) && shipdif)
					{
			empty_fields += "\n\tstate";
						empty_fields += "\n\tshipstate";
					}
				}
				continue;
			}
		}


		if (e.type == "radio")
		{


		}


		if (((e.type == "text")
			|| (e.type == "textarea"))
			&& !e.optional)
		{
			//Check for empty field
			if ((e.value == null) || (e.value == "") || isblank(e.value))
			{
				if(e.name != "tfFunding[]" && e.name != "tfExcluded[]" && e.name != "tfDeadline[]")
				{
					if(e.name == "tfName")
					{
						empty_fields += "\n\tFoundation Name";
					}
					if(e.name == "tfAddress1")
					{
						empty_fields += "\n\tAddress";
					}
					if(e.name == "tfCity")
					{
						empty_fields += "\n\tCity";
					}
					if(e.name == "tfState")
					{
						empty_fields += "\n\tState";
					}
					if(e.name == "tfZip")
					{
						empty_fields += "\n\tZip Code";
					}
					if(e.name == "tfTelephone")
					{
						empty_fields += "\n\tTelephone";
					}
					if(e.name == "tfFax")
					{
						empty_fields += "\n\tFax";
					}
					if(e.name == "tfContactName")
					{
						empty_fields += "\n\tContact Name";
					}
					if(e.name == "tfContactEmail")
					{
						empty_fields += "\n\tEmail";
					}
					if(e.name == "tfWebSite")
					{
						empty_fields += "\n\tWeb Site";
					}
					if(e.name == "tfGuidlines")
					{
						empty_fields += "\n\tGuidlines";
					}
					if(e.name == "tfKeywords")
					{
						empty_fields += "\n\tKey Words";
					}
				}

				continue;
			}
			if (e.name == "tfTelephone" || e.name=="tfFax")
			{
				if (!isphonenumber(e.value))
				{
					errors += "- The phone/fax number is not formatted properly\n";
					errors += "            336-725-1621 or 3367251621\n";
				}
				continue;
			}

			if (e.name == "tfZip")
			{
				if (!iszipcode(e.value))
				{
					errors += "- The zip code is not formatted properly\n";
					errors += "            27104 or 27104-1234\n";
				}
				continue;
			}

			if (e.name == "tfFunding[]")
			{
				funding++;
				continue;
			}

			if (e.name == "tfContactEmail")
			{
				if (!isemail(e.value))
				{
					errors += "- The email address does not appear to be  formatted properly\n";
				}
				continue;
			}

			if (e.name == "tfWebSite")
			{
				if (!iswebsite(e.value))
				{
					errors += "- The website address does not appear to be  formatted properly\n";
				}
				continue;
			}

			//Now check for fields that are supposed to be numeric
			if (e.numeric || (e.min != null) || (e.max != null))
			{
				var v = parseFloat(e.value);
				if (isNaN(v) ||
					((e.min != null) && (v < e.min)) ||
					((e.max != null) && (v > e.max)))
				{
					errors += "- The field " + e.name + " must be a number";
					if (e.min != null)
					{
						errors += " that is greater than " + e.min;
					}
					if (e.max != null && e.min != null)
					{
						errors += " and less than " + e.max;
					}
					else
					{
						if (e.max != null)
						{
							errors += "that is less than " + e.max;
						}
					}
				}
			}
		}
	}

	if (funding == 0)
	{
		errors += "- You must enter at least one funding area\n";
	}
	// Display error message if it exists.

	if (!empty_fields && !errors)
	{
		document.f.submit();

	}

	msg = "________________________________________________________\n\n";
	msg += "The form was not submitted because of the following error(s)\n";
	msg += "Please correct these errors(s) and re-submit.\n";
	msg += "________________________________________________________\n\n";

	if (empty_fields)
	{
		msg += "The following required fields are empty:"
			+ empty_fields + "\n";
	}
	if (errors)
	{
		msg += "\n";
	}

	msg += errors;
	alert(msg);
	return false;
}