
// THIS FILE IS COPYRIGHT BY BABYSOFT


// This function is use to check the info request form before it submit, 
// customize this function to fulfill your needs
function checkForm(){
    var input = '';

    if (document.getElementById('company_name').value == ''){
	alert ("Please provide your company name");
	document.getElementById('company_name').focus();
	return false;
    }
    else if (document.getElementById('contact_name').value == ''){
	alert ("Please provide your contact name");
	document.getElementById('contact_name').focus();
	return false;
    }
    else if (document.getElementById('phone1').value == '' &&
	document.getElementById('phone1').value.length != 3){
	alert ("Please provide your phone area code");
	document.getElementById('phone1').focus();
	return false;
    }
    else if (document.getElementById('phone2').value == '' &&
	document.getElementById('phone2').value.length != 3){
	alert ("Please provide complete phone number");
	document.getElementById('phone2').focus();
	return false;
    }
    else if (document.getElementById('phone3').value == '' &&
	document.getElementById('phone3').value.length != 4){
	alert ("Please provide complete phone number");
	document.getElementById('phone3').focus();
	return false;
    }
    else if (document.getElementById('email').value == ''){
	alert ("Please provide your email address");
	document.getElementById('email').focus();
	return false;
    }
    else if (document.getElementById('postalcode').value == ''){
	alert ("Please provide your Postal / Zip Code");
	document.getElementById('postalcode').focus();
	return false;
    }

    else{
	if (isPhone('phone1',3) == false){
	    return false;
	}
	if (isPhone('phone2',3) == false){
	    return false;
	}
	if (isPhone('phone3',4) == false){
	    return false;
	}
	if (isEmail('email') == false){
	    return false;
	}
	if (isWebsite('website',true) == false){
	    return false;
	}
	if (countMaxChar('msgarea') == false){
	    return false;
	}

	input = confirm("Is the above information correct?");
	return input;
    }
}


// check is the field all numbers
// param1: the id of the field required to check
// return: true if all value is digits, or empty. Otherwise false
function isNumber(id){
    var i=0;
    var number = '';

    if (document.getElementById(id) == null){
	alert ("formValidation.js->isNumber: Invalid id in parameter: (" + id + "), pleaes verify it and try again");
	return false;
    }

    number = document.getElementById(id).value;

    // do not deal with empty field
    if (number.length == 0){
	return true;
    }

    // check all digits now
    for (i=0; i < number.length; i++){
        // Error, this is not a number
        if ( ! ((number.charCodeAt(i) <= 57) && (number.charCodeAt(i) >= 48)) ){
            alert ("Invalid Entry, it can only enter digits!!!\nPlease try again.");
            document.getElementById(id).focus();
            return false;
        }
    }
    return true;
}


// Check to see is the postal code is correct format
// param1: the id of the postal code
// Return: true if the postal code is wrong format or is empty, otherwise true
//         it will also automatically assign uppercase letter back to the field
function isPostalCode(id){
    var i=0;
    var input = '';

    if (document.getElementById(id) == null){
	alert ("formValidation.js->isPostalCode: Invalid id in parameter: (" + id + "), pleaes verify it and try again");
	return false;
    }

    input = document.getElementById(id).value.toUpperCase();
    input = input.replace(/[ ]/g,'');

    // do not deal with empty field
    if (input.length == 0){
	return true;
    }

    if (input.length != 6){
	alert ("Invalid Postal Code!\nPlease enter a valid postal code.");
	document.getElementById(id).focus();
	return false;
    }

    for (i=0; i < input.length; i++){
        // must be upper case characters
        if (i == 0 || i == 2 || i == 4){
	    // Error, this is not a capital character
	    if ( ! ((input.charCodeAt(i) <= 90) && (input.charCodeAt(i) >= 65)) ){
		alert ("Invalid Postal Code!\nPlease enter a valid postal code.");
		document.getElementById(id).focus();
		return false;
	    }
        }
        // must be numbers
        else if (i == 1 || i == 3 || i == 5){
	    // Error, this is not a number
	    if ( ! ((input.charCodeAt(i) <= 57) && (input.charCodeAt(i) >= 48)) ){
		alert ("Invalid Postal Code!\nPlease enter a valid postal code.");
		document.getElementById(id).focus();
		return false;
	    }
        }
    }

    // everythings are fine, put the Postal code back to the input field
    document.getElementById(id).value = input;
    return true;
}


// Check to see is the postal code or Zip code and is in correct format
// param1: the id of the postal code / zip code
// Return: true if the postal code / zip code is wrong format or is empty, 
//         otherwise return true
//         it will also automatically assign uppercase letter back to the 
//         id field if it is a postal code
function isPostalorZipCode(id){
    var input = '';
    var zipCode = true;
    var postalCode = true;

    if (document.getElementById(id) == null){
	alert ("formValidation.js->isPostalorZipCode: Invalid id in parameter: (" + id + "), pleaes verify it and try again");
	return false;
    }

    input = document.getElementById(id).value.toUpperCase();
    input = input.replace(/[ ]/g,'');

    // do not deal with empty field
    if (input.length == 0){
	return true;
    }

    // Now Check Postal Code ------------------------
    if (input.length != 6){
	postalCode = false;
    }

    for (i=0; i < input.length; i++){
        // must be upper case characters
        if (i == 0 || i == 2 || i == 4){
	    // Error, this is not a character
	    if ( ! ((input.charCodeAt(i) <= 90) && (input.charCodeAt(i) >= 65)) ){
		postalCode = false;
	    }
        }
        // must be numbers
        else if (i == 1 || i == 3 || i == 5){
	    // Error, this is not a number
	    if ( ! ((input.charCodeAt(i) <= 57) && (input.charCodeAt(i) >= 48)) ){
		postalCode = false;
	    }
        }
    }


    // Now Check Zip code ----------------------------
    // check all digits now
    for (i=0; i < input.length; i++){
        // Error, this is not a number
        if ( ! ((input.charCodeAt(i) <= 57) && (input.charCodeAt(i) >= 48)) ){
            zipCode = false;
        }
    }

    // All Done --------------------------------------
    if (postalCode == false && zipCode == false){
	alert ("You have entered a code which is neither a valid Postal Code or a Zip Code!\nPlease verify it and try again.");
	document.getElementById(id).focus();
	return false;
    }
    else{
	// everythings are fine, put the Postal code back to the input field
	document.getElementById(id).value = input;
	return true;
    }
}



// check number part for business phone
// param1: the id of the phone number
// param2: the count on how may number it should be
// return: true if the phone number is valid, otherwise false
function isPhone(id, count){
    var i=0;
    var number = '';

    if (document.getElementById(id) == null){
	alert ("formValidation.js->isPhone: Invalid id in parameter: (" + id + "), pleaes verify it and try again");
	return false;
    }

    number = document.getElementById(id).value;

    // do not deal with empty field
    if (number.length == 0){
	return true;
    }

    // phone number is count digits
    if (number.length != count){
        alert ("Invalid Phone Number!\nPlease enter a valid phone number.");
        document.getElementById(id).focus();
        return false;
    }

    for (i=0; i < number.length; i++){
        // Error, this is not a number
        if ( ! ((number.charCodeAt(i) <= 57) && (number.charCodeAt(i) >= 48)) ){
            alert ("Invalid Phone Number!\nPlease enter a valid phone number.");
            document.getElementById(id).focus();
            return false;
        }
    }
    return true;
}





// validate email address
// param1: the id of the email field
// return: true if the email field is empty or correct, otherwise false
function isEmail(id){
    var input = '';

    if (document.getElementById(id) == null){
	alert ("formValidation.js->isEmail: Invalid id in parameter: (" + id + "), pleaes verify it and try again");
	return false;
    }

    input = document.getElementById(id).value;
    input = input.replace(/[ ]/g,'');

    if (input == ''){
        return true;
    }
    /* check valid email */
    else if (!((input.indexOf("@") == input.lastIndexOf("@") && input.indexOf("@")!=-1) && (input.indexOf(".")!=-1))){
        alert("The business email address is invalid.\nPlease re-enter your email address.");
	document.getElementById(id).focus();
        return false;
    }
    return true;
}



// validate the entry of the "Web Site" field
// param1: the id of the email field
// param2: see weather you wish to automatically put http:// prefix
//         false means no prefix, true means prefix
// return: true if empty or correct format, otherwise false
function isWebsite(id,prefix){
    var i=0;
    var input = '';

    if (document.getElementById(id) == null){
	alert ("formValidation.js->isWebsite: Invalid id in parameter: (" + id + "), pleaes verify it and try again");
	return false;
    }

    input = document.getElementById(id).value;
    input = input.replace(/[ ]/g,'');

    if (input == ''){
	return true;
    }


    for (i=0; i < input.length; i++){
        // Error, this is not a input
        if ( ! (input.indexOf('.')!=-1) ){
            alert ("Invalid Entry!\nPlease check for Web Site URL.");
            document.getElementById(id).focus();
            return false;
        }
    }
    
    // if not http:// at the top, at it automatically
    if (input.indexOf('http://') != 0){
	if (prefix == true){
	    document.getElementById(id).value = 'http://' + input;
	}
	return true;
    }
    return true;
}

// Check weather this is a valid month format or not
// param1: the month id you wish to check
// Return: true if the date is in valid format, otherwise false.
function isMonth(id){
    var input = '';
    var ary = new Array();

    if (document.getElementById(id) == null){
	alert ("formValidation.js->isMonth: Invalid id in parameter: (" + id + "), pleaes verify it and try again");
	return false;
    }

    input = document.getElementById(id).value;
    ary = input.split('-');

    // Do not deal with empty case.
    if (input == ''){
	return true;
    }

    // check weather this is a full number with ':' or not.
    // check all digits now
    for (i=0; i < input.length; i++){
        // Error, this is not a number
        if ( ! (((input.charCodeAt(i) <= 57) && (input.charCodeAt(i) >= 48))) &&
		(input.charCodeAt(i) != 45) ){
            alert ("Invalid Entry, it can only enter digits!!!\nPlease try again.");
            document.getElementById(id).focus();
            return false;
        }
    }

    if (ary.length != 2){
	alert ("Date Format Error (Valid Format: YYYY-MM). Please try again.");
	document.getElementById(id).focus();
	return false;
    }

    if (ary[0].length != 4){
	alert ("Date Format Error (Valid Format: YYYY-MM). Please try again.");
	document.getElementById(id).focus();
	return false;
    }

    if (ary[1] > 12 || ary[1] < 1){
	alert ("Date Format Error (Valid Format: YYYY-MM). Please try again.");
	document.getElementById(id).focus();
	return false;
    }

    // Value should now be correct, Restructure the format

    // first, deal with all the wield format
    temp = new Number(ary[0]);
    ary[0] = temp.toString();
    temp = new Number(ary[1]);
    ary[1] = temp.toString();

    // Now, see weather we need to re-do the format
    if (ary[1].length == 1){
	ary[1] = '0' + ary[1];
    }
    else if (ary[1].length == 0){
    	ary[1] = '01';
    }

    document.getElementById(id).value = ary[0]+'-'+ary[1];

    return true;


}


// Check weather this is a valid date or not
// param1: the date id you wish to check
// Return: true if the date is in valid format, otherwise false.
function isDate(id){
    var input = '';
    var ary = new Array();

    if (document.getElementById(id) == null){
	alert ("formValidation.js->isDate: Invalid id in parameter: (" + id + "), pleaes verify it and try again");
	return false;
    }

    input = document.getElementById(id).value;
    ary = input.split('-');

    // Do not deal with empty case.
    if (input == ''){
	return true;
    }

    // check weather this is a full number with ':' or not.
    // check all digits now
    for (i=0; i < input.length; i++){
        // Error, this is not a number
        if ( ! (((input.charCodeAt(i) <= 57) && (input.charCodeAt(i) >= 48))) &&
		(input.charCodeAt(i) != 45) ){
            alert ("Invalid Entry, it can only enter digits!!!\nPlease try again.");
            document.getElementById(id).focus();
            return false;
        }
    }

    if (ary.length != 3){
	alert ("Date Format Error (Valid Format: YYYY-MM-DD). Please try again.");
	document.getElementById(id).focus();
	return false;
    }

    if (ary[0].length != 4){
	alert ("Date Format Error (Valid Format: YYYY-MM-DD). Please try again.");
	document.getElementById(id).focus();
	return false;
    }

    if (ary[1] > 12 || ary[1] < 1){
	alert ("Date Format Error (Valid Format: YYYY-MM-DD). Please try again.");
	document.getElementById(id).focus();
	return false;
    }

    if (ary[2] > 31 || ary[2] < 1){
	alert ("Date Format Error (Valid Format: YYYY-MM-DD). Please try again.");
	document.getElementById(id).focus();
	return false;
    }


    // Value should now be correct, Restructure the format

    // first, deal with all the wield format
    temp = new Number(ary[0]);
    ary[0] = temp.toString();
    temp = new Number(ary[1]);
    ary[1] = temp.toString();
    temp = new Number(ary[2]);
    ary[2] = temp.toString();

    // Now, see weather we need to re-do the format
    if (ary[1].length == 1){
	ary[1] = '0' + ary[1];
    }
    else if (ary[1].length == 0){
    	ary[1] = '01';
    }

    if (ary[2].length == 1){
        ary[2] = '0' + ary[2];
    }
    else if (ary[2].length == 0){
    	ary[2] = '01';
    }

    document.getElementById(id).value = ary[0]+'-'+ary[1]+'-'+ary[2];

    return true;


}


// Check weather this is a valid credit card date or not MMYY
// param1: the date id you wish to check
// Return: true if the date is in valid format, otherwise false.
function isUCCDate(id){
    var input = '';
    var month = '';
    var year = '';
    var ary = new Array();

    if (document.getElementById(id) == null){
	alert ("formValidation.js->isUCCDate: Invalid id in parameter: (" + id + "), pleaes verify it and try again");
	return false;
    }

    input = document.getElementById(id).value;
    month = input.substr(0,2);
    year = input.substr(2,2);

    // Do not deal with empty case.
    if (input == ''){
	return true;
    }

    // check weather this is a full number with ':' or not.
    // check all digits now
    for (i=0; i < input.length; i++){
        // Error, this is not a number
        if ( ! (((input.charCodeAt(i) <= 57) && (input.charCodeAt(i) >= 48)))){
            alert ("Invalid Entry, it can only enter digits!!!\nPlease try again.");
	    document.getElementById(id).value = '';
            document.getElementById(id).focus();
            return false;
        }
    }

    if (input.length != 4){
	alert ("Date Format Error (Valid Format: MMYY). Please try again.");
	document.getElementById(id).focus();
	return false;
    }

    month = new Number(month);
    if (month > 12 || month < 1){
	alert ("Date Format Error (Valid Format: MMYY). Please try again.");
	document.getElementById(id).focus();
	return false;
    }

    return true;


}



// Check weather this is a valid time format or not (HH:MM / HH:MM:SS)
// param1: the date id you wish to check
// Return: true if the date is in valid format, otherwise false.
function isTime(id){
    var input = '';
    var ary = new Array();
    var temp;

    if (document.getElementById(id) == null){
	alert ("formValidation.js->isTime: Invalid id in parameter: (" + id + "), pleaes verify it and try again");
	return false;
    }

    input = document.getElementById(id).value;
    ary = input.split(':');

    // Do not deal with empty case.
    if (input == ''){
	return true;
    }

    // check weather this is a full number with ':' or not.
    // check all digits now
    for (i=0; i < input.length; i++){
        // Error, this is not a number
        if ( ! (((input.charCodeAt(i) <= 57) && (input.charCodeAt(i) >= 48))) &&
		(input.charCodeAt(i) != 58) ){
            alert ("Invalid Entry, it can only enter digits!!!\nPlease try again.");
            document.getElementById(id).focus();
            return false;
        }
    }


    if (ary.length != 2 && ary.length != 3){
	alert ("Time Format Error (Valid Format: HH:MM or HH:MM:SS). Please try again.");
	document.getElementById(id).focus();
	return false;
    }

    if (ary[0] > 23 || ary[0] < 0){
	alert ("Time Format Error (Valid Format: HH:MM or HH:MM:SS). Please try again.");
	document.getElementById(id).focus();
	return false;
    }

    if (ary[1] > 59 || ary[1] < 0){
	alert ("Time Format Error (Valid Format: HH:MM or HH:MM:SS). Please try again.");
	document.getElementById(id).focus();
	return false;
    }

    if (ary.length == 3){
       if (ary[2] > 59 || ary[2] < 0){
	    alert ("Time Format Error (Valid Format: HH:MM or HH:MM:SS). Please try again.");
	    document.getElementById(id).focus();
	    return false;
        }
    }


    // Value should now be correct, Restructure the format

    // first, deal with all the wield format
    temp = new Number(ary[0]);
    ary[0] = temp.toString();
    temp = new Number(ary[1]);
    ary[1] = temp.toString();
    if (ary.length == 3){
	temp = new Number(ary[2]);
	ary[2] = temp.toString();
    }

    // Now, see weather we need to re-do the format
    if (ary[0].length == 1){
	ary[0] = '0' + ary[0];
    }
    else if (ary[0].length == 0){
	ary[0] = '00';
    }

 
    if (ary[1].length == 1){
	ary[1] = '0' + ary[1];
    }
    else if (ary[1].length == 0){
	ary[1] = '00';
    }

    if (ary.length == 3){
        if (ary[2].length == 1){
            ary[2] = '0' + ary[2];
        }
        else if (ary[2].length == 0){
    	    ary[2] = '00';
        }
    }

    if (ary.length == 3){
        document.getElementById(id).value = ary[0]+':'+ary[1]+':'+ary[2];
    }else{
        document.getElementById(id).value = ary[0]+':'+ary[1];
    }
    return true;
}


// Check weather this is a decimal number or not (xx.xx)
// param1: the date id you wish to check
// Return: true if the date is in valid format, otherwise false.
function isDecimal(id){
    var input = '';
    var ary = new Array();
    var i=0;
    var number = -999;

    if (document.getElementById(id) == null){
	alert ("formValidation.js->isDecimal [Error]: Invalid id in parameter: (" + id + "), pleaes verify it and try again");
	return false;
    }

    input = document.getElementById(id).value;

    // Do not deal with empty case.
    if (input == ''){
	input = 0;
	// return true;
    }

    // check weather this is a full number with dot or not.

    // check all digits now
    /*for (i=0; i < input.length; i++){
        // Error, this is not a number
        if ( ! (((input.charCodeAt(i) <= 57) && (input.charCodeAt(i) >= 48))) &&
		(input.charCodeAt(i) != 46 && input.charCodeAt(i) != 45) ){
            alert ("Invalid Entry, it can only enter digits!!!\nPlease try again.");
            document.getElementById(id).focus();
            return false;
        }
    }*/

    number = new Number(input);

    // Still an error
    if (isNaN(number) == true){
        alert ("Invalid Entry, it can only enter digits!!!\nPlease try again.");
        document.getElementById(id).value = '0.00';
        document.getElementById(id).focus();
        return false;
    }

    document.getElementById(id).value = number.toFixed(2);

    return true;
}


// Calculate the maximum amount of characters
// param1: the id of the tax field
// return: true if empty or characters less then 255, otherwise false
function countMaxChar(id){
    var input = '';

    if (document.getElementById(id) == null){
	alert ("formValidation.js->countMaxChar: Invalid id in parameter: (" + id + "), pleaes verify it and try again");
	return false;
    }

    input = document.getElementById(id).value;

    if (input.length >= 255){
	 alert("This text field cannot be more than 254 Characters");
         document.getElementById(id).focus();
	 return false;
     }
     return true;
}


// pass to the next text box when a amount of character is reached, this 
// function will also handle left/right arrow button to make sure the 
// running behavour is smooth
// please note: 
//      1. param2 and param3 is optional but at lease have one, if not, 
//         there are no point for this function.
//      2. This function will take effect when the "count + 1" case 
//         occur, if you limited the size to "count" only, this function 
//         will not work
//      3. Suggested to be used in the event: onkeyup
// param1: the id of the counting text box
// param2: (optional) the id of the previous text box passing to
// param3: (optional) the id of the next text box passing to
// param4: the amount of character count to pass
// param5: the window event, always "event
// return: false if id error, otherwise true
function gotoNext(id,pid,nid,count,keyboardEvent){
    var input = '';
    var data_org = '';
    var data_next = '';

    if (document.getElementById(id) == null || (document.getElementById(pid) == null && document.getElementById(nid) == null)){
	alert ("formValidation.js->gotoNext: Invalid data in parameter: param1(" + id + "), param2(" + pid + "), param3(" + nid + "), param4(" + count + "), pleaes verify it and try again");
	return false;
    }
    if (!keyboardEvent){
	alert ("formValidation.js->gotoNext: Event Error, pleaes verify your event parameter and try again");
	return false;
    }
    // if previous id has error
    if (document.getElementById(pid) == null || pid == ''){
	pid = '';
    }
    // if next id has error
    if (document.getElementById(nid) == null || nid == ''){
	nid = '';
    }

    input = document.getElementById(id).value;

//    if (keyboardEvent.keyCode == "37" || keyboardEvent.keyCode == "39"){
//	document.getElementById(pid).focus();
	

//    }else{
        if (input.length > count && nid != ''){
    
    	// Construct the original data
        for (var i=0; i < count; i++){
            data_org = data_org + input.charAt(i);
        }
	// Construct the next field data
        for (var i=count; i < input.length; i++){
            data_next = data_next + input.charAt(i);
        }

    	// data_next = input.charAt(count);
        document.getElementById(id).value = data_org;
        document.getElementById(nid).focus();
        document.getElementById(nid).value = data_next;
    	return true;
         }
    
        // if the text box is empty, pass it back to the previous textbox
        if (input.length == 0 && pid != ''){
             document.getElementById(pid).focus();
             document.getElementById(pid).value = document.getElementById(pid).value;
    	 return true;
         }
//     }
     return true;
}


// Change the first character of the first word to capital
// The id of the word you wish to change
// Return: false if the id fail, otherwise true
//         This function will automatically place the new
//         word back to the id with first capital character
function firstCharCapital(id){
    var input = '';
    var tempArray = new Array();
    var tempArray_in = new Array();

    if (document.getElementById(id) == null){
	alert ("formValidation.js->firstCharCapital: Invalid id in parameter: (" + id + "), pleaes verify it and try again");
	return false;
    }

    input = document.getElementById(id).value;
    // do not deal with empty field
    if (input.length == 0){
	return true;
    }

    // Seperate every word
    tempArray = input.split(' ');
    for (var i=0; i < tempArray.length; i++){
	tempArray_in = tempArray[i].split('');
	tempArray[i] = '';
	for (var j=0; j < tempArray_in.length; j++){
	    if (j == 0){
		tempArray[i] += tempArray_in[0].toUpperCase();
	    }else{
		tempArray[i] += tempArray_in[j];
	    }
	}
    }
    // Join Back together
    $input = '';
    for (var i=0; i < tempArray.length; i++){
	if (i == 0){
	    input = tempArray[0];
	}else{
	    input += ' ' + tempArray[i];
	}
    }


    // everythings are fine, put the Postal code back to the input field
    document.getElementById(id).value = input;
    return true;


}




// Change the all the characters of to capital letter
// The id of the word you wish to change
// Param1: The id of the value you wish to do
// Return: false if the id fail, otherwise true
//         This function will automatically place the new
//         word back to the id with all capital characters
function allCharCapital(id){
    var input = '';
    var tempArray = new Array();
    var tempArray_in = new Array();

    if (document.getElementById(id) == null){
	alert ("formValidation.js->allCharCapital: Invalid id in parameter: (" + id + "), pleaes verify it and try again");
	return false;
    }

    input = document.getElementById(id).value;
    // do not deal with empty field
    if (input.length == 0){
	return true;
    }

    input = input.toUpperCase();

    // everythings are fine, put the Postal code back to the input field
    document.getElementById(id).value = input;
    return true;


}


// Change The Gender based on it's title
// Param1: The id of the title
// Param2: The id of the Gender
// Param3: The mapping of the gender vs title
//         Note: (format) titleIndex-titleName-genderid,titleIndex-titleName-genderid (e.g. 0-Mr.-0)
// Return: false if id fail, otherwise true
function titleToGender(tid, gid, genderMap){
    var input = '';
    var tvalue = '';
    var gvalue = '';
    var titleList = new Array();
    var title = new Array();
    
    if (document.getElementById(tid) == null){
	alert ("formValidation.js->titleToGender: Invalid title id in parameter: (" + tid + "), pleaes verify it and try again");
	return false;	
    }
    if (document.getElementById(gid) == null){
	alert ("formValidation.js->titleToGender: Invalid gender id in parameter: (" + gid + "), pleaes verify it and try again");
	return false;	
    }
    if (genderMap == ''){
	alert ("formValidation.js->titleToGender: Invalid gender mapping in parameter: (" + genderMap + "), pleaes verify it and try again");
	return false;
    }

    tvalue = document.getElementById(tid).options[document.getElementById(tid).selectedIndex].value;
    gvalue = document.getElementById(gid).options[document.getElementById(gid).selectedIndex].value;

    // So all id is correct, perform the job
    titleList = genderMap.split(',');
    for (var i=0; i < titleList.length; i++){
	title = titleList[i].split('-');
	if (title[0] == tvalue){
	    document.getElementById(gid).selectedIndex = title[2];
	}
    }
    return true;

}


// Master warn if page is required
// Warn page when the page contains ID: warnIndicator
function masterWarn(warnIndicator){
    if (document.getElementById(warnIndicator) != null){
	if (confirm("Perform this action will delete all your un-saved information.\nAre you sure you want to process?") == false){
	    return false;
	}else{
	    return true;
	}
    }else{
	return true;
    }

}


// Check Invalid Characters at all time
// This function is used to check is there exist any invalid characters in
// all the typings. It is expected to use in the body tag under onkeyup 
// event so that when a person types, it will already check
// Param1: Mode: 0/Empty: verbose mode, 1: quite mode
// Return false always
function checkInvalidChars(mode){
    var temp;

    // This is for Input Tag, we still need to check TextArea
    var inputobj = document.getElementsByTagName('input');
    for (var i=0; i < inputobj.length; i++){
      if (inputobj[i] != null && inputobj[i].type == 'text' && inputobj[i].value != null){
	if (inputobj[i].value.indexOf('"') != -1 || inputobj[i].value.indexOf("'") != -1){
	  if (mode == null || mode == '' || mode == '0'){
	    alert ("No (') or "+'(")'+" is allowed. All invalid characters will be automatically removed.");
	    // inputobj[i].focus();
          }
	  temp = inputobj[i].value;
	  temp = String(temp);
	  temp = temp.replace(/'/g,'');
	  temp = temp.replace(/"/g,'');
	  inputobj[i].value = temp;
	}
      }
    }

    // This is for text area
    textobj = document.getElementsByTagName('textarea');
    for (var i=0; i < textobj.length; i++){
      if (textobj[i] != null && textobj[i].value != null){
	if (textobj[i].value.indexOf('"') != -1 || textobj[i].value.indexOf("'") != -1){
	  if (mode == null || mode == '' || mode == '0'){
	    alert ("No (') or "+'(")'+" is allowed. All invalid characters will be automatically removed.");
	    // textobj[i].focus();
          }
	  temp = textobj[i].value;
	  temp = String(temp);
	  temp = temp.replace(/'/g,'');
	  temp = temp.replace(/"/g,'');
	  textobj[i].value = temp;
	}
      }
    }

    return false;
}


// Remove all the spaces for the id value field
// Param1: id of the value you want to remove space
// Note: the removed space value will be assign back into the value field
// Return: false.
function noSpace(id){
    var input = '';

    if (document.getElementById(id) == null){
	alert ("formValidation.js->noSpace: Invalid id in parameter: (" + id + "), pleaes verify it and try again");
	return false;
    }

    // input = document.getElementById(id).value.toUpperCase();
    input = document.getElementById(id).value;
    input = input.replace(/[ ]/g,'');
    document.getElementById(id).value = input;

}


