// getAnchorPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the page.
function getAnchorPosition(anchorname) {
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	// Logic to find position
 	if (use_gebi && document.all) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=AnchorPosition_getPageOffsetLeft(o);
		y=AnchorPosition_getPageOffsetTop(o);
		}
 	else if (use_css) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { found=1; break; }
			}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
			}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
		}
	else {
		coordinates.x=0; coordinates.y=0; return coordinates;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// getAnchorWindowPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the window
function getAnchorWindowPosition(anchorname) {
	var coordinates=getAnchorPosition(anchorname);
	var x=0;
	var y=0;
	if (document.getElementById) {
		if (isNaN(window.screenX)) {
			x=coordinates.x-document.body.scrollLeft+window.screenLeft;
			y=coordinates.y-document.body.scrollTop+window.screenTop;
			}
		else {
			x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
			y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
			}
		}
	else if (document.all) {
		x=coordinates.x-document.body.scrollLeft+window.screenLeft;
		y=coordinates.y-document.body.scrollTop+window.screenTop;
		}
	else if (document.layers) {
		x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
		y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
	}
function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
	}	
function AnchorPosition_getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
	}
function AnchorPosition_getWindowOffsetTop (el) {
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
	}
//******************************************************************************************
//END AnchorPosition Code
//******************************************************************************************


var trainerWindow;

trainerWindow = null;

function checkEmailAddress(field) {
	// Note: The next expression must be all on one line...
	//       allow no spaces, linefeeds, or carriage returns!
	var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.biz)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.info)|(\..{2,2}))$)\b/gi);

	if (goodEmail){
		return true;
	} else {
		alert('Please enter a valid e-mail address.')
		field.focus();
		field.select();
		return false;
	}
}

function displayMessageBox(text, e){
	var item = document.getElementById('messageBox');
	
	if( ! e ){
		e = window.event;
	}
	
	if( item ){
		//if( item.style.display != 'block' ){
			//item.innerHTML = text;
			item.document.write(text);
			if( item.style ){
				item.style.top = (e.y) + document.body.scrollTop;
				item.style.left = (e.x) + document.body.scrollLeft;
			}else{
				item.top = (e.y) + document.body.scrollTop;
				item.left = (e.x) + document.body.scrollLeft;
			}
			item.style.display = 'block';
		//}
	}
	
	item = null;
}

function hideMessageBox(){
	var item = document.getElementById('messageBox');
	
	item.style.display = 'none';
	item.style.top = 0;
	item.style.left = 0;
	
	item = null;
}

function sendToClipboard ( s ) {
	if( window.clipboardData && clipboardData.setData ) {
		clipboardData.setData("Text", s);
		return true;
	} else {
		alert("Internet Explorer required");
		return false;
	}
}


function trainerLookup(trainer_id, link){
	var URL;
	var linkPosition;
	var options;
	
	linkPosition = getAnchorWindowPosition(link.name);
	//linkPosition.x+=link.offsetLeft;
	//linkPosition.y+=link.offsetHeight;
	
	URL = 'trainer_lookup.php?current_trainer=' + trainer_id + '&display=trainer_txt&value=trainer_id';
	options = 'scrollbars,width=250,height=200,screenX=' + linkPosition.x + ',left=' + linkPosition.x + ',screenY=' + linkPosition.y + ',top=' + linkPosition.y + '';
	trainerWindow = window.open(URL, "trainer_window", options);
	
	return false;
}

function validateForm(frm){
	var i_element=0;
	var current_element;
	var test_value=0;
	var requirements;
	var value_name;
	
	for( i_element = 0; i_element < frm.elements.length; i_element++ ){
		current_element = frm.elements[i_element];
		
		requirements = current_element.title.toLowerCase().split(" ");
		
		value_name="";
		for( j = 1; j < requirements.length-1; j++ ){
			if( j > 1 ){
				value_name+=" ";
			}
			value_name+=requirements[j];
		}
		
		if( value_name.length == 0 ){
			value_name=requirements[requirements.length-1];
		}
		
		if( requirements.length > 1 && (requirements[0] == "required" || (current_element.value.length > 0)) ){
			switch( requirements[requirements.length-1] ){
				case "[date]":
					test_value = new Date(current_element.value);
					if( isNaN(test_value) ){
						alert('Please enter a valid ' + value_name + '.');
						current_element.select();
						current_element.focus();
						return false;
					}
				break;
				case "[datetime]":
					test_value = new Date(current_element.value);
					if( isNaN(test_value) ){
						alert('Please enter a valid ' + value_name + '.');
						current_element.select();
						current_element.focus();
						return false;
					}
				break;
				case "[e-mail]":
					if( ! checkEmailAddress(current_element) ){
						return false;
					}
				break;
				case "[numeric]":
					test_value = (current_element.value.toUpperCase() == current_element.value);
					if( test_value ){
						test_value = (current_element.value.toLowerCase() == current_element.value);
					}
					if( test_value == false || isNaN(Math.round(current_element.value)) ){
						alert('Please enter a valid ' + value_name + '.');
						current_element.select();
						current_element.focus();
						return false;
					}
				break;
				case "[string]":
					if( current_element.value.length == 0 ){
						alert('Please enter a valid ' + value_name + '.');
						current_element.select();
						current_element.focus();
						return false;
					}
				break;
				
				case "[other]":
						if(current_element.checked==true){
                            if(frm.txt_other.value.length==0){
                        alert('Please enter a valid ' + value_name + '.');
						current_element.select();
						current_element.focus();
						return false;}
					}
				
				break;
			}
		}
		test_value = null;
		current_element = null;
	}
	
	return true;
}

function window_onFocus() {
	if( trainerWindow != null ){
		if( ! trainerWindow.closed ){
			trainerWindow.close();
		}
		trainerWindow = null;
	}
}
