/* js_functions.js */
function selectAllLocations(theForm)
{
	var theList = document.getElementById('lstLocations');
	if(theList)
	{
		for (i = 0; i < theList.options.length; i++)
		{
			 theList.options[i].selected = true;
		} 
	}
}

function moveSelectedOptions(from,to,action){
	for(var i=0;i<from.options.length;i++){
		var o = from.options[i];
		var dID = new Array;
		if(o.selected){
			to.options[to.options.length] = new Option( o.text, o.value, false, false);
		}
	}							
	for(var i=(from.options.length-1);i>=0;i--){
		var o = from.options[i];
		if(o.selected){
			from.options[i] = null;
		}
	}
}

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();
}

/******************** FORM VALIDATION *******************

Data types & their designators:
	-regular text	_t
	-email       	_e
	-number      	_n
	-date        	_d

*/

function ValidateForm(pForm){
	vForm = pForm;
	
	for(i=0;i<vForm.length;++i){
		vElement = vForm.elements[i];
		vValidationRule = vElement.id;
		vChar1 = vValidationRule.substr(0, 1);
		vChar2 = vValidationRule.substr(1, 1);
		
		if(vForm.rdoPageType == ""){
			alert("Please select a page type.")
			vForm.rdoPageType.focus();
			return false;
		}
		
		if(vValidationRule != "" && vValidationRule.length == 2){ 
			//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;
				}
			}
		}
	}
	
	return true;
}
