// JavaScript Document

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_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_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_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 validate_form(f)
{
    var msg;
    var empty_fields = "";
	var overflow_fields = "";
    var errors = "";
	var focus_field = "";
	var form_data = "";
	var c = 0;

    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't 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,
    // then verify that they are numbers and that they are in the right range.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
        if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
            // first check if the field is empty
			if ((e.value == null) || (e.value == "") || isblank(e.value)) {
				if (!e.description){
					if(empty_fields.indexOf((e.name)) == -1) {
						empty_fields += "\n          " + e.name;
					}
//                	empty_fields += "\n          " + e.name;
				}
				else {
					if(empty_fields.indexOf((e.description)) == -1) {
						empty_fields += "\n          " + e.description;
					}
//					empty_fields += "\n          " + e.description;
				}
				if (focus_field == "") {
					focus_field = e;
				}
                continue;
            }

			// Now check for fields that are supposed to be dates.
			if(e.datefield)
			{
				if(!validate_date(e.value))
				{
					if (!e.description){
						if(errors.indexOf((e.name)) == -1) {
							errors += "\n- The field " + e.name + " must be a valid date";
						}
					}
					else {
						if(errors.indexOf((e.description)) == -1) {
							errors += "\n- The field " + e.description + " must be a valid date";
						}
					}
//					errors += "- The field " + e.name + " must be a valid date";
					if (focus_field == "") {
						focus_field = e;
					}
				}
			}

            // 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 (focus_field == "") {
							focus_field = e;
						}
					}
                    if (e.max != null && e.min != null) {
                        errors += " and less than " + e.max;
						if (focus_field == "") {
							focus_field = e;
						}
					}
                    else if (e.max != null) {
                        errors += " that is less than " + e.max;
						if (focus_field == "") {
							focus_field = e;
						}
					}
                    errors += ".\n";
                }
            }
        }
		if (((e.type == "text") || (e.type == "textarea")) && e.max_chars) {
			if (e.value.length > e.max_chars)
			{
				if (!e.description){
                	overflow_fields += "\n          " + e.name;
				}
				else {
					overflow_fields += "\n          " + e.description;
				}
			}
		}
		if (e.type == "radio" && !e.optional) {
			var bOneChecked = false;
	        var aRadios = document.getElementsByName(e.name);
	        for (var j = 0; j < aRadios.length; j++) {
              if (aRadios[j].checked) bOneChecked = true;
 	        }

    	     if (!bOneChecked) {
				 if (!e.description){
				 	if(empty_fields.indexOf((e.name)) == -1) {
                		empty_fields += "\n          " + e.name;
					}
				}
				else {
					if(empty_fields.indexOf((e.description)) == -1) {
						empty_fields += "\n          " + e.description;
					}
				}
				if (focus_field == "") {
					focus_field = e;
				}
			}
		}
    }

    // Now, if there were any errors, then display the messages, and
    // return true to prevent the form from being submitted.  Otherwise
    // return false
    if (!empty_fields && !errors && !overflow_fields) {
/*		for(var i = 0; i < f.length; i++) {
			var e = f.elements[i];
			if((e.type == "text") || (e.type == "textarea")) {
				if(!e.description) {
					form_data += e.name + "::" + e.value + "%%";
				}
				else {
					form_data += e.description + "::" + e.value + "%%";
				}
				c++;
			}
			else if((e.type == "radio") && (e.checked == true)) {
				if(!e.description) {
					form_data += e.name + "::" + e.value + "%%";
				}
				else {
					form_data += e.description + "::" + e.value + "%%";
				}
				c++;
			}	
		}
		f.form_data.value = form_data;
		f.element_count.value = c;*/
		return true;
	}

    msg  = "______________________________________________________\n\n"
    msg += "The form was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "______________________________________________________\n\n"

    if (empty_fields) {
        msg += "- The following field(s) have not been completed correctly:" 
                + empty_fields + "\n";
    }
	if(overflow_fields)
	{
		msg += "\n- The following field(s) contain too many characters:"
			+ overflow_fields + "\n";
	}
	if(errors){
		msg += "\n"
			+ errors;
	}
    alert(msg);
	if (focus_field != "") {
		focus_field.focus();
	}
    return false;
}

function validate_date(date)
{
	var ValidChars = "0123456789/";
	var IsNumber=true;
	var Char;

	for (i = 0; i < date.length && IsNumber == true; i++) 
    { 
      Char = date.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
      {
         IsNumber = false;
      }
   	}

	if(date.indexOf("/") < 0 || IsNumber == false)
	{
		return false;
	}

	var date_array = date.split("/");
	
	if(date_array.length < 3)
	{
		return false;
	}
	else
	{
		//Set up date elements for checking
		var day = parseFloat(date_array[0]);
		var month = parseFloat(date_array[1]);
		var year = parseFloat(date_array[2]);
		var now = new Date();
		var this_year = now.getFullYear()
		var max_year = this_year + 50;
		
		//Check for months and leap years
		if(day < 1 || day > 31 || month < 1 || month > 12 || year > this_year)
		{
			return false;
		}
		else if(month == 2 && ((year % 4 > 0 && day > 28) || day > 29))
		{
			return false;
		}
		else if((month == 4 || month == 6 || month == 9 || month == 11) && day > 30)
		{
			return false;
		}
		else if(day > 31)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}

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 is_email(str)
{
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp)
  {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function open_window(url)
{
	var features = "height=600,width=620,left=50,top=50,menubar=no,location=no,resizable=no,scrollbars=yes,status=no,toolbar=no";
	
	var new_win = window.open(url,"popup",features);
}

function image_popup(url)
{
	var features = "height=600,width=850,left=50,top=50,menubar=no,location=no,resizable=no,scrollbars=yes,status=no,toolbar=no";
	
	var new_win = window.open(url,"popup",features);
}