//THE FUNCTION FOR THE CASCADING MENU
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function chkF(field,alerttxt,reject){
	with (field) {
		if (value==null||value==""||value==reject){
			alert(alerttxt);
			return false
		} else { return true }
	}
}

function chkE(field,alerttxt){
	with (field){
		apos=value.indexOf("@")
		dotpos=value.lastIndexOf(".")
		if (apos<1||dotpos-apos<2) 
			  {alert(alerttxt);return false}
		else {return true}
	}
}

function registerForm(thisForm) {
	with (thisForm){
		if (chkF(username, "Please enter a username.", "asd")==false){ username.focus(); return false }
		if (chkF(fullName, "Please enter your name.", "asd")==false){ fullName.focus(); return false }
		if (chkE(email,"Sorry that isn't a valid Email Address")==false){ email.focus(); return false }
		if (chkF(password, "Please enter a password.", "asd")==false){ password.focus(); return false }
		if (password.value != confirm.value) { alert("Sorry you passwords do not match, please try again"); password.focus(); return false }		
	}
}

function loginForm(thisForm) {
	with (thisForm){
		if (chkF(email,"Please enter your username")==false){ email.focus(); return false }
		if (chkF(password,"Please enter your Password", "")==false){ password.focus(); return false }
	}
}

function changePassword(thisForm) {
	with (thisForm){
		if (chkF(password,"Please enter your Current Password", "")==false){ password.focus(); return false }
		if (chkF(newPassword,"Please enter your New Password", "")==false){ newPassword.focus(); return false }
		if (newPassword.value != confirmPassword.value) { alert("Sorry you passwords do not match, please try again"); newPassword.focus(); return false }
	}
}

function imgover(img_name, img_src) { document[img_name].src = img_src; }

function mainBoxTab(section) {
	PublishContent('Loading...','ajax');
	GetContent('/ajax.php?do=mainBoxTab&section='+section,'ajax');
}

function GetContent(url,id) {
	var httpRequest;
	if (window.XMLHttpRequest) {
		try { httpRequest = new XMLHttpRequest(); }
		catch(e) {}
	} 
	else if (window.ActiveXObject) {
		try { httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch(e) {
			try { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } 
			catch(e) {}
		}
	   }
	if(! httpRequest) {
		alert('\n\nSorry, unable to open a connection to the server.');
		return false;
	}
	httpRequest.onreadystatechange = function() { PublishContent(httpRequest,id); };
	httpRequest.open('GET',url,true);
	httpRequest.send('');
}

function PublishContent(content,id) {
	try {
		if (content.readyState == 4) {
			if(content.status == 200) { document.getElementById(id).innerHTML=content.responseText; }
			else { /* alert('\n\nContent request error, status code:\n'+content.status+' '+content.statusText); */ }
			}
		}
	catch(error) { alert('Error: '+error.name+' -- '+error.message); }
}

function SearchBox(act, act2) {
	if (act == 'click') {
		document.getElementById('searchBox').value = '';
		document.getElementById('searchBox').style.color = "#000000";
	} else if (act == 'blur') {
		if (document.getElementById('searchBox').value == '') {
			if (act2 != null) { document.getElementById('searchBox').value = act2; } else { document.getElementById('searchBox').value = 'Search...'; }
			 document.getElementById('searchBox').style.color = "#CCCCCC";
		} else { document.getElementById('searchBox').style.color = "#CCCCCC"; }
	}
}

function uploadForm(thisForm) {
	with (thisForm){
		if (chkF(uploadedfile, "Please select the video you would like to upload.", "asd")==false){ uploadedfile.focus(); return false }
		doUpload();
	}
}

function newOrg(thisForm) {
	with (thisForm){
		if (chkF(username, "Please enter the new organisations username.", "asd")==false){ username.focus(); return false }
		if (chkF(password, "Please enter their password.", "asd")==false){ password.focus(); return false }
		if (chkF(confirmPassword,"Please confirm the password", "")==false){ confirmPassword.focus(); return false }
		if (password.value != confirmPassword.value) { alert("Sorry your passwords do not match, please try again"); password.focus(); return false }
		if (chkF(newCredits,"Please confirm the users number of credits", "")==false){ newCredits.focus(); return false }
	}
}

function doUpload() {
	document.getElementById('uploadS').disabled=true;
	document.getElementById('tablearea').style.display = "none";
	var filename = document.getElementById('uploadedfile').value;
	document.getElementById('doUpload').innerHTML='<br /><p style="text-align:center;"><img src="/images/loader.gif" width="220" height="19" alt="LOADING" title="LOADING" /></p><h3 style="margin-bottom:8px;text-align:center;">Video Now Uploading</h3><p style="text-align:center;">We are now uploading your video <strong>'+filename+'</strong> to our system, when this has finished you will automatically be redirected.</p><p style="text-align:center;">DO NOT click away from this page while this upload is in progress or your upload will be cancelled.</p>';
}


function availability() {
	document.getElementById('availabilityresult').innerHTML = '<p>Checking...</p>';
	GetContent('/checkavailability.php?itm='+escape(document.getElementById('username_box').value), 'availabilityresult');
}


function newwin(lnk) {
	window.open(lnk.href, 'newwindow','toolbar=yes, location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes, resizable=yes');
	return false;
}

// The are you sure function
function areyousure(txt) { var q = confirm(txt); if (!q){ return false; } }
