// FORM VALIDATION ROUTINES

/* -=-=- Number field validation -=-=-
   Usage :
   <input type="text" size="4" maxlength="4" name="postcode" value="" onBlur="validateNum(this)">
*/

var whitespace = " \t\n\r";
var defaultEmptyOK = true;

function validateNum(item) {
	thisItem = item.value;
	if (thisItem.match(/[^0-9 ]/)) {
		alert("Invalid Number!")
		item.focus();
		return (false);
	} else {
		return (true);
	}
}

function isTextFieldEmpty(text_field) {

	emptyspaces = true;
	return_value = false;
	text_length = text_field.value.toString().length;
	msg = ""

	msg = "text_length:  " + text_length + "\n"
	if ( text_field.value != "" ) {
		for (i = 0; i < text_field.value.toString().length; i++) {
			// if length of the input string is 0 then it's empty
			msg = msg + i + ": " + text_field.value.toString().charAt(i)
			if ( text_field.value.toString().charAt(i) != " " ) {
				emptyspaces = false
				msg = msg + ",  emptyspaces now: " + emptyspaces;
			}
			msg = msg + "\n"
		}
				
		return_value = emptyspaces

	} else {
		// if no input string then it's empty
		return_value = true
	}
	msg = msg + "return_value: " + return_value + "\n"


	//alert(msg);
	return return_value
}	
		
function isDateValid() { }
		
function isPasswordConfirmed(password, confirmed_password) {
	if (password.value == confirmed_password.value) {
		return true
	} else {
		return false
	}
}
function isEmpty(s) {   
	return ((s == null) || (s.length == 0))
}
function isWhitespace (s) {   
	var i;

    if (isEmpty(s)) return true;

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    return true;
}


function emailOK(e) {
	if (!(isEmail(e.value))) {
		errors = "\n\t\t - " + e.name + ": the email address is not valid";				
		//alert(errors)
		return false;
	}
	return true;
}
function isEmail (s)
{   
	if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);

    if (isWhitespace(s)) return false;
    var i = 1;
    var sLength = s.length;

    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}
  
// FILE VALIDATION ROUTINES - currently being debugged in /cma_new/media/Media_Edit.asp   (CP  - 15/04/2002)

function getFilename (InString)  {
	LastSlash=InString.lastIndexOf ('\\', InString.length-1)
	OutString=InString.substring  (LastSlash+1, InString.length)
	return (OutString);	
}

function getFilenameExtension (InString)  {
	LastSlash=InString.lastIndexOf ('.', InString.length-1)
	OutString=InString.substring  (LastSlash+1, InString.length)
	return (OutString);	
}

function validateFilename(filename)
{
		 	var expr
			var alert_msg
			var valid = "abcdefghijklmnopqrstuvwxyz0123456789_."
			var ok = "yes";
			var temp
			var result;
			
			result = true;
			
			if (filename == "") {
				result = false;
			} else {
				ext = getFilenameExtension(filename)
			
				if (ext.length > 4 ) {
					alert  ("File Extension cannot be greater than 4 characters");
					result = false;
				}
		
				for (var i=0; i<filename.length; i++) {
					temp = "" + filename.substring(i, i+1);
					if (valid.indexOf(temp) == "-1") 
					ok = "no";
				}
				if (ok == "no") {
					alert("Invalid filename: must be of the format 'filename.ext'");
					result = false;
				}
			}
			
			alert("validateFilename: " + result);
			return result;
}


// 30/4/02. Vernon Yeo
// function calTimeAMPM with calculate the time in 00 - 24 and return the value. 
function calTimeAMPM (fhour, fam_pm) {
	var temp = "" + ((fam_pm == 'PM') ? Math.abs(fhour) + 12 : fhour)

	if (fam_pm == 'AM' && temp == "12") 
		return 00
	if (fam_pm == 'PM' && temp == "24") 
		return 12
	return temp
}

// compare the date and times.
function doDateTimeCheck(from, to) {
	if (Date.parse(from) < Date.parse(to)) {
		//alert("The dates are valid.");
		return true;
	} else {
		if (from == "" || to== "") {
			//alert_msg =  alert_msg + "Both dates must be entered. \n";
			return false;
		} else { 
			//alert ("End date must be greater than start date. \n");
			return false;
		}
	}
}

function doDateCheck(from, to) {
	if (Date.parse(from) <= Date.parse(to)) {
		//alert("The dates are valid.");
		return true;
	} else {
		if (from == "" || to== "") {
			//alert_msg =  alert_msg + "Both dates must be entered. \n";
			return false;
		} else { 
			//alert ("End date must be greater than start date. \n");
			return false;
		}
	}
}		