//short form for getElementById method
function e(id){
	
	return document.getElementById(id)
	
}



//alternates the background colour of each table row
function tableRowCol(){
	
	var tables = document.getElementsByTagName("table");
	
	for(i=0; i < tables.length; i++){
		
		if(tables[i].className == "main_table"){
			
			var rows = tables[i].rows;
	
			for(j=0; j < rows.length; j++){
				
				if(j % 2){
					rows[j].style.backgroundColor = '#ffffff';
				}else{
					rows[j].style.backgroundColor = '#eef2f9';
				}
				
			}
			
		}
		
	}
	
}



//alternates the row background colour of given table
function tableRows(table,colour1,colour2){
	
	var tables = document.getElementsByTagName("table");
	
	for(i=0; i < tables.length; i++){
		
		if(tables[i].className == table){
			
			var rows = tables[i].rows;
	
			for(j=0; j < rows.length; j++){
				
				if(j % 2){
					rows[j].style.backgroundColor = colour2;
				}else{
					rows[j].style.backgroundColor = colour1;
				}
				
			}
			
		}
		
	}
	
}



//jump menu for login
function jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}



//FEATURED LOGO POSITIONING
function featuredLogo(img,boxheight){
	
	var logoheight = e(img).height;
	var position = (boxheight-logoheight)/2;
	e(img).style.top = position+"px";
	
}



//SHOW OR HIDE BOXES
function showHide(show,hide){
	
	e(show).style.display = "block";
	e(hide).style.display = "none";
	
}



//clears contents of form element
function clearContents(obj){

	obj.value = "";
	obj.style.color = "#333333";
	
}



//adds password to form
function addPWD(){
	
	e("passwd").value = "Pa$$word";

}


//removes URL encoding
function URLDecode(id){

	e(id).href = unescape(e(id).href);

}



	



//function to show dropdown boxes
function showMenu(id){
	
	var id = document.getElementById(id)
	
	if(id.style.display == "block"){
		
		id.style.display = "none"
		
	} else {
		
		id.style.display = "block"
		
	}
	
}



//function to show/hide tabbed boxes
function showBox(id){
	
	var box = "box_" + id;
	var tab = "tab_" + id;
	var tabs = document.getElementsByTagName("a");
	var divs = document.getElementsByTagName("div");
	
	for(i=0; i<tabs.length; i++){
		
		if ((tabs[i].className == "tab")  &&  (e(tabs[i]) != tab)){
			
			tabs[i].style.background = "url(http://www.itac.ca/images/box_tab.gif)";
			tabs[i].style.color = "#7F7F7F";
			
		}
		
	}
	
	for(i=0; i<divs.length; i++){
		
		if ((divs[i].className == "content_box") &&  (e(divs[i]) != box)){
		
			divs[i].style.display = "none";
			
			
		}
		
	}

	//new Element.setOpacity( e(box), 0 );
	//Effect.Appear(e(box), { duration: 0.3, from: 0.0, to: 1.0 });
	
	e(tab).style.background = "url(http://www.itac.ca/images/box_tab_active.gif)";
	
	e(tab).style.color = "#454545";
	
	e(box).style.display = "block";
	
	
}



//function to show/hide tabbed boxes
function showBoxBlue(id){
	
	var box = "box_" + id;
	var tab = "tab_" + id;
	var tabs = document.getElementsByTagName("a");
	var divs = document.getElementsByTagName("div");
	
	for(i=0; i<tabs.length; i++){
		
		if ((tabs[i].className == "tab")  &&  (e(tabs[i]) != tab)){
			
			tabs[i].style.background = "url(http://www.itac.ca/images/tabs_blue/tab-bg.gif)";
			tabs[i].style.borderColor = "#aabbc3";
			tabs[i].style.color = "#F2FBFF";
			
		}
		
	}
	
	for(i=0; i<divs.length; i++){
		
		if ((divs[i].className == "tabs-blue-box") &&  (e(divs[i]) != box)){
		
			divs[i].style.display = "none";
			
			
		}
		
	}

	//new Element.setOpacity( e(box), 0 );
	//Effect.Appear(e(box), { duration: 0.3, from: 0.0, to: 1.0 });
	
	e(tab).style.background = "url(http://www.itac.ca/images/tabs_blue/tab-bg-selected.gif)";
	e(tab).style.borderColor = "#aaaaaa";
	e(tab).style.color = "#222222";
	
	e(box).style.display = "block";
	
	
}



// email address validation
function emailCheck(str) {

	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	   return false
	}
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}
	
	 if (str.indexOf(at,(lat+1))!=-1){
		return false
	 }
	
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	 }
	
	 if (str.indexOf(dot,(lat+2))==-1){
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		return false
	 }
	
	 return true					
}



// form validation
function validateField(form){
	
	//for each form element that is a text field and has a class of 'validate'
	
	var form = e(form);
	
	for (var n=0; n < form.elements.length; n++) {
		
		if((form.elements[n].type == "text") && (form.elements[n].className.match("validate"))){
			
			var field = form.elements[n];
			
			var p = document.createElement("p");
			
			p.className = "error";
			
			var errormsg = '<p class="error">field must be complete</p>';
	
			if ((field.value == null) || (field.value == "")){
				new Effect.Highlight(field, {startcolor:'#FFFF66', endcolor:'#ffffff'})
				field.focus()
				return false
			}
			
			//check for valid email address
			if(form.elements[n].name == "email"){
			
				if (emailCheck(field.value) == false){
					new Effect.Highlight(field, {startcolor:'#CC6666', endcolor:'#ffffff'})
					field.value="email address must be valid"
					field.focus()
					return false
				}
				
			}
			
		}
	}
		
	submitForm(form.id);
}



// multi-browser XMLHttpRequest function
function getXmlHttpObject(){
	var xmlHttp=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e)
		{
		try
		  {
		  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		catch (e)
		  {
		  return false;
		  }
		}
	  }
	return xmlHttp;
}


// function to submit form data to php processor
function submitForm(form){
	
	e('loaderbar').style.display = "block";
	
	var form = e(form);
	
	var formparent = form.parentNode.id;
	
	var params = "";
	
	//for each form element that is either a text or radio add element name and value to params variable
	for (var n=0; n < form.elements.length; n++) {
		if(form.elements[n].type == "text" || form.elements[n].type == "hidden"){
			params = params + form.elements[n].name + "=" + form.elements[n].value + "&";
		}else if(form.elements[n].type == "radio" && form.elements[n].checked){
			params = params + form.elements[n].name + "=" + form.elements[n].value + "&";
		}
	}
	
	xmlHttp=getXmlHttpObject();
	
	var url="http://www.itac.ca/includes/process.php?"+params;
	
	xmlHttp.open("GET",url,true);
	
	xmlHttp.onreadystatechange=displayResults;

	xmlHttp.send(null);
	
}


// function to return submitted form results page
function displayResults()
{ 
	if (xmlHttp.readyState==4)
	{ 
	document.getElementById("response").innerHTML=xmlHttp.responseText;
	}
}


/*
addPrintLink function by Roger Johansson, www.456bereastreet.com
*/
var addPrintLink = {
	init:function(sTargetEl,sLinkText) {
		if (!document.getElementById || !document.createTextNode) {return;} // Check for DOM support
		if (!document.getElementById(sTargetEl)) {return;} // Check that the target element actually exists
		if (!window.print) {return;} // Check that the browser supports window.print
		var oTarget = document.getElementById(sTargetEl);
		var oIcon = document.createElement('img');
		oIcon.src = '/images/main/icons/print.gif';
		oIcon.alt = '';
		var oLink = document.createElement('a');
		oLink.id = 'print-link'; // Give the link an id to allow styling
		oLink.href = '#'; // Make the link focusable for keyboard users
		oLink.appendChild(oIcon);
		oLink.appendChild(document.createTextNode(sLinkText));
		oLink.onclick = function() {window.print(); return false;} // Return false prevents the browser from following the link and jumping to the top of the page after printing
		//oTarget.appendChild(oIcon);
		oTarget.appendChild(oLink);
	},
/*
addEvent function included here for portability. Replace with your own addEvent function if you use one.
*/
/* addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html */
	addEvent:function(obj, type, fn) {
		if (obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() {obj["e"+type+fn](window.event);}
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	}
};
addPrintLink.addEvent(window, 'load', function(){addPrintLink.init('printlink',' Print');});