// Tests whether the specified value is the correct suburb format.  It must
// contain alphabets or dash (-) or forward slash (/).
function isValidSuburbFormat(suburb) {
	if (suburb.length == 0) {		
		return true;
	} // endof if
	for (var i = 0; i < suburb.length; i++) {
		c = suburb.charAt(i);
		if (!((c >= 'A' && c <= 'Z')
		    || c == '-'
		    || c == '\/'
		    || c == ' ')) {
		    return false;
		} // endof if
	} // endof for
	return true;
} // endof isValidSuburbFormat

// Tests whether the specified value is the correct street format.  It must
// contain alphabets or digits or dash (-) or forward slash (/).
function isValidStreetFormat(street) {
	if (street.length == 0) {		
		return true;
	} // endof if
	for (var i = 0; i < street.length; i++) {
		c = street.charAt(i);
		if (!((c >= 'A' && c <= 'Z')
		       || (c >= 0 && c <= 9)
		       || c == '-'
		       || c == '\/'
		       || c == ' ')) {
		    return false;
		} // endof if
	} // endof for
	return true;
} // endof isValidStreetFormat

// Tests whether the specified phone no. has the correct format.
// The format is 'xx xxxx xxxx', where x is a digit.
function isValidPhoneNoFormat(phoneNo) {
	var phoneNoRe = /[0-9][0-9]\s[0-9][0-9][0-9][0-9]\s[0-9][0-9][0-9][0-9]/;
	return phoneNoRe.test(phoneNo);
} // endof isValidPhoneNoFormat

// Tests whether the specified mobile phone no. has the correct format.
// The format is 'xxxx xxx xxx', where x is a digit.
function isValidMobileNoFormat(mobileNo) {
	var mobileNoRe = /[0-9][0-9][0-9][0-9]\s[0-9][0-9][0-9]\s[0-9][0-9][0-9]/;
	return mobileNoRe.test(mobileNo);
} // endof isValidMobileNoFormat

// Tests whether the specified postal code has the correct format.
// The format is 'xxxx', where x is a digit.
function isValidPostcodeFormat(postcode) {
	var postcodeRe = /[0-9][0-9][0-9][0-9]/;
	return postcodeRe.test(postcode);
} // endof isValidPostcodeFormat

//HH
function validateHour(hour) {
	if (hour.length == 0)
		return false;
	if (hour >= 0 && hour < 24) {
		return true;
	}
	return false;
}

//MM
function validateMinute(minute) {
	if (minute.length == 0)
		return false;
	if (minute >= 0 && minute < 60) {
		return true;
	}
	return false;
}

//HH:MM
function validateTime(time) {
	colon = time.indexOf(':');//start from 0
	if (colon != 2 || colon == -1)
		return false;
	hour = new Number(time.substring(0, colon));
	minute = new Number(time.substring(colon + 1, time.length));

	tempHour = new Number(hour).toString();
	tempMinute = new Number(minute).toString();
	if (tempHour != hour || tempMinute != minute) {
		return false;
	}
	if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60) {
		return false;
	}
	return true;
}

//dd/mm/yyyy
function validateDate(date) {
	var validDateRE = /(\d{2})\/(\d{2})\/(\d{4})/;
	if(!validDateRE.test(date))
		return false; 


	firstSlash = date.indexOf('/');
	secondSlash = date.indexOf('/', firstSlash + 1);

	if (firstSlash == -1 || secondSlash == -1)
		return false;

	day = date.substring(0, firstSlash);
	month = date.substring(firstSlash + 1, secondSlash);
	year = date.substring(secondSlash + 1, date.length);

	if (year.length != 4)
		return false;

	if (day.length == 1)
		day = "0" + day;

	if (month.length == 1)
		month = "0" + month;

	var DayArray = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var MonthArray = new Array("01","02","03","04","05","06","07","08","09","10","11","12");

	/* Check Valid Year */
	var filter=/[0-9]{4}/;
	if (! filter.test(year)) {
		return false;
	}
	/* Check Valid Month */
	filter=/01|02|03|04|05|06|07|08|09|10|11|12/ ;
	if (! filter.test(month)) {
		return false;
	}
	/* Check For Leap Year */
	var N = Number(year);
	if ( ( N%4 == 0 && N%100 !=0 ) || ( N%400 == 0 ) ) {
		DayArray[1]=29;
	}
	/* Check for valid days for month */
	for (var ctr = 0; ctr <= 11; ctr++) {
		if (MonthArray[ctr] == month) {
			if ( day <= DayArray[ctr] && day > 0 ) {
				return true;
			} else {
				return false;
			}
		}
	}
	return false;
}

function trim(string) {
	re = /^\s+|\s+$/;
	return string.replace(re, "");
}

/*
function isNumber(string) {
	//filter=/01|02|03|04|05|06|07|08|09|10|11|12/ ;
	//if (! filter.test(month)) {
	//	return false;
	//}

	return true;
}
*/

function validateEmail(address) {
	address = trim(address);
	if (address.length == 0)
		return true;
	/// Number of '@' chars present in input string.
	var  numAtChars;
	/// The part of input address before the '@' character.
	var  userNameIn;
	/// The part of input address after the '@' character.
	var  domainNameIn;
	/// Holds the fields of the entered address, delimitted by '@' chars.
	var  addressFields = new Array();
	/// Divide the input email address into fields.
	///  Note that the array "addressFields" will have one more element
	///  than the number of '@' signs in the input string.
	/// IE4 handles this OK with '@' as last character but NS4,4.5 and Opera 3.5 don't.
	addressFields = address.split( '@' );
	numAtChars = addressFields.length - 1;
	if ( address == "" )
		return false;
	else if ( numAtChars  ==  0 )
		return false;
	else if ( numAtChars  >  1 )
		return false;
	else if ( addressFields[0] == "" )
		return false;
	else if ( addressFields[1] == "" )
		return false;
	else
		{
		userNameIn   = addressFields[0];
		domainNameIn = addressFields[1];

		if ( userNameIn.indexOf( " " ) != -1 )
			return false;
		else if ( domainNameIn.indexOf( " " ) != -1 )
			return false;

		else if ( isStandardDomain( domainNameIn ) == false )
			return false;
		else
			return true;

		}
	return false;
}

function  isStandardDomain( domainIn )  {
	var  last4chars  =  domainIn.substring( domainIn.length-4, domainIn.length );

	var  last3chars  =  domainIn.substring( domainIn.length-3, domainIn.length );

	last4chars = last4chars.toUpperCase();

	var  countryCodePattern = /\.[a-zA-Z][a-zA-Z]/;

	if      ( last4chars == ".COM" ) return true;
	else if ( last4chars == ".EDU" ) return true;
	else if ( last4chars == ".GOV" ) return true;
	else if ( last4chars == ".NET" ) return true;
	else if ( last4chars == ".MIL" ) return true;
	else if ( last4chars == ".ORG" ) return true;
	else if ( last4chars == ".BIZ" ) return true;
	else if ( last3chars.search( countryCodePattern )   !=  -1 )
		return true;

	return  false;

}

function trimLeadingZero(number) {
	while (number.charAt(0) == "0") {
		if (number.length == 1)
			return number;
		number = number.substring(1, number.length);
	} // endof if
	
	// Returns the original parameter.
	return number;
} // endof trimLeadingZero

function isInteger(thenumber) {
	newnumber = trimLeadingZero(thenumber);	//to avoid octal (base-8)
	if (isNaN(newnumber)) {
		// The specified parameter is not a valid number.
		return false;
	} // endof if
	// The specified parameter is indeed a number.
	if (parseInt(newnumber) == parseFloat(newnumber))
		return true;//integer
	return false;//float
} // endof isInteger

function isNotNegativeInteger(thenumber) {
	return (isInteger(thenumber)) && (parseInt(trimLeadingZero(thenumber)) >= 0);
} // endof isPositiveInteger

function isPositiveInteger(thenumber) {
	return (isInteger(thenumber)) && (parseInt(trimLeadingZero(thenumber)) > 0);
} // endof isPositiveInteger

//deal with textarea copy from word doc
// ', ", keep adding to the array
var keys = [[8217, "'"], [8221, '"']]; 
function getReplacementChar(ch){
	var ascii_value = ch.charCodeAt(0);
//	alert(ch + ":" + ascii_value);
	for (j = 0; j < keys.length; j++) {
		if (ascii_value == keys[j][0]){
			return keys[j][1];
		}	
	}
	return ch;
} // endof getReplacementChar

function convertSymbol(text) {
	var new_text = '';
	for (i = 0; i < text.length; i++) {
		new_text += getReplacementChar(text.charAt(i));
	} // endof for
	return new_text;
} // endof convertSymbol

// Tests whether the specified text contains non-ascii character.
function containNonAsciiChar(text) {
    for (i = 0; i < text.length; i++) {
        var ascii_value = text.charCodeAt(i);              
        if (ascii_value > 127) {
            alert('The specified character: '+text.charAt(i)+' is invalid.');
            return true;
        } // endof if
    } // endof for
    
    return false;
} // endof containNonAsciiChar