//------------------------------------------------------------------------------
// Project		: sc-digital
// Filename		: script.common.js
// Developer	: Niel Astle
// Company		: Sc.Digital.Solutions
// Created		: 2005/07/06
// Copyright    : Sc.Digital.Solutions
// Description  : Commonly used javascript functions
//------------------------------------------------------------------------------
// DEVELOPMENT
/*
function GlobalErrorHandler(the_msg, the_url, the_linenumber){
    document.writeln('<b>Javascipt error</b><br>');
    document.writeln('<br><b>Error message= </b>'+the_msg);
    document.writeln('<br><b>URL= </b>'+the_url);
    document.writeln('<br><b>Line Number= </b>'+the_linenumber);
    return true
}
window.onerror = GlobalErrorHandler;
*/

//------------------------------------------------------------------------------
// HTML Editor
var HTMLEditor = function(the_html, the_parent_div) {
    // init default values
    this.html = the_html;
    this.div = document.getElementById(the_parent_div);
    
    // init event handlers
    this.div.onclick = function() { alert('clicked'); };
}

HTMLEditor.prototype.refresh = function() {
    this.div.innerHTML = this.html;
}

HTMLEditor.prototype.bold = function() {
}


//------------------------------------------------------------------------------
function ClearValues() {
    for (i = 0; i < arguments.length; i++) document.getElementById(arguments[i]).value = '';
}

//------------------------------------------------------------------------------
// Toggle tree image node between collapse.gif & expand.gif
function ToggleTreeNodeImg(the_element_id, the_path) {
    the_element = document.getElementById(the_element_id);
    collapse_signature = the_element.src.substr(the_element.src.length-12, 12);
    if (collapse_signature == 'collapse.gif') the_element.src = the_path + 'expand.gif';
    else the_element.src = the_path + 'collapse.gif';
}

//------------------------------------------------------------------------------
// Capture mouse position
mouse_x = 0;
mouse_y = 0;
document.onmousemove = GetMousePosition;
function GetMousePosition(e) {
    mouse_x = event.clientX + document.body.scrollLeft;
    mouse_y = event.clientY + document.body.scrollTop;
}

//------------------------------------------------------------------------------
// Appends '-over' to the the_element_id's classname
function ElementOver(the_element_id) {
    the_element = document.getElementById(the_element_id);
    over_signature = the_element.className.substr(the_element.className.length-5, 5);
   if (over_signature != '-over') {
        the_element.className = the_element.className + '-over';
    }
}

//------------------------------------------------------------------------------
// Appends '_f2' to the the_element_id's classname
function BUT_Over(the_element_id) {
    the_element = document.getElementById(the_element_id);
    the_length = the_element.src.length;
    the_element.src = the_element.src.substr(0, the_length-4);
    the_element.src = the_element.src+'_f2.gif';
}

//------------------------------------------------------------------------------
// Removes '_f2' from the the_element_id's classname
function BUT_Out(the_element_id) {
    the_element = document.getElementById(the_element_id);
    the_length = the_element.src.length;
    the_element.src = the_element.src.substr(0, the_length-7);
    the_element.src = the_element.src+'.gif';
}

//------------------------------------------------------------------------------
// Removes '-over' from the the_element_id's classname
function ElementOut(the_element_id) {
    the_element = document.getElementById(the_element_id);
    over_signature = the_element.className.substr(the_element.className.length-5, 5);
    if (over_signature == '-over') {
        the_element.className = the_element.className.slice(0, the_element.className.length-5);
    }
}

//------------------------------------------------------------------------------
// Sets a element's display style to 'block' - Show it
function ToggleElement(the_element_id) {
    the_element = document.getElementById(the_element_id);
    if (the_element.style.display != 'block') the_element.style.display = 'block';
}

//------------------------------------------------------------------------------
// Sets a element's display style to 'block' - Show it
function ShowElement(the_element_id, the_header_id, the_img_id) {
    the_element = document.getElementById(the_element_id);
    the_header = document.getElementById(the_header_id);
    the_image = document.getElementById(the_img_id);
    
    if (the_element.style.display != 'block') {
        the_element.style.display = 'block';
        the_header.style.background = '#EBB79E';
        the_image.src = './media/imgs_faq/minus.gif';
    }
    else {
        the_element.style.display = 'none';
        the_header.style.background = '#FBF1EC';
        the_image.src = './media/imgs_faq/plus.gif';
    }
}

//------------------------------------------------------------------------------
// Sets a element's display style to 'inline' - Show it
function ShowElementInline(the_element_id) {
    the_element = document.getElementById(the_element_id);
    if (the_element.style.display != 'inline') the_element.style.display = 'inline';
}

//------------------------------------------------------------------------------
// Sets a element's display style to 'none' - Hide it
function HideElement(the_element_id) {
    the_element = document.getElementById(the_element_id);
    if (the_element.style.display != 'none') the_element.style.display = 'none';
}

//------------------------------------------------------------------------------
// Sets an array of elements' display style to 'none' - Hide them
function HideElements(the_elements_array) {
    array_length = the_elements_array.length;
    for (var i = 0; i < array_length; i++) HideElement(the_elements_array[i]);
}

//------------------------------------------------------------------------------
// Show an element and hide an array of elements
function ShowHideElements(to_show_element_id, to_hide_elements_array) {
    array_length = to_hide_elements_array.length;
    for (var i = 0; i < array_length; i++)
        if (to_hide_elements_array[i] != to_show_element_id) HideElement(to_hide_elements_array[i]);
    ShowElement(to_show_element_id);
}

//------------------------------------------------------------------------------
// Toggle element hidden & visible
function ToggleElement(the_element_id, block_style) {
    the_element = document.getElementById(the_element_id);
    if (the_element.style.display == 'none') the_element.style.display = block_style;
    else the_element.style.display = 'none';
}

//------------------------------------------------------------------------------
// Changes the class of an element
function ChangeClass(the_element_id, the_class) {
    the_element = document.getElementById(the_element_id);
	if (the_element.className != the_class) the_element.className = the_class;
}

//------------------------------------------------------------------------------
// Gives the user a confirmation box before directing to a specified url
function ConfirmOpenPage(the_message, the_url) {
	if (window.confirm(the_message)) OpenPage(the_url);
}

//------------------------------------------------------------------------------
// Gives the user a confirmation box before submitting the form to an url
function ConfirmSubmitPage(the_message, the_form, the_url) {
	if (window.confirm(the_message)) SubmitPage(the_form, the_url);
}

//------------------------------------------------------------------------------
// Directs the browser to the specified url
function OpenPage(the_url) {
	document.location.replace(the_url);
}

//------------------------------------------------------------------------------
// Displays a not implemented error message
function NotImplemented() {
    alert('Not Implemented');
    return false;
}

//------------------------------------------------------------------------------
month_names = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
month_days = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
day_names = new Array('Sunday', 'Monday', 'Thuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
date_today = new Date();
date_today_year = date_today.getFullYear();
date_today_month = date_today.getMonth();
date_today_date = date_today.getDate();
//------------------------------------------------------------------------------
// Determine a style based on the given date
function GetDateStyle(the_date) {
    the_year = the_date.getFullYear();
    the_month = the_date.getMonth();
    the_date2 = the_date.getDate();
    the_day = the_date.getDay();
    
    style = '-normal';
    if (the_day == 0 || the_day == 6) style = '-weekend';
    if (date_today_year == the_year && date_today_month == the_month && date_today_date == the_date2) style = '-today';
    
    return style;
}

//------------------------------------------------------------------------------
// Converts the date string(YYYY-MM-DD) to a date object
function ConvertDate(date_str) {
    date_arr = date_str.split('-',3);
    the_date = new Date(date_arr[0], date_arr[1]-1, date_arr[2]);
    return the_date
}

//------------------------------------------------------------------------------
// Converts the date to a numerical string
function ConvertDateNumerical(the_date) {
    date_text = the_date.getFullYear() + '-' + (the_date.getMonth()+1) + '-' + the_date.getDate();
    return date_text;
}

//------------------------------------------------------------------------------
// Converts the date to a text string
function ConvertDateText(the_date) {
    date_text = the_date.getDate() + ' ' + month_names[the_date.getMonth()] + ' ' + the_date.getFullYear();
    return date_text;
}

//------------------------------------------------------------------------------
// Opens an url in a new window
new_window_no = 0;
function OpenURLWindow(the_url, the_width, the_height) {
    // Keep track how many windows where opened
    new_window_no++;
    
    // Create window
    new_window = window.open(the_url ,'new_window_'+new_window_no,'toolbar=no,status=no,resizable=no,directories=no,location=no,menubar=no,scrollbars=yes,width='+the_width+',height='+the_height);

    // Movie window to top right of screen
    start_x = Math.floor(window.screen.width - the_width - 10);
    start_y = 0;
    new_window.moveTo(start_x, start_y);
}

//------------------------------------------------------------------------------
// Opens an image in a new window
function OpenImageWindow(the_image, the_image_name, the_width, the_height) {
    // Keep track how many windows where opened
    new_window_no++;

    // Scale image to fit on user's screen
    max_width = window.screen.width - 50;
    max_height = window.screen.height - 100;
    if (the_width > max_width) {
        the_height = Math.floor((max_width / the_width) * the_height);
        the_width = the_width;

    }
    if (the_height > max_height) {
        the_width = Math.floor((max_height / the_height) * the_width);
        the_height = max_height;
    }
    
    // Create window
    image_window = window.open('','new_window_'+new_window_no,'toolbar=no,status=no,resizable=no,directories=no,location=no,menubar=no,scrollbars=no,width='+the_width+',height='+the_height);
    image_window.document.writeln('<html>');
    image_window.document.writeln('<head><title>'+the_image_name+'</title></head>');
    image_window.document.writeln('<style type="text/css">body { font: 12px Arial; color: #696969; margin: 0px; padding: 0px; background-color: #FFFFFF; }</style>');
    image_window.document.writeln('<body><img src="'+the_image+'" width="'+the_width+'" height="'+the_height+'"></body>');
    image_window.document.writeln('</html>');
    
    // Movie window to center of screen
    centerX = Math.floor((window.screen.width / 2) - (the_width / 2));
    centerY = Math.floor((window.screen.height / 2) - (the_height / 2));
    image_window.moveTo(centerX,centerY);
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
form_select_date_date = null;
form_select_date_element = null;
function form_select_date(the_element_id) {
    form_select_date_element = document.getElementById(the_element_id);
    if (form_select_date_element.value && form_select_date_element.value != '0000-00-00') form_select_date_date = ConvertDate(form_select_date_element.value);
    else form_select_date_date = new Date();
    form_select_date_refresh();
    the_element = document.getElementById('form_select_date');
    the_element.style.left = mouse_x;
    the_element.style.top = mouse_y;
    ShowElement('form_select_date');
}

function form_select_date_refresh() {
    the_date = form_select_date_date;

    // get month start date
    date_month_start = the_date;
    date_month_start.setDate(1);
    day_start = date_month_start.getDay();
    day_end = month_days[the_date.getMonth()];

    // setup calander
    the_calander = document.getElementById('form_select_calander');
    source = '';
    source += '<h3>' + month_names[the_date.getMonth()] + ' ' + the_date.getFullYear() + '</h3>';
    source += '<table cellpadding="0" cellspacing="0">';
    source += '<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr><tr>';
    current_day = 0;

    // first calander line
    for (i = 0; i < day_start; i++) source += '<td></td>';

    tdate = the_date;
    for (i = day_start; i < 7; i++) {
        current_day++;
        tdate.setDate(current_day);
        style = GetDateStyle(tdate);
        source += '<td id="day_' + current_day + '" class="day' + style + '" onmouseover="ElementOver(\'day_' + current_day + '\');" onmouseout="ElementOut(\'day_' + current_day + '\');" onclick="form_select_date_select(' + current_day + ');">' + current_day + '</td>';
    }

    // calander lines from 2nd
    for (i = 0; current_day < day_end; i++) {
        if (i % 7 == 0) source += '</tr><tr>';
        current_day++;
        tdate.setDate(current_day);
        style = GetDateStyle(tdate);
        source += '<td id="day_' + current_day + '" class="day' + style + '" onmouseover="ElementOver(\'day_' + current_day + '\');" onmouseout="ElementOut(\'day_' + current_day + '\');" onclick="form_select_date_select(' + current_day + ');">' + current_day + '</td>';
    }

    source += '</tr></table>';

    the_calander.innerHTML = source;
}

function form_select_date_prev() {
    the_year = form_select_date_date.getFullYear();
    the_month = form_select_date_date.getMonth();

    if (the_month <= 0) { the_month = 11; the_year--; }
    else the_month--;

    form_select_date_date.setFullYear(the_year);
    form_select_date_date.setMonth(the_month,1);

    form_select_date_refresh();
}

function form_select_date_next() {
    the_year = form_select_date_date.getFullYear();
    the_month = form_select_date_date.getMonth();

    if (the_month >= 11) { the_month = 0; the_year++; }
    else the_month++;

    form_select_date_date.setFullYear(the_year);
    form_select_date_date.setMonth(the_month,1);

    form_select_date_refresh();
}

function form_select_date_select(the_day) {
    form_select_date_date.setDate(the_day);
    form_select_date_element.value = ConvertDateNumerical(form_select_date_date);
    HideElement('form_select_date');
}

function form_select_date_today() {
    form_select_date_date = new Date();
    form_select_date_refresh();
}

function set_date_today(the_element_id) {
    date_today = new Date();
    the_element = document.getElementById(the_element_id);
    the_element.value = ConvertDateNumerical(date_today);
}

function set_date_clear(the_element_id) {
    the_element = document.getElementById(the_element_id);
    the_element.value = '0000-00-00';
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// TEST
function SubmitPage(the_form, the_action) {
	document.forms[the_form].action = the_action;
	if (document.forms[the_form].onsubmit != null) {
    	if (document.forms[the_form].onsubmit()) document.forms[the_form].submit();
    }
    else document.forms[the_form].submit();
}

function SubmitForm(the_form, the_action, the_onsubmit) {
	the_form = document.forms[the_form];
	the_form.action = the_action;
	the_form.onsubmit = the_onsubmit;
	if (the_form.onsubmit != null) {
    	if (the_form.onsubmit()) the_form.submit();
    }
    else the_form.submit();
}

function ChangeStyle(the_obj, new_style) {
	the_obj.className = new_style;
}

function ChangeColor(the_obj, new_color) {
	the_obj.style.color = new_color;
}

function ToggleBlockDisplay(block) {
    block_obj = document.getElementById(block);
    if (block_obj.style.display == 'none') block_obj.style.display = 'block';
    else block_obj.style.display = 'none';
}

//------------------------------------------------------------------------------

