// JavaScript Document

//{AJAX -----------------------------------------------------------------------------
// this function will load a list of urls into a list of id's elements
function loadPages(urls,ids,callback){
    if (typeof callback == "undefined") {
        callback = function(){};
    }
    if(urls.length != 0){
        var url = urls.shift();
        var id = ids.shift();
        var process_html = "<div style='text-align:center;'>";
        process_html += "<img src='style/images/loader.gif' alt='loading...'/>";
        process_html += "</div>"
        document.getElementById(id).innerHTML=process_html;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            var content = new XMLHttpRequest();
        } else {// code for IE6, IE5
            var content = new ActiveXObject("Microsoft.XMLHTTP");
        }
        content.onreadystatechange = function() {
            if (content.readyState==4 && content.status==200) {
                document.getElementById(id).innerHTML=content.responseText;
                loadPages(urls,ids,callback);
            } else if (content.readyState==4 && (content.status==404 || content.status==500)) {
                display_error();
            }
        }
        content.open("GET",url,true);
        content.send();
    } else {
        callback();
    }
}

// this function will load a url into an id's element
function loadPage(str, id, process) {
    if (typeof process == "undefined") {
        process = true;
    }
    if(process){
        process_request();
    }
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        var content=new XMLHttpRequest();
    } else {// code for IE6, IE5
        var content=new ActiveXObject("Microsoft.XMLHTTP");
    }
    content.onreadystatechange = function() {
        if (content.readyState==4 && content.status==200) {
            document.getElementById(id).innerHTML=content.responseText;
            if(process){
                process_finish();
            }
        } else if (content.readyState==4 && (content.status==404 || content.status==500)) {
            if(process){
                process_finish();
            }
            display_error();
        }
    }
    content.open("GET",str,true);
    content.send();
}
function loadPageC(str, id, call) {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        var content=new XMLHttpRequest();
    } else {// code for IE6, IE5
        var content=new ActiveXObject("Microsoft.XMLHTTP");
    }
    content.onreadystatechange = function() {
        if (content.readyState==4 && content.status==200) {
            document.getElementById(id).innerHTML=content.responseText;
            if(typeof call != "undefined"){
                call();
            }
        } else if (content.readyState==4 && (content.status==404 || content.status==500)) {
            display_error();
        }
    }
    content.open("GET",str,true);
    content.send();
}
//}----------------------------------------------------------------------------------






//{Helpers --------------------------------------------------------------------------

// this function will toggle the visibility of an element.
// note: if element does not have defined display this function defaults to hide
function toggleVisibility(id){
    var element = document.getElementById(id);
    var visibility = element.style.display;
    if(visibility != 'none'){
        element.style.display = 'none';
    } else {
        element.style.display = 'block';
    }
}

// this function will copy the innerHTML of one element into another
function copyInto(to,from){
    document.getElementById(to).innerHTML += document.getElementById(from).innerHTML;
}
function copy(to,from){
    document.getElementById(to).innerHTML = document.getElementById(from).innerHTML;
}

// these are some helper functions for formbuilder
var splitNum=1;
function addToForm(id){
    var newName = id+splitNum;
    splitNum++;
    var newDiv = document.createElement("div");
    newDiv.id = newName;
    newDiv.appendChild(document.createElement("hr"));
    document.getElementById(id).appendChild(newDiv);
    return newName;
}
function removeFromForm(id){
    if(splitNum != 1){
        splitNum--;
        var oldName = id+splitNum;
        removeElement(oldName);
    }
}
// this function will delete any element with 'id'=id
function removeElement(id) {
  var element = document.getElementById(id);
  element.parentNode.removeChild(element);
}

// this function will black out the entire screen
function blackout(){
    toggleVisibility('main_blackout');
    document.getElementById('main_blackout').style.position = 'fixed';
    document.getElementById('main_blackout').style.left = '0px';
    document.getElementById('main_blackout').style.right = '0px';
    document.getElementById('main_blackout').style.top = '0px';
    document.getElementById('main_blackout').style.bottom = '0px';
}
//}----------------------------------------------------------------------------------




//{form validation-------------------------------------------------------------------
function validateNumber(fld,req) {
    var error = "";
    var numCheck = /^\d+$/; // allow  numbers

    if (typeof req == "undefined") {
        req = false;
    }
    if (fld.value == "" && req) {
        error = "You didn't enter a number.\n";
        fld.style.background = '#FFFF33';
    } else if(!req && fld.value == ''){
        fld.style.background = 'White';
    } else if (!fld.value.match(numCheck)) {
        error = "The number contains illegal characters.\n";
        fld.style.background = '#FFFF33';
    }  else {
        fld.style.background = 'White';
    }
    return error;
}
function validateZip(fld,req){
    var error = "";
    // var numCheck = /^\d+$/; // allow  numbers
    zip=fld.value;
    if (typeof req == "undefined") {
        req = false;
    }
    if (zip == "" && req) {
        error = "You didn't enter a zip code.\n";
        fld.style.background = '#FFFF33';
    } else if(!req && zip == ''
        || zip.match(/^[0-9]{5}$/)
        || zip.match(/[A-Z][0-9][A-Z][0-9][A-Z][0-9]/i)
        || zip.match(/[A-Z][0-9][A-Z](\s|[-])[0-9][A-Z][0-9]/i)
    ){
        fld.style.background = 'White';
    } else {
        error = "The zip number contains illegal characters.\n";
        fld.style.background = '#FFFF33';
    }
    return error;
}
function validatePhone(fld,req) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

    if (typeof req == "undefined") {
        req = false;
    }
    if (fld.value == "" && req) {
        error = "You didn't enter a phone number.\n";
        fld.style.background = '#FFFF33';
    } else if(!req && fld.value == ''){
        fld.style.background = 'White';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = '#FFFF33';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. ";
        error += "Make sure you included an area code.\n";
        fld.style.background = '#FFFF33';
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld,req) {
    var error="";
    var tfld = trim(fld.value); // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (typeof req == "undefined") {
        req = false;
    }
    if (fld.value == "" && req) {
        fld.style.background = '#FFFF33';
        error = "You didn't enter an email address.\n";
    } else if(!req && fld.value == ''){
        fld.style.background = 'White';
    } else if (!emailFilter.test(tfld)) { //test email for illegal characters
        fld.style.background = '#FFFF33';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#FFFF33';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePassword(fld,req) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers

    if (typeof req == "undefined") {
        req = false;
    }
    if (fld.value == "" && req) {
        fld.style.background = '#FFFF33';
        error = "You didn't enter a password.\n";

    }  else {
        fld.style.background = 'White';
    }
   return error;
}

function validateUsername(fld,req) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if (typeof req == "undefined") {
        req = false;
    }
    if (fld.value == "" && req) {
        fld.style.background = '#FFFF33';
        error = "You didn't enter a username.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateEmpty(fld,req) {
    var error = "";
    if (typeof req == "undefined") {
        req = false;
    }
    if (fld.value.length == 0 && req) {
        fld.style.background = '#FFFF33';
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;
}
//}----------------------------------------------------------------------------------



//{ all dialog window code ----------------------------------------------------------
function display_error() {
    if(!document.getElementById('dlg_error')){
        var elem = document.createElement('div');
        elem.setAttribute('id','dlg_error');
        elem.style.display = 'none';
        elem.style.width = '400px';
        var message = "<h2>Sorry, There was an error processing your request.</h2>";
        elem.innerHTML = message;
        document.body.appendChild(elem);
    }
    pop_from_id('dlg_error', 'Error');
}
function pop_from_id(id,title){
    if (typeof title == "undefined") {
        title = "";
    }
    document.getElementById('pop_window').style.width = document.getElementById(id).style.width;
    document.getElementById('pop_title').innerHTML = title;
    copy('pop_content',id);
    toggleVisibility('pop_container');
    blackout();
    location.href = "#";
}
function pop_from_url(url,title,width,call){
    if (typeof title == "undefined") {
        title = "";
    }
    if(typeof width == "undefined"){
        document.getElementById('pop_window').style.width = '875px';
    } else {
        document.getElementById('pop_window').style.width = width+'px';
    }

    document.getElementById('pop_title').innerHTML = title;
    document.getElementById('pop_content').innerHTML = "<div style='text-align:center;'><img src='style/images/loader.gif' alt='loading...'/></div>";
    if(typeof call == "undefined"){
        loadPage(url,'pop_content',false);
    } else {
        loadPageC(url,'pop_content',call);
    }
    toggleVisibility('pop_container');
    blackout();
    location.href = "#pop_container";
}
function process_request(){
    var title = "Processing your request";
    document.getElementById('pop_window').style.width = '400px';
    document.getElementById('pop_title').innerHTML = title;
    document.getElementById('pop_content').innerHTML="<div style='text-align:center;'><img src='style/images/loader.gif' alt='loading...'/></div>";
    toggleVisibility('pop_container');
    blackout();
    location.href = "#";
}

function process_finish(){
    close_pop();
}

function close_pop(){
    toggleVisibility('pop_container');
    blackout();
}
//} ---------------------------------------------------------------------------------

//{ hover component -----------------------------------------------------------------
var hover = {
    current: false,
    init: function(){
        if(!document.getElementById('hover_container')){
            var title = document.createElement('div');
            title.setAttribute('id','hover_title');
            title.setAttribute('style','background-color:#ddd;padding:2px;');
            var content = document.createElement('div');
            content.setAttribute('id','hover_content');
            content.setAttribute('style','margin-top:2px;');
            var container = document.createElement('div');
            container.setAttribute('id','hover_container');
            container.setAttribute('style','background-color:#fff;border-style:solid;border-width:1px;border-color:#999;border-radius:3px;position:absolute;display:none;box-shadow: 2px 2px 10px #000;padding:2px;');
            container.appendChild(title);
            container.appendChild(content);
            document.body.appendChild(container);
        }
    },
    show: function(event,id,title){
        hover.init();
        var y = event.pageY-30;
        var x = event.pageX+20;
        document.getElementById('hover_container').style.top = y+'px';
        document.getElementById('hover_container').style.left = x+'px';
        document.getElementById('hover_container').style.display = 'block';
        if(id != hover.current){
            document.getElementById('hover_title').innerHTML = title;
            document.getElementById('hover_content').innerHTML = document.getElementById(id).innerHTML;
            hover.current = id;
        }
    },
    hide: function(){
        document.getElementById('hover_container').style.display = 'none';
    }
};
//}----------------------------------------------------------------------------------
