// JavaScript Document



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

function getHTTPObject() {
	var xhr = false;
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xhr = new ActiveXObject("Microsoft.XMLHTTP");	
	}
	return xhr;
}


function is_ie() {
	if ((navigator.userAgent).indexOf("MSIE") != -1) return true;
}


/* generic text mark up tags */
function create_tag(id,c,text,tagtype,name) {
	var tag = document.createElement(tagtype);
	tag.setAttribute("id",id);
	tag.className = c;
	tag.name = name;
	var tag_text = document.createTextNode(text);
	tag.appendChild(tag_text);
	if (tagtype == "textarea") {
		tag.onfocus = function() {
			if (this.value == this.defaultValue) {
				//alert("");
				this.value = "";
				this.style.fontStyle = "normal";
			}	
		}
		tag.onblur = function() {
			if (this.value == "") {
				this.value = this.defaultValue;
				this.style.fontStyle = "italic";
			}
		}
	}
	return tag;
}

function create_link(url,txt,c,id,target) {
	var new_link = document.createElement("a");
	new_link.setAttribute("href",url);
	new_link.setAttribute("id",id);
	new_link.setAttribute("target",target);
	new_link.className = c;
	var anchor_text = document.createTextNode(txt);
	new_link.appendChild(anchor_text);
	return new_link;
}

function create_form(action,method,c,id) {
	var new_form = document.createElement("form");
	new_form.setAttribute("action",action);
	new_form.setAttribute("id",id);
	new_form.setAttribute("method",method);
	new_form.className = c;
	return new_form;
}

function create_label(f,txt) {
	var label;	
	label = document.createElement("label");
	label.setAttribute("for",f);
	var label_text = document.createTextNode(txt);
	label.appendChild(label_text);
	return label;
	
}

function create_input(name,id,value,type,c) {
	var input;
	input = document.createElement("input");
	input.setAttribute("name",name);
	input.setAttribute("id",id);
	input.setAttribute("value",value);
	input.setAttribute("type",type);
	input.className = c;
	if (type == "text") {
		input.onfocus = function() {
			if (this.value == this.defaultValue) {
				//alert("");
				this.value = "";
				this.style.fontStyle = "normal";
			}	
		}
		input.onblur = function() {
			if (this.value == "") {
				this.value = this.defaultValue;
				this.style.fontStyle = "italic";
			}
		}
	}
	if (type == "submit") input.value = ""; 
	return input;	
}

/* input */
function create_radio(name,id,value,type,checked) {
	var input;
	if(is_ie()) {
		if (checked == "checked") {
			checked_str = "checked=\"checked\"";
		} else {
			checked_str = "";
		}
		html = '<input name="' + name + '" id="' + id + '" value="' + value + '" type="' + type + '"' + checked_str + '>';
		//alert(html);
      	input = document.createElement(html);
   	} else {
		input = create_input(name,id,value,type);
		if (checked == "checked") input.setAttribute("checked",checked);
	}
	return input;	
}

/* select */
function create_select(name,id) {
	var select = document.createElement("select");
	select.setAttribute("name",name);
	select.setAttribute("id",id);
	return select;	
}

/* option */
function create_option(value,text) {
	var option = document.createElement("option");
	option.setAttribute("value",value);
	var option_text = document.createTextNode(text);
	option.appendChild(option_text);
	return option;	
}

/* button */
function create_button(name,id,text,type,c) {
	//Dynamically created buttons won't submit in IE - use input instead
	var button;
	if(is_ie()) {
		button = create_input(name,id,text,type,c);
	} else {
		var button = document.createElement("button");
		button.setAttribute("name",name);
		button.setAttribute("id",id);
		button.className = c;
		var button_text = document.createTextNode(text);
		button.appendChild(button_text);
		button.setAttribute("type",type);
	}
	return button;
}


function parse_response(request) {
	if (request.readyState == 4) {
		if (request.status == 200 || request.status == 304) {
			if (!request.responseXML) {
				alert("An error has occurred. Please try again.");
				return false;
			}
			var response = request.responseXML.documentElement;
			var mmc_action = response.getElementsByTagName('result')[0].firstChild.nodeValue;
			var active_form = document.getElementById("active_form");
			if (mmc_action == "email") {
				if (active_form) {
					var span = create_tag("active_form","response_msg","An email has been sent","span","");
					active_form.parentNode.replaceChild(span,active_form);
				}
			} else if (mmc_action == "update") {
				if (active_form) {
					var id = active_form.accommodation_id.value;
					var num_beds = active_form.num_beds.value;
					document.getElementById("beds_" + id).firstChild.nodeValue = num_beds;
					var location = active_form.location.value;
					document.getElementById("location_" + id).firstChild.nodeValue = location;
					var span = create_tag("active_form","response_msg","Successfully updated","span","");
					active_form.parentNode.replaceChild(span,active_form);
				}				
			} else if (mmc_action == "delete") {
				if (active_form) {
					var id = active_form.accommodation_id.value;
					//alert(id);
					//beds_id = document.getElementById("beds_" + id);
					accommodation_id = document.getElementById("edit_accommodation_" + id);
					//form_holder_id = document.getElementById("form_holder_" + id);
					
					var td = accommodation_id.parentNode;
					while (td.childNodes.length >= 1) {
						td.removeChild(td.firstChild);	
					}
					var span = create_tag("active_form","response_msg","Item Deleted","span","");
					td.appendChild(span);
				}				
			} else if (mmc_action == "insert") {
				window.location.reload();	
			}
		} else {
			if (active_form) {
				var span = create_tag("active_form","response_msg","An error has occurred","span","");
				active_form.parentNode.replaceChild(span,active_form);
			}			
		}
	}
}



function send_data(data) {
	var request = getHTTPObject();
	if (request) {
		//alert(data);
		request.onreadystatechange = function() {
			parse_response(request);	
		}
		request.open("POST","/ajax/process_submission.php?token="+Math.random(),true);
		request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		request.setRequestHeader("Content-length", data.length);
		request.setRequestHeader("Connection", "close");
		request.send(data);
		return true;
	} else {
		return false;	
	}
}

function prepare_form() {	
	if (!document.getElementById) return false;
	if (!document.getElementById("active_form")) return false;
	var active_form = document.getElementById("active_form");
	active_form.onsubmit = function() {
		if (!validateForm(this)) return false;
		var data = "";
		for (var i=0;i<this.elements.length;i++) {
			data += this.elements[i].name;
			data += "=";
			data += escape(this.elements[i].value);
			data += "&";
		}
		return !send_data(data);
	}	
}


function setup_accommodation() {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("accommodation")) return false;
	var table = document.getElementById("accommodation");
	var a = table.getElementsByTagName("a");
	for (i=0;i<a.length;i++) {
		
		a[i].onclick = function() {					//EDIT
			var active_form = document.getElementById("active_form");
			if (active_form) active_form.parentNode.removeChild(active_form);
			//alert(this.id);
			var form = create_form("","POST","","active_form");
			if (this.id.indexOf("edit") != -1) {
				
				var id = this.id.substr(19);
				var holder = document.getElementById("form_holder_" + id);
				var label = create_label("num_beds","No. of Beds");
				var num_beds = create_input('num_beds','num_beds','','text','required text');
				num_beds.setAttribute("maxlength","3");
				num_beds.maxLength = "3";
				num_beds.value = document.getElementById("beds_" + id).firstChild.nodeValue;
				
				var location = create_input('location','location','','text','required text');
				location.setAttribute("maxlength","200");
				location.maxLength = "200";
				location.value = document.getElementById("location_" + id).firstChild.nodeValue;
				
				var mmc_action = create_input('mmc_action','mmc_action','update','hidden','');
				
				var btn = create_button('','update','Update','submit','button');
				var d_btn = create_button('','delete','Delete','submit','button');
				d_btn.onclick = function() {
					document.getElementById("mmc_action").value = "delete";
				}
				
				form.appendChild(label);
				form.appendChild(num_beds);
				form.appendChild(location);
	
			} else if (this.id.indexOf("new") != -1) {	//ADD NEW
				var id = this.id.substr(20);
				//alert(id);
				
				//var label = create_label("num_beds","No. of Beds");
				var num_beds = create_input('num_beds','num_beds','No. of Beds','text','required text');
				num_beds.defaultValue = num_beds.value;
				num_beds.setAttribute("maxlength","3");
				num_beds.maxLength = "3";

				var location = create_input('location','location','Location','text','required text');
				location.defaultValue = location.value;
				location.setAttribute("maxlength","200");
				location.maxLength = "200";

				var mmc_action = create_input('mmc_action','mmc_action','insert','hidden','');
				var status_id;
				if (this.id.indexOf("_a_") != -1) {
					status_id = create_input('status_id','status_id','1','hidden','');
					var holder = document.getElementById("form_a_event_holder_" + id);
				} else {
					status_id = create_input('status_id','status_id','2','hidden','');
					var holder = document.getElementById("form_w_event_holder_" + id);
				}
				
				var event_id = create_input('event_id','event_id',id,'hidden','');	
				var btn = create_button('','insert','Add','submit','button');
				
				
				//form.appendChild(label);
				form.appendChild(num_beds);
				form.appendChild(location);
				form.appendChild(status_id);
				form.appendChild(event_id);
					
				
				
			} else {	//CONTACT
				
				var id = this.id.substr(14);
				
				var holder = document.getElementById("form_holder_" + id);
				var full_name = create_input('full_name','full_name','Enter your full name','text','required text');
				full_name.defaultValue = "Enter your full name";
				var email_address = create_input('email_address','email_address','Enter your email address','text','required text');
				email_address.defaultValue = "Enter your email address";
				var email_message = create_tag('email_message','','Type your message here','textarea','email_message');
				email_message.defaultValue = "Type your message here";
				var mmc_action = create_input('mmc_action','mmc_action','email','hidden','');
				var btn = create_button('','send','Send','submit','button');
				
				form.appendChild(full_name);
				form.appendChild(email_address);
				form.appendChild(email_message);	
			
			}
			
			if (!event_id) {
				var accommodation_id = create_input('accommodation_id','accommodation_id',id,'hidden','');
				form.appendChild(accommodation_id);
			}
			
			form.appendChild(mmc_action);
			
			form.appendChild(btn);
			if(d_btn) form.appendChild(d_btn);
				
			holder.appendChild(form);
			prepare_form();
			//if (full_name) full_name.select();
			return false;
		}
	}
		
}


function setup_register() {

	if (document.getElementById("register")) {
	
		
		var register = document.getElementById("register");
		
		register.onclick = function() {	
			var active_form = document.getElementById("active_form");
			if (active_form) active_form.parentNode.removeChild(active_form);
			var form = create_form("/accommodation/","POST","","active_form");
			form.onsubmit =  function() {
				return validateForm(this);
			}
			var holder = document.getElementById("login_form_holder");
			
			var h2 = create_tag('','','Register','h2','');;
			var full_name = create_input('full_name','full_name','Enter your full name','text','required text');
			full_name.defaultValue = "Enter your full name";
			var email_address = create_input('email_address','email_address','Enter your email address','text','required text');
			email_address.defaultValue = "Enter your email address";
			var password = create_input('password','password','Enter your password here','text','required text');
			password.defaultValue = "Enter your password here";
			
	
			password.onblur = function() {
				//alert("");
				if (this.value.length == 0) {
					if (is_ie()) {
						//var ie_password = this.cloneNode(false);
						//alert(ie_password.type);
						ie_password = document.createElement("input");
						ie_password.type = 'text';
						ie_password.id = 'password';
						ie_password.name = "password";
						ie_password.className = 'required text';
						//alert(this.onclick);
						ie_password.onfocus = this.onfocus;
						ie_password.onblur = this.onblur;
						ie_password.value = "Enter your password here";
						ie_password.defaultValue = this.defaultValue;
						
						password = this.parentNode.replaceChild(ie_password,this);
					} else {
						this.type = "text";	
						this.value = "Enter your password here";
					}
				}
			}
			
			password.onfocus = function() {
				//alert(this.defaultValue);
				if (this.value == this.defaultValue) {
					if (is_ie()) {
						var ie_password = this.cloneNode(false);
						ie_password.type='password';
						//alert(this.onblur);
						ie_password.onblur = this.onblur;
						ie_password.onfocus = this.onfocus;
						ie_password.defaultValue = this.defaultValue;
						ie_password.value = this.value;
						if (ie_password.value == ie_password.defaultValue) ie_password.value = "";
						password = this.parentNode.replaceChild(ie_password,this);
						//alert(password.id);
						document.getElementById("password").focus();
						document.getElementById("password").focus();
					} else {
						this.value = "";
						this.type = "password";	
					}
				}
			}
			
			var mmc_action = create_input('mmc_action','mmc_action','register','hidden','');
			var btn = create_button('','register','Register','submit','button');
			
			form.appendChild(h2);
			form.appendChild(full_name);
			form.appendChild(email_address);
			form.appendChild(password);
			form.appendChild(mmc_action);
			form.appendChild(btn);
			
			holder.appendChild(form);
			return false;
		}
		
	}
}


function setup_login() {
	if (document.getElementById("login")) {
		
		var login = document.getElementById("login");

		login.onclick = function() {
			var active_form = document.getElementById("active_form");
			if (active_form) active_form.parentNode.removeChild(active_form);
			var form = create_form("/accommodation/","POST","","active_form");
			form.onsubmit =  function() {
				return validateForm(this);
			}
			var holder = document.getElementById("login_form_holder");
			
			var h2 = create_tag('','','Login','h2','');;
			var email_address = create_input('email_address','email_address','Enter your email address','text','required text');
			email_address.defaultValue = "Enter your email address";
			var password = create_input('password','password','Enter your password here','text','required text');
			password.defaultValue = "Enter your password here";
			
			password.onblur = function() {
				//alert("");
				if (this.value.length == 0) {
					if (is_ie()) {
						//var ie_password = this.cloneNode(false);
						
						ie_password = document.createElement("input");
						ie_password.type = 'text';
						ie_password.id = 'password';
						ie_password.name = "password";
						ie_password.className = 'required text';
						//alert(this.onclick);
						ie_password.onfocus = this.onfocus;
						ie_password.onblur = this.onblur;
						ie_password.value = "Enter your password here";
						ie_password.defaultValue = this.defaultValue;
						
						password = this.parentNode.replaceChild(ie_password,this);
					} else {
						this.type = "text";	
						this.value = "Enter your password here";
					}
				}
			
			}
			
			password.onfocus = function() {
				//alert(this.defaultValue);
				if (this.value == this.defaultValue) {
					if (is_ie()) {
						var ie_password = this.cloneNode(false);
						ie_password.type='password';
						//alert(this.onblur);
						ie_password.onfocus = this.onfocus;
						ie_password.onblur = this.onblur;
						ie_password.defaultValue = this.defaultValue;
						ie_password.value = this.value;
						if (ie_password.value == ie_password.defaultValue) ie_password.value = "";
						password = this.parentNode.replaceChild(ie_password,this);
						//alert(password.id);
						document.getElementById("password").focus();
						document.getElementById("password").focus();
					} else {
						this.value = "";
						this.type = "password";	
					}
				}
			}
			
			var mmc_action = create_input('mmc_action','mmc_action','login','hidden','');
			var btn = create_button('','login','Login','submit','button');
			
			form.appendChild(h2);
			form.appendChild(email_address);
			form.appendChild(password);
			form.appendChild(mmc_action);
			form.appendChild(btn);
			
			holder.appendChild(form);
			return false;
		}
	}
}


add_load_event(setup_login);
add_load_event(setup_register);
add_load_event(setup_accommodation);