// Fix embedded objects in IE
var objects = document.getElementsByTagName("object");

for (var i=0; i<objects.length; i++)
    objects[i].outerHTML = objects[i].outerHTML;


// ********** COMMON JS FUNCTIONS  ********
function OpenPopup(pURL, pWidth, pHeight, pScrollbars){
	vWidth = pWidth;
	vHeight = pHeight;
	vleft=screen.width/2-(vWidth/2);
	vtop=screen.height/2-(vHeight/2)-27;
		
	vPopup = window.open(pURL, "popup", "toolbar=no,location=0,directories=no,status=no,menubar=0,scrollbars=" + pScrollbars + ",resizable=0,width="+vWidth+",height="+vHeight+",top="+vtop+",left="+vleft);
	vPopup.focus();
}

function ExpandCollapse(tr){
	var currentDisplay = document.getElementById(tr).style.display;
	if (currentDisplay != null && currentDisplay == 'none') {
	    document.getElementById(tr).style.display = '';
	} else {
	    document.getElementById(tr).style.display = 'none';
	}
}

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];}
}

//******************* FORM VALIDATION *******************
/**********************************************************

This file is a catch-all javascript validation for an HTML form.  Set the ID attribute of your form elements (namely textboxes and textareas) to the values outlined below, and use the ValidateForm function in the onsubmit event of your form, and all validation is taken care of for you.

To use this file, do the following:

	1.Include the file on your page by using the following line in the head:
		<script language="javascript" src="formvalidation.js"></script>
	
	2.Then in your form tag, add the following:
		onsubmit="return ValidateForm(this)"
	
	3.Note: if using an image to call the submit of the form, use the following line:
		<a href="javascript:if(ValidateForm(document.MyForm)){document.MyForm.submit();}">

	4.Set the ID attribute of your form elements to "xy", where x is the type of data in the field, and y designates if it is required or not.  Use "r" for required, "o" for optional.

Data types & their designators:
-regular text		_t
-email       		_e
-number      		_n
-telephone   		_p	//not handled yet
-web address 		_w	//not handled yet
-postal code 		_??	//not handled yet
-date        		_d

ie: -for a required textbox, the ID would be "tr"
    -for a required email field, the ID would be "er"
    -for an optional email field, the ID would be "eo"
    -etc...

**********************************************************/

function ValidateForm(pForm){
	vForm = pForm;
	
	for(i=0;i<vForm.length;++i){
		vElement = vForm.elements[i];
		vValidationRule = vElement.id;
		if(vValidationRule != ""){
		    vChar1 = vValidationRule.charAt(vValidationRule.length-2);
		    vChar2 = vValidationRule.charAt(vValidationRule.length-1);
		}
		
		alert(vValidationRule + ': ' + vChar1 + ': ' + vChar2);
		
		if(vValidationRule != ""){ 
			//check if element is required
			if(vChar2=="r" && vElement.value==""){
				alert("Please enter all required fields.");
				vElement.focus();
				return false;
			}
			
			//validate email address
			if(vChar1=="e"){
				vValidEmail = true;
				
				if(vElement.value.length>0){
					for(j=0; j<=vElement.value.length-1; ++j){
						if(vElement.value.charAt(j) == " "){vValidEmail = false;}
					}
						
					vCount = 0;
					for(k=0; k<=vElement.value.length-1; ++k){
						if(vElement.value.charAt(k) == "@"){++vCount;}
					}
					if(vCount != 1){vValidEmail = false;}
						
					if(vElement.value.indexOf("@") < 1){vValidEmail = false;}
					if(vElement.value.indexOf("@") == vElement.value.length-1){vValidEmail = false;}
					if(vElement.value.indexOf(".") < 1){vValidEmail = false;}
					if(vElement.value.indexOf(".") == vElement.value.length-1){vValidEmail = false;}
				}
				
				vInvalidChars = "!*&/\?+=)(^%$#:;";
				
				for(m=0; m<vInvalidChars.length; ++m){
					if(vElement.value.indexOf(vInvalidChars.charAt(m)) > -1){vValidEmail = false;}
				}
				
				if(vValidEmail==false){
					alert("Please enter a valid email address.");
					vElement.focus();
					return false;
				}
			}
			
			//validate number field
			if(vChar1=="n" && isNaN(vElement.value)==true){ 
				alert("You have entered an invalid number in a number field.");
				vElement.focus();
				return false;
			}
			
			//validate date field
			if(vChar1=="d" && isNaN(Date.parse(vElement.value))){ 
				if(!(vChar2=="o" && vElement.value=="")){
					alert("You have entered an invalid date in a date field.");
					vElement.focus();
					return false;
				}
			}
		}
	}
	
	alert(true);
	return true;
}

