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_validateForm() { //v4.2 -- validates radioButtons, textBoxes and selectBoxes. for textboxes, can do email addresses ('isEmail') and Dates ('isDate' - in dd/mm/yyyy format)
  args = MM_validateForm.arguments;
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  var checkboxNames = "";
  var checkboxCount = 0;
  
  for (i=0; i<(args.length-2); i+=3) { 
  
	 test=args[i+2]; val=MM_findObj(args[i]);
	 if (val) { 
	    nm=val.name;
	    if (typeof(val.length)!="undefined"&&val.length>0&&typeof(val[0].name)!="undefined"){
		  nm=val[0].name; 
		  if(val[0].type=="radio") {
			var index;
			for(index = 0; index < val.length && !val[index].checked; index++);

			if (index == val.length && val[0] && isDisplayed(val[0])) errors+='- '+nm+' is required.\n';

		  } 
	    } 
	    else if (val.type=="select-one"){
	    	   if (val.selectedIndex == 0 && isDisplayed(val)) errors+='- '+nm+' is required.\n';
	    }
	    else if (val.type=="checkbox"){;
	    	   checkboxNames += "    - " + nm + "\n";
	    	   if (val.checked || !isDisplayed(val)) checkboxCount++;
	    }
	    else if (val.type=="text")  { 
		   val=val.value;
		  if (test.charAt(0) == 'R' && val.replace(/^\s+|\s+$/g,"")=="") {
			   errors += '- '+nm+' is required.\n';
		   }
		   else if (test.indexOf('isEmail')!=-1 && val != "") { 
			   p=val.indexOf('@');
			   if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
		   } 
		   else if (test.indexOf('isDate')!=-1 && val != "") {
			   if (val.length != 10) {
				   errors+='- '+nm+' must contain a date (dd/mm/yyyy).\n';
			   }
			   else {
				   var day=val.substring(0,2); var month=val.substring(3,5); var year=val.substring(6); var sep1=val.charAt(2); var sep2=val.charAt(5);				   
				   if (isNaN(day) || day > 31 || day < 1 || isNaN(month) || month > 12 || month < 1 || isNaN(year) || year.length != 4 || !isNaN(sep1) || !isNaN(sep2)) {
				   	errors+='- '+nm+' must contain a date (dd/mm/yyyy).\n';
				   }       				   
			   }
		   } 
		   else if (test!='R' && val != "") { 
			   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';
		   } 
		}				
    	  }
    	   
    	}
  } 
  if (checkboxNames.length > 0 && checkboxCount == 0) errors += "At least one of the following is required: \n" +  checkboxNames;
  if (errors) alert('The following error(s) occurred:\n'+replace(errors,"_"," "));
  document.MM_returnValue = (errors == '');
  return errors == '';
}

function submitAllCheckboxes()
{	
debugger;
	for (var i = 0; i < document.forms.length; i++)
	{
		form = document.forms[i];
		for (var j = 0; j < form.length; j++)
		{
			if (form[j].type && form[j].type == "checkbox") 
			{
				form[j].value = (form[j].checked ? "Yes" : "No");
				form[j].checked = true;
			}
		}
	}
}

function replace(sIn,cReplacee,cReplacer){//v1.0
//needed for MM_validateForm()v4.1
	var sArray = sIn.split(cReplacee);
	var sToReturn = sArray[0];
	for (var i = 1; i < sArray.length; i++)
	{
		sToReturn += cReplacer + sArray[i];
	}
	return sToReturn;
}

function isTelephone()
{
   var n = event.keyCode;   
   retVal = (event.ctrlKey || (n > 47 && n < 58) || (n > 95 && n < 106) || (n > 34 && n < 40) || n == 8 || n == 9 || n == 110 || n == 188 || n == 107 || n == 187 || n == 46);
   return retVal;
}

function isNumeric()
{
   var n = event.keyCode;
   retVal = (event.ctrlKey || (n > 47 && n < 58) || (n > 95 && n < 106) || (n > 34 && n < 40) || n == 8 || n == 9 || n == 110 || n == 188 || n == 46);
   return retVal;
}

function isDisplayed(elem){
//assumes that things are made visible and invisible only thorugh DIVs
	var parentElem = elem.parentElement;
	
	while (parentElem && parentElem.tagName != "DIV"){
		parentElem = parentElem.parentElement;		
	}
	
	return (parentElem && parentElem.style && parentElem.style.display != "none")
}


function BlankForm(Form)
{
	if (!Form) return;
	
	for (var i = 0; i < Form.elements.length; i++)
	{
		var Elem = Form.elements[i];
		var Type = Elem.type;
		
		if (Type == "select-one")
		{
			if (Elem.options.length > 0) Elem.options.selectedIndex = 0;
		}
		
		else if (Type == "text")
			Elem.value = "";
	}
}




