var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height, menu) {
	if(popUpWin) if(!popUpWin.closed) popUpWin.close();

	if(menu == undefined) menu = "no";

	popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=yes,menubar='+menu+',scrollbars=auto,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
	popUpWin.focus()
}

function validateForm() {
	var given_name = document.getElementById('given_name').value;
	var family_name = document.getElementById('family_name').value;
	var email = document.getElementById('email').value;
	var phone_number = document.getElementById('phone_number').value;
	var country = document.getElementById('country').selectedIndex;
	var messageBody = document.getElementById('messageBody').value;
	
	var messageString = "";
	if(given_name == "") messageString = messageString + "Given name is empty<br>"; 
	if(family_name == "") messageString = messageString + "Family name is empty<br>";
	if(email == "") messageString = messageString + "Email Address is empty<br>";
	if(phone_number == "") messageString = messageString + "Phone number is empty<br>";
	if(country == 0) messageString = messageString + "Country not selected<br>";
	if(messageBody == 0) messageString = messageString + "Message not provided<br>";
		
	if(messageString != "") {
		document.getElementById('jValidateArea').innerHTML = "<h3>Errors:</h3><br>"+messageString;
		return false;
	} else return true;
}

function getSel() {
	var browser=navigator.appName
	var b_version=navigator.appVersion

	if ((browser=="Microsoft Internet Explorer") && b_version>='4') {
   		var str = document.selection.createRange().text;
     		document.forms.pageForm.contentTextArea.focus();
     		var sel = document.selection.createRange();
     		return str;
	} else {
   		field = document.getElementById('contentTextArea');
   		startPos = field.selectionStart;
   		endPos = field.selectionEnd;
   		before = field.value.substr(0, startPos);
   		selected = field.value.substr(field.selectionStart, (field.selectionEnd - field.selectionStart));
   		after = field.value.substr(field.selectionEnd, (field.value.length - field.selectionEnd));
		return selected;
   	}
}

function setSel(text) {
	var browser=navigator.appName
	var b_version=navigator.appVersion

	if ((browser=="Microsoft Internet Explorer") && b_version>='4') {
   		var str = document.selection.createRange().text;
     		document.forms.pageForm.contentTextArea.focus();
     		var sel = document.selection.createRange();
     		sel.text = text;
	} else {
   		field = document.getElementById('contentTextArea');
   		startPos = field.selectionStart;
   		endPos = field.selectionEnd;
   		before = field.value.substr(0, startPos);
   		selected = field.value.substr(field.selectionStart, (field.selectionEnd - field.selectionStart));
   		after = field.value.substr(field.selectionEnd, (field.value.length - field.selectionEnd));
		field.value = before + "" + text + "" + after;
   	}
}


/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}


var not_whitespace = new RegExp(/[^\s]/);//This can be given inside the funciton - I made it a global variable to make the scipt a little bit faster.
var parent_count;
//Process the xml data
function xml2array(xmlDoc,parent_count) {
	var arr;
	var parent = "";
	parent_count = parent_count || new Object;

	var attribute_inside = 0; /*:CONFIG: Value - 1 or 0
	*	If 1, Value and Attribute will be shown inside the tag - like this...
	*	For the XML string...
	*	<guid isPermaLink="true">http://www.bin-co.com/</guid>
	*	The resulting array will be...
	*	array['guid']['value'] = "http://www.bin-co.com/";
	*	array['guid']['attribute_isPermaLink'] = "true";
	*	
	*	If 0, the value will be inside the tag but the attribute will be outside - like this...	
	*	For the same XML String the resulting array will be...
	*	array['guid'] = "http://www.bin-co.com/";
	*	array['attribute_guid_isPermaLink'] = "true";
	*/

	if(xmlDoc.nodeName && xmlDoc.nodeName.charAt(0) != "#") {
		if(xmlDoc.childNodes.length > 1) { //If its a parent
			arr = new Object;
			parent = xmlDoc.nodeName;
			
		}
	}
	var value = xmlDoc.nodeValue;
	if(xmlDoc.parentNode && xmlDoc.parentNode.nodeName && value) {
		if(not_whitespace.test(value)) {//If its a child
			arr = new Object;
			arr[xmlDoc.parentNode.nodeName] = value;
		}
	}

	if(xmlDoc.childNodes.length) {
		if(xmlDoc.childNodes.length == 1) { //Just one item in this tag.
			arr = xml2array(xmlDoc.childNodes[0],parent_count); //:RECURSION:


		} else { 
			var index = 0;

			for(var i=0; i<xmlDoc.childNodes.length; i++) {//Go thru all the child nodes.
				var temp = xml2array(xmlDoc.childNodes[i],parent_count); //:RECURSION:
				if(temp) {
					var assoc = false;
					var arr_count = 0;
					for(key in temp) {
						if(isNaN(key)) assoc = true;
						arr_count++;
						if(arr_count>2) break;//We just need to know wether it is a single value array or not
					}

					if(assoc && arr_count == 1) {
						if(arr[key]) {	//If another element exists with the same tag name before,
										//		put it in a numeric array.
							//Find out how many time this parent made its appearance
							if(!parent_count || !parent_count[key]) {
								parent_count[key] = 0;

								var temp_arr = arr[key];
								arr[key] = new Object;
								arr[key][0] = temp_arr;
							}
							parent_count[key]++;
							arr[key][parent_count[key]] = temp[key]; //Members of of a numeric array
						} else {
							parent_count[key] = 0;
							arr[key] = temp[key];
							if(xmlDoc.childNodes[i].attributes.length) {
								for(var j=0; j<xmlDoc.childNodes[i].attributes.length; j++) {
									var nname = xmlDoc.childNodes[i].attributes[j].nodeName;
									if(nname) {
										/* Value and Attribute inside the tag */
										if(attribute_inside) {
											var temp_arr = arr[key];
											arr[key] = new Object;
											arr[key]['value'] = temp_arr;
											arr[key]['attribute_'+nname] = xmlDoc.childNodes[i].attributes[j].nodeValue;
										} else {
										/* Value in the tag and Attribute otside the tag(in parent) */
											arr['attribute_' + key + '_' + nname] = xmlDoc.childNodes[i].attributes[j].nodeValue;
										}
									}
								} //End of 'for(var j=0; j<xmlDoc. ...'
							} //End of 'if(xmlDoc.childNodes[i] ...'
						}
					} else {
						arr[index] = temp;
						index++;
					}
				} //End of 'if(temp) {'
			} //End of 'for(var i=0; i<xmlDoc. ...'
		}
	}

	if(parent && arr) {
		var temp = arr;
		arr = new Object;
		
		arr[parent] = temp;
	}

	//alert(dump(arr));
	return arr;
}

function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
 for(var item in arr) {
  var value = arr[item];
 
  if(typeof(value) == 'object') { //If it is an array,
   dumped_text += level_padding + "'" + item + "' ...\n";
   dumped_text += dump(value,level+1);
  } else {
   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  }
 }
} else { //Stings/Chars/Numbers etc.
 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
} 

function ajRequest(form, elm, name, value, file) {
	var xmlHttp;
	var formVariables = "";
	var extraVariables = "";

	if(name != '' && value != '') extraVariables = "&"+name+"="+value;

	if(form != '') {
		if(typeof form == "string") form = document.getElementById(form);

		for(var i=0; i < form.elements.length; i++) {
			if(form.elements[i].type == "checkbox" || form.elements[i].type == "radio") {
				if(form.elements[i].checked) {
					if(i != 0) formVariables += "&"+form.elements[i].name+"="+form.elements[i].value;
					else formVariables = form.elements[i].name+"="+form.elements[i].value;
				}
			} else {
				if(i != 0) formVariables += "&"+form.elements[i].name+"="+form.elements[i].value;
				else formVariables = form.elements[i].name+"="+form.elements[i].value;
			}
		}
	}
	
	if(form != '') var params = formVariables+extraVariables;
	else  var params = extraVariables;

	try { xmlHttp=new XMLHttpRequest(); }
	catch (e)  {
		var msxmlhttp = new Array(
		'Msxml2.XMLHTTP.5.0',
		'Msxml2.XMLHTTP.4.0',
		'Msxml2.XMLHTTP.3.0',
		'Msxml2.XMLHTTP',
		'Microsoft.XMLHTTP');

		for (i=0;i<msxmlhttp.length;i++) {
			try { xmlHttp = new ActiveXObject(msxmlhttp[i]); }
			catch (e) { xmlHttp = null; }
		}
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4 && xmlHttp.status == 200) {
			var str = xmlHttp.responseText;
			if(document.getElementById(elm)) {
				if(file) {
					if(str == "/restricted/") {
						window.parent.location.href=str;
						fileWin.focus();
					}
					if(str == "/unrestricted/") {
						window.parent.location.href=str;
						fileWin.focus();
					} else  {
						document.getElementById(elm).innerHTML = str;
						fileWin.close();
					}
				} else document.getElementById(elm).innerHTML = str;
			}
		}
	}

	xmlHttp.open("POST", "ajax", true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.send(params);
}

function isArray(obj) {

   if (obj.constructor.toString().indexOf("Object") == -1)
      return false;
   else
      return true;
}

function insertStyle(style) {
	var tempText = getSel();
	var returnText = "<span class='"+style+"'>"+tempText+"</span>";
	setSel(returnText);
}

function insertTag(tagname) {
	var tempText = getSel();
	var returnText = "<"+tagname+">"+tempText+"</"+tagname+">";
	setSel(returnText);
}

function validateFormATLASAccessRequestForm()
{

var lastName = document.getElementById('lastName').value;
var firstName = document.getElementById('firstName').value;
var streetAddress = document.getElementById('streetAddress').value;
var city = document.getElementById('city').value;
var state = document.getElementById('state').value;
var postCode = document.getElementById('postCode').value;
var country = document.getElementById('country').value;
var email = document.getElementById('email').value;
var avondaleIDNumber = document.getElementById('avondaleIDNumber').value;
var graduateYear = document.getElementById('graduateYear').value;
if(lastName =="" || firstName =="" || streetAddress =="" || city =="" || state =="" || postCode =="" || country =="" || email =="")
{
alert("Please make sure that all fields are filled in");
}
else
{
document.aTLASAccessRequestForm.submit();
}
}

function displayDetail() {
	var chosenOption = document.getElementById('mediaType').selectedIndex;
	hideDiv('bookDiv');
	hideDiv('serialDiv');

	if(chosenOption == 0) {
		showDiv('nothingChosenDiv');
		hideDiv('continueDiv');
	} else {
		hideDiv('nothingChosenDiv');
		showDiv('continueDiv');
	}
		
	if(document.getElementById('mediaType').value == "Book") showDiv('bookDiv');
	if(document.getElementById('mediaType').value == "Serial") showDiv('serialDiv');
}

function openday() {
	var opt = document.getElementById('regType').selectedIndex;
	hideDiv('indDiv');
	hideDiv('schDiv');
	hideDiv('caDiv');

	if(opt == 1) showDiv('indDiv');
	else if(opt == 2) showDiv('schDiv');
	else if(opt == 3) showDiv('caDiv');
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) oldonload();
			func();
		}
	}
}

function updateSCAmount() {
	total_amount = 0;

	if(document.getElementById('myob1').checked) total_amount += 250;
	if(document.getElementById('myob2').checked) total_amount += 250;
	if(document.getElementById('comp1').checked) total_amount += 250;
	if(document.getElementById('comp2').checked) total_amount += 250;
	if(document.getElementById('net1').checked) total_amount += 250;
	if(document.getElementById('net2').checked) total_amount += 250;
	if(document.getElementById('word1').checked) total_amount += 250;
	if(document.getElementById('word2').checked) total_amount += 250;

	document.getElementById('totalAmount').innerHTML = total_amount;
}

function updateStudentSurvey() {
	var element = document.getElementById('surveyDiv');

	if(document.getElementById('spec_prov_yes').checked) showDiv(element);
	else if(document.getElementById('spec_prov_no').checked) hideDiv(element);
}

function hideCourseTabs() {
	for(var i=1;i<=5;i++) {
		var c = "courseTab"+i;
		var e = "block"+i
		hideDiv(e);
		if(document.getElementById(c)) {
			str = document.getElementById(c);
			while(str.innerHTML.indexOf("_active.gif") != -1)
				str.innerHTML = str.innerHTML.replace("_active.gif", "_base.gif");
		}
	}
}

function showCourseTab(id) {
	hideCourseTabs();

	var c = "courseTab"+id;
	var e = "block"+id;
	showDiv(e);
	if(document.getElementById(c)) {
		str = document.getElementById(c);
		while(str.innerHTML.indexOf("_base.gif") != -1)
			str.innerHTML = str.innerHTML.replace("_base.gif", "_active.gif");
	}
}

function hideAnchors() {
	hideDiv('anchors');
	showDiv('blocks');
}

function hideTabs(cl, l) {
	for(var i=1;i<=l;i++) {
		var c = cl.concat(i);
		var e = "block"+i;
		hideDiv(e);
		if(document.getElementById(c)) {
			str = document.getElementById(c);
			while(str.innerHTML.indexOf("_active.gif") != -1)
				str.innerHTML = str.innerHTML.replace("_active.gif", "_base.gif");
		}
	}
}

function showTab(cl, id, limit) {
	if(limit == '')
		limit = 0;
	hideTabs(cl, limit);

	var c = cl.concat(id);
	var e = "block"+id;
	showDiv(e)
	if(document.getElementById(c)) {
		str = document.getElementById(c);
		while(str.innerHTML.indexOf("_base.gif") != -1)
			str.innerHTML = str.innerHTML.replace("_base.gif", "_active.gif");
	}
}

function hideDiv(id) {
	if(typeof id == "string" && document.getElementById(id))
		e = document.getElementById(id);
	else e = id;
	if(e) e.style.display = 'none';
}

function showDiv(id) {
	if(typeof id == "string" && document.getElementById(id))
		e = document.getElementById(id);
	else e = id;
	if(e) e.style.display = 'block';
}

function expand(id, b, s, h) {
	e = (typeof id == "string" && document.getElementById(id) ? document.getElementById(id) : id);
	if(e) {
		if(e.style.display == '' || e.style.display == 'block') {
			hideDiv(e);
			if(b) b.innerHTML = s;
		} else {
			showDiv(e);
			if(b) b.innerHTML = h;
		}
	}
}

function etri_menu(id) { expand("c"+id);expand("e"+id);expand("cc"+id); }
