// JavaScript Document

var ie7_Options = new Array(); // 

function include(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}





function removeNode(node) {
	node.parentNode.removeChild(node);
}
function $id(id) {
	return document.getElementById(id);	
}

function $tag(name) {
	return document.getElementsByTagName(name);	
}
function $class (className) {
  var all = document.all ? document.all : document.getElementsByTagName('*');
  var elements = new Array();
  for (var e = 0; e < all.length; e++) {
    if (all[e].className == className) {
      elements[elements.length] = all[e];
    }
  }
  return elements;
}

Array.prototype.trim = function() {
	ar = Array();
	for(i=0;i<this.length;i++) {
		if(this[i] != "") {
			ar.push(this[i]);
		}
	}
	
	return ar;
}


 function doOther() {

 	if(this.value == "other") {
	 	x = this;
		id = x.id;
		n = x.name;
		if(n == "") {
			n = id;
		}
		p = x.parentNode;
		p.removeChild(x);
		p.innerHTML += "<input name=\"" + n + "\"  id=\"" + id + "\" type=\"text\"/>";		
		document.getElementById(id).select();
 	} else {
 		return false;
 	}
}

function easyCheckDate(node) {
	//assumes that the child nodes	
	dateItems = node.getElementsByTagName("select");
	if(!checkDate(dateItems.item(0).value,dateItems.item(1).value, dateItems.item(2).value)) {
		node.getElementsByTagName("span").item(0).innerHTML = "Incorect date";	
	} else {
		node.getElementsByTagName("span").item(0).innerHTML = "";			
	}
}

function checkDate(day,month,year) {
	thisDate = new Date(year,month-1,day);	
	if(thisDate.getDate() != day || thisDate.getMonth() + 1 != month || thisDate.getFullYear() != year) {
		return false;
	} else {
		return true;	
	}
}

function limitText(textarea,count) {
	
	if (textarea.value.length > 255) {
		textarea.value = textarea.value.substring(0, 255);
	} else {
		
		count.innerHTML = 255 - textarea.value.length;
	}
}

function sendMessage() {
	request = new HttpRequest();
	request.addHeader("Content-Type", "text/html");
	request.onComplete = function(response) {
		alert(response.text);
	}
	request.post("/message/send","username=" + $id("messageTo").value + "&subject=" + $id("messageSubject").value + "&message=" + $id("messageText").value);	
}


function onLoad(type) {
	nodes = $tag("iframe");
	
	for(i = 0; i < nodes.length; i++) {
		
		if(nodes[i].src.indexOf("/add") > 0) {
		//	alert("TEST");
			
			nodes[i].src += "&r=" + (Math.random()*10);
		}
		//n = nodes[i].src.split("/");
		//n.trim();
		//alert(n);
	//	alert(nodes[i].src.split("/")[3].substring(0,3));
		
		
	}
	if(type == "event") {
		if($id("country")) {
			$id("country").onchange = eventChangeURL;
		}
		if($id("province")) {
			$id("province").onchange = eventChangeURL;
		}	
		if($id("town")) {
			$id("town").onchange = eventChangeURL;
		}
		if($id("venue")) {
			$id("venue").onchange = eventChangeURL;
		}			
	}
}

function eventChangeURL() {

	path = "/event";
	if($id("country")) {
		path += "/" + $id("country").value;
	}
	if(this.id != "country") {	
		if($id("province")) {
			path += "/" + $id("province").value;
		}	
		if(this.id != "province") {
			if($id("town")) {
				path += "/" + $id("town").value;
			}
			if(this.id != "town") {			
				if($id("venue")) {
					path += "/" + $id("venue").value;
				}
			}
		}
	}

	window.location=path;

}


function createIESelectOptions(selectEl, responseText)
{
	
	// 14.06.2007 to workaround ie7 issue with setting city.innerHTML	
	optionsStringToHTMLElementsAndAttributes(responseText)
	
	selectEl.innerHTML = '';		

	for (var i=1; i<ie7_Options.length; i++)
	{
		var myOption = document.createElement('option');
			myOption.value = ie7_Options[i].value;
		
		if (ie7_Options[i].selected)
		{
			myOption.selected = true;	
		}
		
		var myOptionString = document.createTextNode(ie7_Options[i].string);		
		myOption.appendChild(myOptionString);		
		//$id("searchResults").appendChild(myOption);
		selectEl.appendChild(myOption);		
	}	
}



function optionsStringToHTMLElementsAndAttributes(str)
{
	// 14.06.2007 to workaround ie7 issue with setting city.innerHTML
	// dissect the string to create a collection of elements and attributes
	// this may be able to be done more efficiently using regular expressions
	
	// empty the array in case there are already values in here
	ie7_Options.length = 0;	
	
	var opts = str.split('<option');		

	for (var i=1; i<opts.length; i++)
	{

		ie7_Options[i] = new Array();
		
		ie7_Options[i].value = opts[i].split("value='")[1];

		
		
		ie7_Options[i].value = ie7_Options[i].value.substring(0, ie7_Options[i].value.indexOf("'"));
		
		if (opts[i].match('selected'))
		{
		//	cannot use split technique as lazy ie syntax: 'selected' rather than 'selected="selected"'
			ie7_Options[i].selected = 'selected';
		}
		
		ie7_Options[i].string = opts[i].split('>')[1];
		ie7_Options[i].string = ie7_Options[i].string.split('<')[0];				
	}
}
