function pudMagick() {
	var ie	= document.all;
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
   
		if (anchor.getAttribute("href") &&
			anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";

		anchor.onfocus = function() { this.blur; }
	}

	if (ie)	{
		navRoot = document.getElementById("nav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];

			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}

	var headers = document.getElementsByTagName("h1");
	headers[0].onclick = function() { window.location="/" }
}

window.onload = pudMagick; 


function validate(form) {
	var success = 1;
	var warning = document.getElementById('warning');
	warning.innerHTML = '';

	if (!document.getElementsByTagName) return;

	var labels = document.getElementsByTagName("label");

	for (var i=0; i<labels.length; i++) {

		var label = labels[i];
		label.parentNode.className = '';
		
		var input = document.getElementById(label.htmlFor);

		if ((input.className == "required") && (input.value == '')) {
			success = 0;
			warning.innerHTML = warning.innerHTML + label.innerHTML + ' is a required field <br>';
			label.parentNode.className="validation"
		};
	
		if ((input.className == "required email")) {
			if(echeck(input.value)==false) {
				success = 0;
				warning.innerHTML = warning.innerHTML + 'Please enter a valid e-mail address <br>';
				label.parentNode.className="validation"
			}
		}
	
		if ((input.id == 'param_pass') && (input.value != document.getElementById('param_pass2').value)) {
			success = 0;
			warning.innerHTML = warning.innerHTML + 'Passwords do not match <br>';
			label.parentNode.className="validation";
			document.getElementById('param_pass2').parentNode.className="validation";
		}
	}

	if (success == 0) {
		document.getElementById('warning').style.display='block';
		return false;
	}

	else {
		document.getElementById(form).submit();
		return true;
	}
}

function echeck(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					
	}

function numItems(num, which, whichArray, dropDownArray) {
	var container = document.getElementById(which + 'Items');

	num = parseInt(num);

	while (container.firstChild) container.removeChild(container.firstChild);

	if (!num) { return false; }
		if ((whichArray) && (which != 'image') && (which != 'input')) {
			if ((num > 0) && (num <= 4)) {
				for (var i=0; i<num; i++) {
					container.innerHTML = container.innerHTML + "<label for='" + which + "Item" + (i + 1) + "' class='smaller'>" + which + " " + (i + 1) + " Name</label><input type='text' name='" + which + "Name_" + (i + 1) + "' value='" + whichArray[i] + "' id='" + which + "Item" + (i + 1) + "' size='30'><br>";
					if (dropDownArray)
					{
						var dropDownChecked = "";
						if (dropDownArray[i])
						{
							dropDownChecked = "CHECKED";
						}
						var dropdownCheckBox = "<label for='" + which + "ItemCheckBox" + (i + 1) + "' class='smaller'> dropdown </label><input type='checkbox' name='" + which + "DropDown_" + (i + 1) + "' value='1' id='" + which + "ItemCheckBox" + (i + 1) + "' "+dropDownChecked+"\><br>";
						container.innerHTML = container.innerHTML + dropdownCheckBox;

					}
				}
			}
			else { 
				alert('An integer between 1 and 4 is required'); this.value='';
			}
		}

		if ((whichArray) && (which == 'image')) {
			if ((num > 0) && (num <= 10)) {
				for (var i=0; i<num; i++) {
					container.innerHTML = container.innerHTML + "<div class='item'><label for='" + which + "_name" + (i + 1) + "' class='caps'>" + which + " " + (i + 1) + " Name</label><input type='text' name='" + which + "_name" + (i + 1) + "' value='" + whichArray[i] + "' id='" + which + "_name" + (i + 1) + "' size='30'><br><label for='" + which + "_file" + (i + 1) + "' class='caps'>" + which + " " + (i + 1) + " File</label><input type='file' name='" + which + "_file" + (i + 1) + "' id='" + which + "_file" + (i + 1) + "'><br><label for='" + which + "_desc" + (i + 1) + "' class='caps'>" + which + " " + (i + 1) + " Desc</label><textarea name='" + which + "_desc" + (i + 1) + "' id='" + which + "_desc" + (i + 1) + "'></textarea><br></div>"
				}
			}
			else {
				alert('An integer between 1 and 10 is required'); this.value='';
			}
		}

		if ((whichArray) && (which == 'input')) {
			if ((num > 0) && (num <= 15)) {
				for (var i=0; i<num; i++) {
					container.innerHTML = container.innerHTML + "<label for='" + which + "Item" + (i + 1) + "' class='smaller'>" + which + " " + (i + 1) + " Name</label><input type='text' name='" + which + "Name_" + (i + 1) + "' value='" + whichArray[i] + "' id='" + which + "Item" + (i + 1) + "' size='30'><br>";
				}
			}
			else { 
				alert('An integer between 1 and 15 is required'); this.value='';
			}
		}
	}

function startChat() {
	document.getElementById("chatHolder").style.display = 'block';
	document.getElementById("chatLink").style.display = 'none';
	initialize();
}
