// JavaScript Document
// Envision Training
// Javascript include file
// Validation scripts and utilities
// Last Updated : 19/05/2004

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

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 delete_item(id)
{
	if(confirm("Are you sure?\n(this cannot be undone)"))
	{
		window.location = "delete.cfm?id=" + id;
	}
}

function cancel_item()
{
	if(confirm("Are you sure?"))
	{
		window.location = "default.cfm";
	}
}

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];
        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.description)) == -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;
				}
			}
		}
    }

    // 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;
	}

    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 is_email(str)
{
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp)
  {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function check_text_limit(form,key_pressed,limit)
{
//	if(browser_ver > 4)
//	{
		var CharsLeft = limit - form.detail.value.length;
		if(key_pressed == 'yes')
		{
			if (event.keyCode != 8 && CharsLeft <= 0)
			{
				form.detail.value = form.detail.value.slice(0,limit);
				CharsLeft = 0;
			}
		}
	
		if(CharsLeft < 1000)
		{
			counter.innerHTML = form.detail.value.length + text_chars_used + CharsLeft + text_chars_available + text_counter_updates;
		}
		else
		{
			counter.innerHTML = form.detail.value.length + text_chars_used + CharsLeft + text_chars_available+ text_counter_updates;
		}
//	}
}

function contains(sValue,sMultiVal)
{
	sCheck 	= new String(sMultiVal);
	sStart	= sCheck.substring(0,sCheck.indexOf(','));
	sEnd	= sCheck.substring(sCheck.lastIndexOf(',')+1,sCheck.length);

	if(sCheck.indexOf(","+sValue+",") != -1 || sEnd == sValue || sStart == sValue)
		return(true);
	return(false);
}

function validate_associate(form)
{
	var return_value = false;
	
	if(form.password.value.length < 6)
	{
		alert("Please enter a password of at least 6 characters in length");
		form.password.value = "";
		form.password_conf.value = "";
		form.password.focus();
	}
	else if(form.password.value != form.password_conf.value)
	{
		alert("Please enter your Password");
		form.password.value = "";
		form.password_conf.value = "";
		form.password.focus();
	}
	else if(validate_form(form) == true)
	{
		return_value = true;
	}
	
	return return_value;
}

function validate_associate_profile(form)
{
	var return_value = false;
	
	var skill_ids = ",";
	
	for(i=0;i<form.skill_id.length;i++)
	{
		if(form.skill_id[i].checked == true)
		{
			skill_ids += form.skill_id[i].value;
		}
	}
	
	if((form.profile.value == null) || (form.profile.value == "") || (isblank(form.profile.value)))
	{
		alert("Please enter your Profile");
		form.profile.focus();
	}
	else if(skill_ids == ",")
	{
		alert("Please select at least one Skill");
		form.skill_id[0].focus();
	}
	else
	{
		return_value = true;
	}
	
	return return_value;
}

function check_return(form)
{
	// Check For <ENTER> being pressed 
	var currentEvent = window.event;
	if(currentEvent.keyCode == 13)
	{
		form.submit();
	}
}
