// Original JavaScript code by Duncan Crombie: dcrombie@chirp.com.au
// Please acknowledge use of this code by including this header.

// CONSTANTS
NS4 = (document.layers) ? true : false;

//-----------------------
//MOVEMENT FUNCTIONS

//simple function to go to a page
function goToUrl(goURL) {
	window.location = goURL;	
}

//simple function to open any external link in a smaller window
function goOpen(openUrl) {
	window.open(openUrl,"_blank","width=300,height=300,menubar,resizable,scrollbars,status,toolbar,titlebar");
}

//simple function to open any external link in a smaller window
function goPop(openUrl) {
	window.open(openUrl,"_blank","width=400,height=250,menubar=0,resizable=0,scrollbars=1,status=0,toolbar=0,titlebar=0");
}

//simple function to open any external link in a smaller window
function goPopSize(openUrl,intWidth,intHeight) {
	window.open(openUrl,"_blank","width="+intWidth+",height="+intHeight+",menubar=0,resizable=0,scrollbars=1,status=0,toolbar=0,titlebar=0");
}

//simple follow on from go, submits to specified page
function goSubmit(goUrl, goForm) {
	goForm.action = goUrl;
	goForm.submit();
	return true;
}

//another simple reusable function for forms
function goCancel(cancelMsg) {
	if (confirm(cancelMsg)) { window.location = "/"; }
}

//-----------------------
//ASSOCIATION FUNCTIONS

//function to add a new item association to an item
function addNew(oNodeName) {
	newUrl = "/admin/new_association.php?oname="+oNodeName;
	goPopSize(newUrl,500,537);
}

//function to add an existing item association to an item
function addExist(oNodeName,oNodeType,uLevel) {
	newUrl = "/admin/exist_association.php?oname="+oNodeName+"&otype="+oNodeType+"&ulevel="+uLevel;
	goPopSize(newUrl,500,537);
}

//-----------------------
//FORM HANDLING FUNCTIONS

//checking if required field is empty
function isEmpty(s) {
	return ((s == null) || (s.length == 0));
}

//checking if required email field is email
function isEmail(str_address) {
    if (str_address.indexOf('@') == -1) {
    	return false;
    } else {
    	return true;
    }
}

//checking if at least one check box is checked
function isChecked(chkName,objForm) {
	var bol_checked = false;
	for(var i=0; i<objForm.elements.length; i++) {
		if(objForm.elements[i].type=="checkbox" && objForm.elements[i].name==chkName) {
			if (objForm.elements[i].checked) { bol_checked = true; }
		}
	}
	return bol_checked;
}

//checking if select has been moved off default value UNFINISHED
function isSelected(s) {
	//return ((s == null) || (s.length == 0));
	return true;
}

//-----------------------
//LOGIN/OUT FUNCTIONS

//users login
function logIn(userUrl) {
	goSubmit(userUrl, document.users_authorisation);
}

//users logout
function logOut(userUrl) {
	document.users_authorisation.auth_out.value = 'out';
	goSubmit(userUrl, document.users_authorisation);
}

function checkEnter(event) { 	
	var code = 0;
	
	if (NS4)
		code = event.which;
	else
		code = event.keyCode;
	if (code==13)
		document.users_authorisation.submit();
}

//-----------------------
//EMAIL FUNCTIONS

function submitGroupEmail() {
	document.email_object_coord.subject.value = document.email_object.subject.value;
	document.email_object_coord.body.value    = document.email_object.body.value;
	goSubmit(document.email_object_coord.action,document.email_object_coord);
}

//-----------------------
//EDITABLE REGION FUNCTIONS

//simple submit
function editable_submit() {
	goSubmit(document.region_editable.action, document.region_editable);
}

//go live - make live hidden field 1 then submit
function editable_live() {
	if (document.region_editable.is_live.value == 1) { document.region_editable.is_live.value = 0 }
	else { document.region_editable.is_live.value = 1 }
	editable_submit();
}

//go live - make live hidden field 1 then submit
function editable_delete(delId,nodeId) {
	var loc = window.location.href.split('?');
	var delString = loc[0]+'?id='+delId+'&del=1&edit='+nodeId;
	if (confirm("Are you sure you want to delete this item?")) { goToUrl(delString); }
}

//go live - make live hidden field 1 then submit
function editable_cancel(showId) {
	//window.back();
	var loc = window.location.href.split('?');
	var showString = loc[0];
	if (showId!=0) { showString+='?id='+showId; }
	goToUrl(showString);
}

//-----------------------
//OTHER FUNCTIONS

//limit the text on textareas etc to prevent DB errors on entering too much text
function limitText(fieldObj,maxChars) {
	var result = true;
	if (fieldObj.value.length >= maxChars)
		result = false;
	if (window.event)
		window.event.returnValue = result;
	return result;
}
