// JavaScript Document
function do_action(iId,dbAction)
{
	if(dbAction)
	{
		var new_loc = 'detail.php?iId=' + iId + '&dbAction=' + dbAction;
		confirmed = confirm('Are you sure?');
	}
	else
	{
		var new_loc = 'detail.php?iId=' + iId;
		confirmed = true;
	}
	if(confirmed)
	{
		window.location = new_loc;
	}
}

function isblank(s)
{
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

function validate_form(f)
{
    var msg;
    var empty_fields = "";
	var overflow_fields = "";
    var errors = "";
	var focus_field = "";
	var form_data = "";
	var c = 0;

    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't have an "optional" property
    // defined.  Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // then verify that they are numbers and that they are in the right range.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
		//alert(e.type);
        if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
            // first check if the field is empty
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
				if (!e.description){
					if(empty_fields.indexOf((e.name)) == -1) {
						empty_fields += "\n          " + e.name;
					}
//                	empty_fields += "\n          " + e.name;
				}
				else {
					if(empty_fields.indexOf((e.description)) == -1) {
						empty_fields += "\n          " + e.description;
					}
//					empty_fields += "\n          " + e.description;
				}
				if (focus_field == "") {
					focus_field = e;
				}
                continue;
            }

            // Now check for fields that are supposed to be numeric.
            if (e.numeric || (e.min != null) || (e.max != null)) { 
                var v = parseFloat(e.value);
                if (isNaN(v) || 
                    ((e.min != null) && (v < e.min)) || 
                    ((e.max != null) && (v > e.max))) {
                    errors += "- The field " + e.name + " must be a number";
                    if (e.min != null) {
                        errors += " that is greater than " + e.min;
						if (focus_field == "") {
							focus_field = e;
						}
					}
                    if (e.max != null && e.min != null) {
                        errors += " and less than " + e.max;
						if (focus_field == "") {
							focus_field = e;
						}
					}
                    else if (e.max != null) {
                        errors += " that is less than " + e.max;
						if (focus_field == "") {
							focus_field = e;
						}
					}
                    errors += ".\n";
                }
            }
        }
		if (((e.type == "text") || (e.type == "textarea")) && e.max_chars) {
			if (e.value.length > e.max_chars)
			{
				if (!e.description){
                	overflow_fields += "\n          " + e.name;
				}
				else {
					overflow_fields += "\n          " + e.description;
				}
			}
		}
		if (e.type == "radio") {
			var bOneChecked = false;
	        var aRadios = document.getElementsByName(e.name);
	        for (var j = 0; j < aRadios.length; j++) {
              if (aRadios[j].checked) bOneChecked = true;
 	        }

    	     if (!bOneChecked) {
				 if (!e.description){
				 	if(empty_fields.indexOf((e.name)) == -1) {
                		empty_fields += "\n          " + e.name;
					}
				}
				else {
					if(empty_fields.indexOf((e.description)) == -1) {
						empty_fields += "\n          " + e.description;
					}
				}
				if (focus_field == "") {
					focus_field = e;
				}
			}
		}
		if (e.type == "select-one")
		{
			if (e.value == "-1")
			{
				if (!e.description){
					if(empty_fields.indexOf((e.name)) == -1) {
						empty_fields += "\n          " + e.name;
					}
//                	empty_fields += "\n          " + e.name;
				}
				else {
					if(empty_fields.indexOf((e.description)) == -1) {
						empty_fields += "\n          " + e.description;
					}
//					empty_fields += "\n          " + e.description;
				}
				if (focus_field == "") {
					focus_field = e;
				}
                continue;
			}
		}
    }

    // Now, if there were any errors, then display the messages, and
    // return true to prevent the form from being submitted.  Otherwise
    // return false
    if (!empty_fields && !errors && !overflow_fields) {
/*		for(var i = 0; i < f.length; i++) {
			var e = f.elements[i];
			if((e.type == "text") || (e.type == "textarea")) {
				if(!e.description) {
					form_data += e.name + "::" + e.value + "%%";
				}
				else {
					form_data += e.description + "::" + e.value + "%%";
				}
				c++;
			}
			else if((e.type == "radio") && (e.checked == true)) {
				if(!e.description) {
					form_data += e.name + "::" + e.value + "%%";
				}
				else {
					form_data += e.description + "::" + e.value + "%%";
				}
				c++;
			}	
		}
		f.form_data.value = form_data;
		f.element_count.value = c;*/
		return true;
		//return false;
	}

    msg  = "______________________________________________________\n\n"
    msg += "The form was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "______________________________________________________\n\n"

    if (empty_fields) {
        msg += "- The following field(s) have not been completed correctly:" 
                + empty_fields + "\n";
    }
	if(overflow_fields)
	{
		msg += "\n- The following field(s) contain too many characters:"
			+ overflow_fields + "\n";
	}
	if(errors){
		msg += "\n"
			+ errors;
	}
    alert(msg);
	if (focus_field != "") {
		focus_field.focus();
	}
    return false;
}

function validate_date(date)
{
	if(date.indexOf("/") < 0)
	{
		return false;
	}

	var date_array = date.split("/");
	
	if(date_array.length < 3)
	{
		return false;
	}
	else
	{
		//Set up date elements for checking
		var day = parseFloat(date_array[0]);
		var month = parseFloat(date_array[0]);
		var year = parseFloat(date_array[0]);
		
		//Check for months and leap years
		if(month == 2 && ((year % 4 > 0 && day > 28) || day > 29))
		{
			return false;
		}
		else if((a_month == 4 || a_month == 6 || a_month == 9 || a_month == 11) && a_day > 30)
		{
			return false;
		}
		else if(a_day > 31)
		{
			return false;
		}
		else if(d_month == 2 && ((d_year % 4 > 0 && d_day > 28) || d_day > 29))
		{
			return false;
		}
		else if((d_month == 4 || d_month == 6 || d_month == 9 || d_month == 11) && d_day > 30)
		{
			return false;
		}
		else if(d_day > 31)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}

function set_contact_info()
{
	if(window.document.data.contact_id.selectedIndex > 0)
	{
		var contact_id = window.document.data.contact_id.selectedIndex - 1;

		window.document.data.contact_name.value = unescape(contact_list[contact_id][0]);
		window.document.data.contact_email.value = unescape(contact_list[contact_id][1]);
		window.document.data.contact_phone.value = unescape(contact_list[contact_id][2]);
		window.document.data.consultant_id.value = contact_list[contact_id][3];
	}
	else
	{
		window.document.data.contact_name.value = "";
		window.document.data.contact_email.value = "";
		window.document.data.contact_phone.value = "";
		window.document.data.consultant_id.value = 0;
	}
}

function validate_job(form)
{
	var return_value = false;
	
	var sectorsOK = false;
	for(i=0;i<form.sector.length;i++)
	{
		if(form.sector[i].checked == true)
		{
			sectorsOK = true;
			break;
		}
	}
	
	if(form.xml.checked == true)
	{
		var xml = true;
	}
	else
	{
		var xml = false;
	}
	
	if(isblank(form.title.value))
	{
		alert("Please enter a title");
		form.title.focus();
	}
	else if(isblank(form.reference.value))
	{
		alert("Please enter a reference");
		form.reference.focus();
	}
	else if(isblank(form.role_type.value))
	{
		alert("Please select a role type");
		form.role_type.focus();
	}
	else if(isblank(form.salary.value))
	{
		alert("Please enter a salary");
		form.salary.focus();
	}
	else if(isblank(form.start_date.value))
	{
		alert("Please enter a start_date");
		form.start_date.focus();
	}
	else if(isblank(form.display_location.value))
	{
		alert("Please enter a location");
		form.display_location.focus();
	}
	else if(isblank(form.detail.value))
	{
		alert("Please enter some detail");
		form.detail.focus();
	}
	else if(form.consultant_id.value == 0)
	{
		alert("Please select a consultant");
		form.consultant_id.focus();
	}
	else if(isblank(form.contact_name.value))
	{
		alert("Please enter a contact_name");
		form.contact_name.focus();
	}
	else if(isblank(form.contact_email.value) && isblank(form.contact_phone.value))
	{
		alert("Please enter a contact email or phone number");
		form.contact_email.focus();
	}
	else if(sectorsOK == false)
	{
		alert("Please select at least one sector");
		form.sector[0].focus();
	}
	else if(xml == true && isblank(form.xmlCountry.value))
	{
		alert("Please enter the Country");
		form.xmlCountry.focus();
	}
	else
	{
		return_value = true;
	}
	return return_value;
	//alert(return_value);
}

function validate_page(form)
{
	var return_value = false;
	
	if(isblank(form.title.value))
	{
		alert("Please enter a title");
		form.title.focus();
	}
	else
	{
		return_value = true;
	}
	return return_value;
	//alert(return_value);
}

function validate_upload(form)
{
	var return_value = false;
	
	if(isblank(form.description.value))
	{
		alert("Please enter a description");
		form.description.focus();
	}
	else if(isblank(form.filename.value))
	{
		alert("Please select a file");
		form.filename.focus();
	}
	else
	{
		return_value = true;
	}
	return return_value;
	//alert(return_value);
}

function validate_banner(form)
{
	var return_value = false;
	
	if(isblank(form.title.value))
	{
		alert("Please enter a description");
		form.title.focus();
	}
	if(isblank(form.link_url.value))
	{
		alert("Please enter a link");
		form.link_url.focus();
	}
	else if(form.file_id.value == 0)
	{
		alert("Please select an image");
		form.file_id.focus();
	}
	else
	{
		return_value = true;
	}
	return return_value;
	//alert(return_value);
}

function validate_item(form)
{
	var return_value = false;
	
	if(isblank(form.name.value))
	{
		alert("Please enter a name");
		form.name.focus();
	}
	else
	{
		return_value = true;
	}
	return return_value;
	//alert(return_value);
}

function validate_news(form)
{
	var return_value = false;
	
	if(isblank(form.headline.value))
	{
		alert("Please enter a headline");
		form.headline.focus();
	}
	else if(isblank(form.summary.value))
	{
		alert("Please enter a summary");
		form.summary.focus();
	}
	else
	{
		return_value = true;
	}
	return return_value;
	//alert(return_value);
}

function validate_event(form)
{
	var return_value = false;
	
	if(isblank(form.title.value))
	{
		alert("Please enter a title");
		form.title.focus();
	}
	else if(isblank(form.location.value))
	{
		alert("Please enter a location");
		form.location.focus();
	}
	else if(isblank(form.summary.value))
	{
		alert("Please enter a summary");
		form.summary.focus();
	}
	else if(isblank(form.contact_name.value))
	{
		alert("Please enter a contact name");
		form.contact_name.focus();
	}
	else if(isblank(form.contact_phone.value))
	{
		alert("Please enter a contact phone");
		form.contact_phone.focus();
	}
	else if(isblank(form.contact_email.value))
	{
		alert("Please enter a contact email");
		form.contact_email.focus();
	}
	else
	{
		return_value = true;
	}
	return return_value;
	//alert(return_value);
}

function validate_publication(form)
{
	var return_value = false;
	
	if(isblank(form.title.value))
	{
		alert("Please enter a title");
		form.title.focus();
	}
	else if(form.cat_id.value == 0)
	{
		alert("Please select a category");
		form.cat_id.focus();
	}
	else if(isblank(form.summary.value))
	{
		alert("Please enter a summary");
		form.summary.focus();
	}
	else if(isblank(form.filename.value) && form.id.value == 0)
	{
		alert("Please select a file");
		form.filename.focus();
	}
	else
	{
		return_value = true;
	}
	return return_value;
	//alert(return_value);
}

function validate_user(form)
{
	var return_value = false;
	
	if(isblank(form.name.value))
	{
		alert("Please enter a name");
		form.name.focus();
	}
	else if(isblank(form.username.value))
	{
		alert("Please enter a username");
		form.username.focus();
	}
	else if(isblank(form.password.value))
	{
		alert("Please enter a password");
		form.password.focus();
	}
	else if(form.password.value != form.conf_password.value)
	{
		alert("The passwords do not match");
		form.password.focus();
	}
	else if(isblank(form.email.value))
	{
		alert("Please enter an email address");
		form.email.focus();
	}
	else
	{
		return_value = true;
	}
	return return_value;
	//alert(return_value);
}

function delete_item(id)
{
	if(confirm("Are you sure you want to delete this item?\n(this cannot be undone)"))
	{
		window.location = "update.cfm?action=delete&id=" + id;
	}
}

function delete_item2(id)
{
	if(confirm("Are you sure you want to delete this item?\n(this cannot be undone)"))
	{
		window.location = "delete.cfm?id=" + id;
	}
}

function change_item_status(action,id)
{
	if(confirm("Are you sure you want to " + action + " this item?"))
	{
		window.location = "update.cfm?action=" + action + "&id=" + id;
	}
}
