/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}


/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}

function getDate() {
	var d=new Date()
	var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
	var year=d.getFullYear()
	year=year.toString()
	year=year.substring(0,4)
	
	document.write("<b>"+d.getDate() + "  ")
	document.write(monthname[d.getMonth()] + "  ")
	document.write(year + "</b>")
}


/*
 * Basic validation & misc. usefull functions script file
 *
 * This JavaScript file provide all the basic function to
 * validating user input and other usefull functions .
 *
 * Created by Mark Frederik - kuvra@yahoo.com
 *
 */


/*
 * Check if the textfield has a value or not
 *
 * @return	booelan		true if it has a value, false if otherwise
 *
 * @param		String		fieldName		name of the text type input object
 * @param		String		alertMsg		Message that shows if it's has no value
 */
function hasValue(fieldName,alertMsg) {
	if ( fieldName.value.length == 0 ) {
		alert(alertMsg);
		fieldName.focus();
		return false;
	} else return true;
}


/*
 * Set a value to a text type input object
 *
 * @param		String		fieldName		name of the text type input object
 * @param		String		theValue		the value you want
 */
function setValue(fieldName,theValue) {
	fieldName.value = theValue;
}


/*
 * erase or clear user input in a text type input object
 *
 * @param		String		fieldName		name of the text type input object you want to clear it's value
 */
function setEmpty(fieldName) {
	fieldName.value = "";
}

/*
 * Check if user input a valid email address
 *
 * @return	int				true if it has a valid email address, false if otherwise
 *
 * @param		String		fieldName		name of the text type input object
 * @param		String		alertMsg		Message that shows if it's invalid
 *
 * Example of invalid email addresses this function can handle :
 * - @                - @email			- user@.
 * - @email.com       - user				- user@email.
 * - user@            - user@.com		- user@@email.com
 */
function isEmail(fieldName, alertMsg) {
	var idx1 = fieldName.value.indexOf("@");
	var status = 1;
	if ( idx1 == -1 ) status = 0;	
	else {
		var idx2 = fieldName.value.lastIndexOf("@");
		if (fieldName.value.substring(0,idx1).length == 0) status = 0;
		if (idx1 != idx2) status = 0;
		else {
			idx2 = fieldName.value.indexOf(".");
			if (idx2 < idx1 ) status = 0;
			else {
				if (fieldName.value.substring(idx1+1,idx2).length == 0 ) status = 0;
				if (fieldName.value.substring(idx2+1,fieldName.value.length).length == 0 ) status = 0;
			}
		}
	}
	if (!status) {
		alert(alertMsg);
		fieldName.focus();
	}
	return status;
}


/*
 * Check if length of the value that user entry < than value of minLen specified
 *
 * @return	boolean		true if it is equal or above the specified value of minLen
 *
 * @param		String		fieldName	Name of the text type input object
 * @param		int				minLen		Minimum length for the value that user input
 * @param		String		alertMsg	Message that shows if length of user's input below minLen
 */
function minLength(fieldName, minLen, alertMsg) {
	if (fieldName.value.length < minLen){
		alert(alertMsg);
		fieldName.focus();
		return false;
	} else return true;
}


/*
 * Check if length of the value that user entry > than value of maxLen specified
 *
 * @return	boolean		true if it is equal or above the specified value of minLen
 *
 * @param		String		fieldName	Name of the text type input object
 * @param		int				maxLen		Maximum length for the value that user input
 * @param		String		alertMsg	Message that shows if length of user's input below minLen
 */
function maxLength(fieldName, maxLen, alertMsg) {
	if (fieldName.value.length > maxLen){
		alert(alertMsg);
		fieldName.focus();
		return false;
	} else return true;
}


/*
 * Check if this is a valid date value or not
 *
 * This function does not check if the value of day > 31, neither check
 * if the value of month > 12, you must check this for yourself before 
 * passing them this function
 *
 * @return	int		true if it is a valid date value, false if otherwise
 *
 * @param		int		day			Value of the day   ( 1 .. 31 )
 * @param		int		month		Value of the month ( 1 .. 12 )
 * @param		int		year		Value of the year
 */
function isDate(day, month, year) {
	var status=1;
	if (( month == 2 ) && ( day > 27 )) {
		if (( year % 4 == 0 ) && ( day > 29 )) {
			status = 0;
			alert("There is only 29 days for February " + year);
		}
		if (( year % 4 != 0 ) && ( day > 28 )) {
			status = 0;
			alert("There is only 28 days for February " + year);
		}
	}

	if ((( month == 4  ) || ( month  == 6 ) || ( month == 9 ) || ( month == 11 )) && ( day > 30 )) {
		status = 0;
		alert("There is only 30 days for the current month");
	}
	return status;
}


/*
 * Check if value of this field is in alphabet only ( A .. Z )
 *
 * @return	int				true if the value is in alphabet, false if others character exist
 *
 * @param		String		Fieldname of the text type input object
 */
function isAlphabet(fieldName) {
	var i=0;
	var status = 1;
	var valStr = fieldName.value.toUpperCase();
	for (i=0 ; i<fieldName.value.length ; i++) {
		if ((valStr.substring(0,i+1) < "A") || (valStr.substring(0,i+1) > "Z")) {
			status = 0;
			break;
		}
	}
	return status;
}


/*
 * Check if value of this field is in numeric entry only ( 0 .. 9 )
 *
 * @return	int				true if the value is in numeric, false if others character exist
 *
 * @param		String		Fieldname		name of the text type input object
 */
function isNumeric(fieldName) {
	var i = 0;
	var status = 1;
	for (i=0 ; i<fieldName.value.length ; i++) {
		if ((fieldName.value.substring(i,i+1) < "0") || (fieldName.value.substring(i,i+1) > "9")) {
			status = 0;
			break;
		}
	}
	return status;
}


/*
 * Erase all spaces on the left of the input string value
 *
 * @return	String		a string with all the spaces on the left erased
 *
 * @param		String		fieldNameValue	value / string you want to erase
 */
function leftTrim(fieldNameValue) {
	var tmpStr = fieldNameValue;
	while (tmpStr.substring(0,1) == " ") {
		tmpStr = tmpStr.substring(1,tmpStr.length);	
	}
	return tmpStr;
}


/*
 * Erase all spaces on the right of the input string value
 *
 * @return	String		a string with all the spaces on the right erased
 *
 * @param		String		fieldNameValue	value or string you want to erase
 */
function rightTrim(fieldNameValue) {
	var tmpStr = fieldNameValue;
	while (tmpStr.substring(tmpStr.length-1,tmpStr.length) == " ")	{
		tmpStr = tmpStr.substring(0,tmpStr.length-1);	
	}
	return tmpStr;
}


/*
 * Erase all spaces on the left and right of the input string value
 *
 * @return	String		a string with all the spaces on the left and right erased
 *
 * @param		String		fieldNameValue	value or string you want to erase
 */
function trim(fieldNameValue) {
	var tmpStr = leftTrim(fieldNameValue);
	tmpStr = rightTrim(tmpStr);
	return tmpStr;
}


/*
 * Check whether one of the character in charList value exist on the fieldName value / entry
 *
 * @return	int				true if a character in charList exist,
 *										false if any char in charList doesn't exist in fieldName value.
 *
 * @param		String		fieldName			name of the text type input object
 * @param		String		charList			list of character(s) you want to check if it's exist or not
 *
 * example of usage :
 * thereIsNo(regForm.nameInput,"@#!$");		
 * - return true if character "@" or "#" or "!" or "$" not found in user input
 */
function thereIsNo(fieldName,charList) {
	var i = 0;
	var j = 0;
	var tmpChr = "A"
	var status = 1;
	for (i=0 ; i<fieldName.value.length ; i++) {
		tmpChr = fieldName.value.substring(i,i+1);
		for (j=0 ; j<charList.length ; j++) {
			if (tmpChr == charList.substring(j,j+1)) {
				status = 0;
				alert("This input cannot contain \"" + charList.substring(j,j+1) + "\" characater(s).\n Please erase it");
				fieldName.focus();
				break;
			}
		}
		if (!status)  break;
	}
	return status;
}

/*
 * check if there is any space in user input
 *
 * @return	int				true if there is a space in user input
 *
 * @param		String		fieldName		name of the text type input object
 * @param		String		alertMsg		Message that shows if there is a space in user input
 */
function noSpace(fieldName,alertMsg) {
	var i = 0;
	var status = 1;
	for (i=0 ; i<fieldName.value.length ; i++ ) {
		if (fieldName.value.substring(i,i+1) == " ") {
			alert("This input cannot contain space(s)");
			fieldName.focus();
			status = 0;
			break;
		}
	}
	return status;
}


function displaymessage()
{
if (confirm('Data sudah ada di bookmark anda')) {
		window.location.href = 'index.php?view=booked';
	}
}

function checkValidation(formInput) {

    if (typeof(formInput) != "object") {
        alert("Validation not supported on this browser.");
        return(false);
    }

    var message;

    if (stringEmpty(formInput.value)) {
        message = "Error! Alamat email kosong.";
        alert(message);
    } else if (noAtSign( formInput.value )) {
        message = "Error! Alamat email \"" + formInput.value + "\" tidak terdapat '@' karakter.";
        alert(message);
    } else if (nothingBeforeAt(formInput.value)) {
        message = "Error! Alamat email \"" + formInput.value;
        message += "\" tidak ada karakter sebelum '@'";
        alert(message);
    } else if (noLeftBracket(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" contains a right square bracket ']',\nbut no corresponding left square bracket '['.";
        alert(message);
    } else if (noRightBracket(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" contains a left square bracket '[',\nbut no corresponding right square bracket ']'.";
        alert( message);
    } else if (noValidPeriod(formInput.value)) {
        message = "Error! Alamat email \"" + formInput.value + "\" harus ada karakter ('.') .";
        alert(message);
    } else if (noValidSuffix(formInput.value)) {
        message = "Error! Alamat email \"" + formInput.value;
        message += "\" setelah '@' alamat domain name tidak lengkap.";
        alert(message);
    } else {
		return(true);
    }

    var objType = typeof(formInput.focus);
    if (objType == "object" || objType == "function") {
         formInput.focus();
    }

    return (false);
}


function stringEmpty (formField) {
    // CHECK THAT THE STRING IS NOT EMPTY
    if ( formField.length < 1 ) {
        return ( true );
    } else {
        return ( false );
    }
}

function noAtSign (formField) {
    // CHECK THAT THERE IS AN '@' CHARACTER IN THE STRING
    if (formField.indexOf ('@', 0) == -1) {
        return ( true )
    } else {
        return ( false );
    }
}

function nothingBeforeAt (formField) {
    // CHECK THERE IS AT LEAST ONE CHARACTER BEFORE THE '@' CHARACTER
    if ( formField.indexOf ( '@', 0 ) < 1 ) {
        return ( true )
    } else {
        return ( false );
    }
}

function noLeftBracket (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR LEFT BRACKET
    if ( formField.indexOf ( '[', 0 ) == -1 && formField.charAt (formField.length - 1) == ']') {
        return ( true )
    } else {
        return ( false );
    }
}

function noRightBracket (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR RIGHT BRACKET
    if (formField.indexOf ( '[', 0 ) > -1 && formField.charAt (formField.length - 1) != ']') {
        return ( true );
    } else {
        return ( false );
    }
}

function noValidPeriod (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf ( '@', 0 ) > 1 && formField.charAt (formField.length - 1 ) == ']')
        return ( false );

    // CHECK THAT THERE IS AT LEAST ONE PERIOD IN THE STRING
    if (formField.indexOf ( '.', 0 ) == -1)
        return ( true );

    return ( false );
}

function noValidSuffix(formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf('@', 0) > 1 && formField.charAt(formField.length - 1) == ']') {
        return ( false );
    }

    // CHECK THAT THERE IS A TWO OR THREE CHARACTER SUFFIX AFTER THE LAST PERIOD
    var len = formField.length;
    var pos = formField.lastIndexOf ( '.', len - 1 ) + 1;
    if ( ( len - pos ) < 2 || ( len - pos ) > 4 ) {
        return ( true );
    } else {
        return ( false );
    }
}