function LTrim( value ) {	//trim whitespace from left
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}


function RTrim( value ) {  //trim whitespace from right
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}


function trimAll( value ) { //trim both
	
	return LTrim(RTrim(value));
	
}

function saveForm(form_obj) {

	var msg = "Please fill in all the required fields.\n";
	var missing_fields = false;

	var mess = false;
	var fields = new Array('question', 'answer', 'given_name', 'surname', 'title', 'company', 'address', 'city',  'zip', 'phone', 'email');
	
	for(x = 0; x<fields.length;x++)
	{
		var value = eval('document.new_user.'+fields[x]+'.value');
		if(value == '')
		{
			if(!missing_fields)
			{
				msg += '\nMissing FIeld(s):\n';
				missing_fields = true;
			}
			msg += '  '+fields[x]+'\n';
			mess = true;
		}
	}

	if(document.getElementById("state").options[document.getElementById("state").options.selectedIndex].value == "") {
		if(!missing_fields)
		{
			msg += '\nMissing FIeld(s):\n';
			missing_fields = true;
		}
		msg += '  State\n';
		mess = true;
	}
	
	var phone_pattern = /^\(\d{3}\) (\d{3})\-(\d{4})$/;
	if(trimAll(document.new_user.phone.value).length < 1 || (document.new_user.phone.value.search(phone_pattern) == -1)) {
		msg += "\nPlease enter a valid phone number.\n";
		mess = true;
	}
		
	if(document.getElementById("industry").options[document.getElementById("industry").options.selectedIndex].value == "") {
		msg += "\nPlease select your industry.\n";
		mess = true;
	}

	if(document.new_user.password.value != document.new_user.password_confirm.value)
	{
		msg += '\nYour Password and Confirm Password do not match.\n';
		mess = true;
	}

	if(!mess) {	
		document.getElementById("submit_action").value = 'save';
		document.new_user.submit();	
		
	} else {
		alert(msg);
		return false;
	}

}




function validateUserForm(form_obj) {

	var msg = "Please fill in all the required fields.\n";
	var missing_fields = false;
	
	var mess = false;
	var fields = new Array('password', 'question', 'answer', 'given_name', 'surname', 'title', 'company', 'address', 'city',  'zip', 'phone', 'email');
	
	for(x = 0; x<fields.length;x++)
	{
		var value = eval('document.new_user.'+fields[x]+'.value');
		if(value == '')
		{
			if(!missing_fields)
			{
				msg += '\nMissing FIeld(s):\n';
				missing_fields = true;
			}
			msg += '  '+fields[x]+'\n';
			mess = true;
		}
	}

	if(document.getElementById("state").options[document.getElementById("state").options.selectedIndex].value == "") {
		if(!missing_fields)
		{
			msg += '\nMissing FIeld(s):\n';
			missing_fields = true;
		}
		msg += '  State\n';
		mess = true;
	}
	
	var phone_pattern = /^\(\d{3}\) (\d{3})\-(\d{4})$/;
	if(trimAll(document.new_user.phone.value).length < 1 || (document.new_user.phone.value.search(phone_pattern) == -1)) {
		msg += "\nPlease enter a valid phone number.\n";
		mess = true;
	}
		
	if(document.getElementById("industry").options[document.getElementById("industry").options.selectedIndex].value == "") {
		msg += "\nPlease select your industry.\n";
		mess = true;
	}

	if(document.new_user.password.value != document.new_user.password_confirm.value)
	{
		msg += '\nYour Password and Confirm Password do not match.\n';
		mess = true;
	}
	var email_pattern = /^[A-Za-z0-9_\+-]+(\.[A-Za-z0-9_\+-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.([A-Za-z]{2,4})$/;
	if(trimAll(form_obj.email.value).length < 1 || (form_obj.email.value.search(email_pattern) == -1)) {
		msg += '\nPlease enter a valid email address.\n';
		mess = true;
	}	

	var three = form_obj.policy_1.value + form_obj.policy_2.value + form_obj.policy_3.value;
	
	var f3 = false;
	
	if(three in oc(['510','181','310'])) 
	{
		f3 = true;
	} else 
	{
		msg += '\nPlease enter a valid policy number.\n';
		mess = true;			
	}	
	
	if((form_obj.policy_4.value.length == 0) || (form_obj.policy_5.value.length == 0) || (form_obj.policy_6.value.length == 0) || (form_obj.policy_7.value.length == 0) || (form_obj.policy_8.value.length == 0) || (form_obj.policy_9.value.length == 0)|| (form_obj.policy_10.value.length == 0)) 
	{

		msg += '\nPlease enter a valid 10 digit policy number.\n';
		mess = true;			
	}

	if(!mess) {	
		document.getElementById("submit_action").value = 'submit';
		document.new_user.submit();	
		
	} else {
	
		alert(msg);
		return false;
	}
	
}

function actionItemConfirm(url) {
	var conf = confirm('You are about to leave the question edit screen and move to it\'s associated action item edit screen. Are you sure you want to do this?');
	if(conf) {
		location.href=url;
	} else {
		return;
	}
}

function checkAllChildCategories(fObj, checked_id) {
	if(document.getElementById(checked_id).checked == true) {
		var mess;
		var pat = /checked_id+'_'/;
		var checks = new Array();
		var elements = fObj.elements;
		for(var i=0; i<elements.length; i++) {
			mess += elements[i].type + "\n\n";
			if(elements[i].type == 'checkbox') {
				checks.push(elements[i].id);
			}
		}
		
		for(var a=0; a<checks.length; a++) {
			if(checks[a].toString().search(checked_id + '_') != -1) {
				//alert(checks[a] + '  is a child of  ' + checked_id);
				document.getElementById(checks[a]).checked = true;			
			}			
		}
	}
}



function uncheckParentCategory(parent_id) {

	if(document.getElementById(parent_id).checked == true) {
		document.getElementById(parent_id).checked = false;
	}

}

function categoryPrompt() {
	var categoryConfirm = confirm("Are you sure you want to delete this category?\nThis cannot be undone.");

	if(categoryConfirm) {
		document.getElementById("action").value = "delete";
		document.forms[1].submit();
	}
}

function groupPrompt() {
	var groupConfirm = confirm("Are you sure you want to delete this group?\nThis cannot be undone.");

	if(groupConfirm) {
		document.getElementById("action").value = "delete";
		document.forms[1].submit();
	}
}

function titlePrompt() {
	var titleConfirm = confirm("Are you sure you want to delete this title?\nThis cannot be undone.");

	if(titleConfirm) {
		document.getElementById("action").value = "delete";
		document.forms[1].submit();
	}
}

function userPrompt() {
	var userConfirm = confirm("Are you sure you want to delete this user?\nThis cannot be undone.");

	if(userConfirm) {
		document.getElementById("action").value = "delete";
		document.new_user.submit();
	}
}

function typePrompt() {
	if(document.getElementById("title_count").value > 0) {
		alert("This cannot be done. There are currently " + document.getElementById("title_count").value + " titles of this type in the database.");
	} else {
	var typeConfirm = confirm("Are you sure you want to delete this type?\nThis cannot be undone.");
		if(typeConfirm) {
			document.getElementById("action").value = "delete";	
			document.forms[1].submit();
		}
	}
}


function deleteFile(form_obj, action, file) {

	var res = confirm('Are you sure you want to delete this file. This cannot be undone.');
	if(res) {
		//form_obj.delete_file.value = action;
		form_obj.file.value = file;
		form_obj.submit();
	}
}

function titleCheck(fObj) {


	var fields = new Array("title","description", "keywords");
	var mess = true;

	for(var i=0; i<fields.length; i++) {
		if(eval('fObj.' + fields[i] + '.value.length') < 1) {			
			mess = false;
		}
	}

	if(fObj.type.options[fObj.type.selectedIndex].value.length < 1) {
		mess = false;
	}
	
	if(!mess) {
		alert("Please enter all the required data");
		return;
	} else {
		
		document.getElementById("action").value = 'submit';
				
		document.title.submit();
		return;
	}
}

function ValidateSurvey(fobj) {

	if(fobj.survey[0].checked)
	{
		if(fobj.category.value == '')
		{
			alert("You must include the survey name.");
			fobj.category.focus();
			return false;
		}
	}
	else if(fobj.survey[1].checked)
	{
		if(fobj.description.value == '')
		{
			alert("You must include the survey question text.");
			fobj.description.focus();
			return false;
		}
	
		if(fobj.control.value == 'radio' && fobj.values.value=='')
		{
			alert("You must include options for your survey question.");
			fobj.values.focus();
			return false;
		}	
	}

	return true;
}

function titleCheckSave(fObj) {

	var fields = new Array("title","description", "keywords");
	var mess = true;

	for(var i=0; i<fields.length; i++) {
		if(eval('fObj.' + fields[i] + '.value.length') < 1) {			
			mess = false;
		}
	}

	if(fObj.type.options[fObj.type.selectedIndex].value.length < 1) {
		mess = false;
	}
	
	if(!mess) {
		alert("Please enter all the required data");
		return;
	} else {
		fObj.action.value = 'save';
		document.forms[1].submit();
		return;
	}
}

function oc(a)
{
  var o = {};
  for(var i=0;i<a.length;i++)
  {
    o[a[i]]='';
  }
  return o;
}


function showDiv(id){ 
	document.getElementById(id).style.display="block";
}

function hideDiv(id){ 
	document.getElementById(id).style.display="none";
}

function toggletab(tabid){

   if(document.getElementById(tabid).style.display=="none")
   {	
   		document.getElementById(tabid).style.display="block";
   }
   else
   {
   		document.getElementById(tabid).style.display="none";   
   }
}