function swap_field_editable_state (field_of_concern,obj,message) {
	var field_of_concern = document.getElementById(field_of_concern); 
	var checked = obj.checked;
	if (checked == false) {
		field_of_concern.disabled = true;
	}
	 else {
		if (message){
			alert(message);
		}
		field_of_concern.disabled = false;
	}	
}

function select_page_type(field_of_concern,is_edit){
	var confirmed = (is_edit) ? confirm('Generally you shouldn\'t change page types for existing pages, as it means you may leave "orphaned" data behind. Only click "OK" if you\'re sure your change won\'t effect this page.') : true;
	if(confirmed){
		var field_of_concern = document.getElementById(field_of_concern);
		field_of_concern.checked = true; 
	}
}

function replace_form_content(form_type_choice,editorName){
	$.get(form_type_choice.value + '.html',function(data){
		var editor = FCKeditorAPI.GetInstance(editorName);
		editor.SetHTML(data);
	});
}

function escape_HTML(S) {
	return S.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}

function check_email_address(email,errorObj){
	if (email == null || ! email.match(/^[a-z0-9'\-\+\._]+\@[a-z0-9'\-\+\._]+\.[a-z]{2,8}$/i)) {
		errorObj.alertstr += email + ' does not look like a valid email address\n';
		errorObj.invalid++;
	}
}

function check_text_field(textFieldName,fieldValue,errorObj,minLength,maxLength){
	if(fieldValue.length > maxLength || fieldValue.length < minLength){
		errorObj.alertstr += textFieldName + ' is incorrect. It has to be longer than ' + minLength + 
		' characters and shorter than ' + maxLength + ' characters.\n';
		errorObj.invalid++;
	}
}

function check_email_address_array(emailArrayString,errorObj){
	if((emailArrayString.length == 0 ) || emailArrayString.match(/^[\s\n]/)){
		return;
	}
	var emailArray = emailArrayString.split(/\s*,\s*/);
	for (var i = 0; i < emailArray.length; i++) {
		check_email_address(emailArray[i],errorObj);
	}
}

function check_page_url(urlString,errorObj){
	if(urlString == null || ! urlString.match(/^[a-z0-9\-_]{2,100}$/i)){
		errorObj.alertstr += 'The URL must be between 2 and 100 characters in length \n and can only contain letters, numbers, underscores or hyphens.\n';
		errorObj.invalid++;
	}
}

function formError(){
	this.alertstr = '';
	this.invalid = 0;
}

function check_page_form(form){
	var errorObj = new formError();
	if(form.elements['new']){
		check_page_url(form.elements['url'].value,errorObj);
	}
	check_text_field('Title',form.elements['title'].value,errorObj,2,100);
	return emit_errors_or_proceed(errorObj);
}

function check_form_to_email_gateway_form(form) {
	var errorObj = new formError();

	check_text_field('Title',form.elements['title'].value,errorObj,3,150);
	check_email_address_array(form.elements['custom_emails'].value,errorObj);
	return emit_errors_or_proceed(errorObj);
}

function emit_errors_or_proceed(errorObj){
	if (errorObj.invalid > 0 || errorObj.alertstr != '') {
		if (! errorObj.invalid){
			errorObj.invalid = 'The following';  
		}
		alert(errorObj.invalid + ' error(s) were encountered with your submission:' + '\n\n' + errorObj.alertstr + '\nPlease correct these errors and try again.\n');
		errorObj.alertstr = '';
		errorObj.invalid  = 0;
		return false;
	}
	return true;  // all checked ok
}
