/**
 * Title: 		JavaScript Utilities
 * 
 * Project:    	mikeshomepage 4.0
 * Created by: 	Mike
 * Created on: 	27.10.2006
*/

/**
 * Show an alert box with the given message
 * @param msg The message
 */
function showMessage(msg) {
    // new line break after message text
    postfix = '\n ';
    // show message in alert box
    alert(msg+postfix);
}
/**
 * Displays an error message alert box
 * @param msg The error message
 * @param func The name of the function the error occured
 * @param origin The function/script name which causes the error
 */
function showErrorMessage(msg, func, origin) {
    msg  = 'Error: '+msg+'\n';
    msg += 'Function: '+func+'.js'+'\n';
    msg += 'File: '+origin+'.js';
    showMessage(msg);
}
/**
 * Displays an Ajax error message alert box
 * @param statusCode The response status code
 * @param statusText The response status text
 * @param script The called script
 */
function showAjaxErrorMessage(statusCode, statusText, script) {
    msg  = 'Error in Ajax request: \n\n';
    msg += 'StatusCode:\t'+statusCode+'\n';
    msg += 'StatusText:\t'+statusText+'\n';
    msg += 'Script:\t\t    '+script;
    showMessage(msg);
}
/**
 * Checks for valid email address
 * @param email The email address
 * @return True if valid otherwise false
 */
function validateEmail(email) {
//    matchString = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]{2,3}$/
//    matchString = /^\w+([\.-]?\w+)*@\w+([\.-]*\w+)*(\.\w{2,3})+$/
    matchString = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
    if (matchString.test(email)){
        return true;
    } else {
        return false;
    }
}
/**
 * Checks for valid URLs
 * @param url The url
 * @return True if valid otherwise false
 */
function validateURL(url) {
/*    matchString = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
    matchString = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;*/
    matchString = /^(ftp|https?):\/\/+(www\.)?[a-z0-9\-\.]{3,}\.[a-z]{2,3}$/;
    if (matchString.test(url)){
        return true;
    } else {
        return false;
    }
}
/**
 * Checks for valid password
 * @param password The password
 * @return True if valid otherwise false
 */
function validatePassword(url) {
    if (url.length>7 && url.length<17) {
        return true;
    } else {
        return false;
    }
}
/**
 * Displays the loading icon and hides the given element (optional)
 * @param icon The icon element
 * @param element The element to hide (optional)
 */
function showLoading(icon, element) {
    if (element) {
        element.style.display = 'none';
    }
    icon.style.display = 'inline';
}
/**
 * Hides the loading icon and display the given element (optional)
 * @param icon The icon element
 * @param element The element to display (optional)
 */
function hideLoading(icon, element) {
    icon.style.display = 'none';
    if (element) {
        element.style.display = 'inline';
    }
}
/**
 * Returns a date string in format "d.m.y H:i"
 * @return The date string
 */
function getDateString() {
	var date = new Date();
	var day = date.getDate();
	day = Number(day)<10?'0'+day:day;
	var month = Number(date.getMonth())+1;
	month = month<10?'0'+month:month;
	var year = String(date.getFullYear()).substring(2,4);
	var hours = Number(date.getHours());
	housrs = hours<10?'0'+hours:hours;
	var minutes = Number(date.getMinutes());
	minutes = minutes<10?'0'+minutes:minutes;
	return day+'.'+month+'.'+year+' '+hours+':'+minutes;
}
