
//Confirm user action
function confirm_action(msg) {
	if (confirm(msg)) {
		return true;
	} else {
		return false;
	}
}

//Confirm user action then redirect to new url
function confirm_location(msg,url) {
	if (confirm(msg)) {
		window.location = url;
	} else {
		return false;
	}
}

//Used for cancelling <form> edits
function cancel_edit(msg) {
	if (msg == null) {
		msg = "Are you sure you want to discard these changes?";
	}
	if (confirm(msg)) {
		javascript:history.back();
		return false;
	} else {
		return true;
	}
}


//Add/Remove/Swap/Check for CSS class on element
//Unobtrusive Javascript - http://onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html
function jscss(a,o,c1,c2)
{
  switch (a){
    case 'swap':
      o.className=!jscss('check',o,c1)?o.className.replace(c2,c1): o.className.replace(c1,c2);
    break;
    case 'add':
      if(!jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    break;
    case 'remove':
      var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    break;
  }
}





//Return an array of elements with a specific class
//Dustin Diaz - http://www.dustindiaz.com/getelementsbyclass/
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}



//Only main categories should have images - hide image abilities from page if cat is set to subcat
function allow_cat_images(obj) {
	var div = document.getElementById("allow_cat_images");
	var div_warn = document.getElementById("allow_cat_images_warn");
	if (obj.value == 0) {
		jscss('remove',div,"hide");
		jscss('add',div_warn,"hide");
	} else {
		jscss('add',div,"hide");
		jscss('remove',div_warn,"hide");
	}
}


//Validate new/existing product edits
function validate_product() {
	var errormsg = false;

	if (document.getElementById("prodname").value == "") {
		errormsg += "\n - You must enter a title";
	}
	if (document.getElementById("prodsubname").value == "") {
		errormsg += "\n - You must enter a subtitle";
	}

	if (errormsg != false) {
		alert("You have not completed all required fields: " + errormsg);
		return false;
	} else {
		return true;
	}
}


//Validate new/existing product edits
function validate_cat() {
	var errormsg = false;

	if (document.getElementById("catname").value == "") {
		errormsg += "\n - You must enter a title";
	}
	if (document.getElementById("parent").value == 0 && document.getElementById("catdesc").value == "") {
		errormsg += "\n - You must enter a description";
	}

	if (errormsg != false) {
		alert("You have not completed all required fields: " + errormsg);
		return false;
	} else {
		return true;
	}
}


//Show or hide the option to add new images/attachments to documents/categories
function allow_upload(obj, key) {
	var cur_radio = obj.value;

	//Select varnames
	var obj_file = document.getElementById("upload_" + key);
	var obj_div  = document.getElementById("add_" + key);

	//Update page to show/hide/disable elements
	switch (cur_radio) {
		case "keep":
		case "delete":
			obj_file.disabled = true;
			jscss('add',obj_div,'hide');
		break;
		case "new":
			obj_file.disabled = false;
			jscss('remove',obj_div,'hide');
		break;
	}
}