﻿// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//                   preLoad
//
// Preloads an array of images
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function preLoad(imgs) {
    for (i = 0; i < imgs.length; i++) {
        var tempImg = new Image(25, 25);
        tempImg.src = imgs[i];
    }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//                   imgFlip
//
// Replaces image for rollover effect                    
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function imgFlip(img, loc) {    
    img.src = loc;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//                   launch
//
// Launches webpage                    
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function launch(link) {
    launch(link, false);
}
function launch(link, newWindow) {
    if (newWindow) { window.open(link, "_blank"); }
    else { window.location = link; }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//                   postTo
//
// Posts Form to new Page (Rental App)                    
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function postTo(link, newWindow) {
    var form = document.forms[0];

    if (newWindow) { form.target = "_blank"; }
    form.action = link;
    
    form.submit();
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//                   tab
//
// Auto-Tab to Next Element                    
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function tab(src, nxt) {
    nxt = document.getElementById(nxt);
    if (src.value.length == src.maxLength) { nxt.focus(); }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//                   createElement
//
// Creates a new Element with Attibutes.     
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function createElement(name) {
    return createElement(name, null, "");
}
function createElement(name, args) {
    return createElement(name, args, "");
}
function createElement(name, args, txt) {
    var o = document.createElement(name);

    for (var i in args) {
        if (i == "class") { o.setAttribute("className", args[i]); }
        o.setAttribute(i, args[i]);
    }

    if (txt) { o.appendChild(document.createTextNode(txt)); }

    return o;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//                   removeChildren
//
// Remove all Child Nodes from Parent.     
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function removeChildren(parent) {
    while (parent.hasChildNodes()) {
        parent.removeChild(parent.firstChild);
    }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//                   workBox
//
// Opens or Closes box.     
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function workBox(box) {
    workBox(box, "auto");
}
function workBox(box, cmd) {
    if (box != null) {
        switch (cmd) {
            case "close":
                box.style.display = "none";
                break;
            case "open":
                box.style.display = "block";
                break;
            default:
                box.style.display != "none" && box.style.display != "undefined" && box.style.display != "" ? box.style.display = "none" : box.style.display = "block";
                break;
        }
    }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//                   CountCharacters
//
// Return character count     
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function CountCharacters(count, input) {
    var left = count - document.getElementById(input).value.length;
    
    return (left < 0) ? 0 : left;
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//                   bbCode
//
// Format BB Code.     
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function bbCode(input) {
    return bbCode(input, false);
}
function bbCode(input, decrypt) {
    var output = input;

    if (!decrypt) {
        output = output.replace(/\[b\]/g, '<strong>');
        output = output.replace(/\[\/b\]/g, '</strong>');
        output = output.replace(/\[i\]/g, '<i>');
        output = output.replace(/\[\/i\]/g, '</i>');
        output = output.replace(/\[u\]/g, '<u>');
        output = output.replace(/\[\/u\]/g, '</u>');
    }
    else {
        output = output.replace(/\<strong\>/g, '[b]');
        output = output.replace(/\<\/strong\>/g, '[/b]');
        output = output.replace(/\<i\>/g, '[i]');
        output = output.replace(/\<\/i\>/g, '[/i]');
        output = output.replace(/\<u\>/g, '[u]');
        output = output.replace(/\<\/u\>/g, '[/u]');    
    }
        
    return output;
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//                   GetQueryString
//
// Format Query String.     
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function GetQueryString() {
	var query = location.search.substring(1);
    var pairs = query.split('&');
    var args = new Object();
                        
    for(var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');
        if (pos >= 0) {
            args[pairs[i].substring(0, pos)] = unescape(pairs[i].substring(pos + 1)); 
        }
    }
         
    return args;
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//                   FormatNumber
//
// Formats number with or without commas.     
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function FormatNumber(num, cmd) {
	var output = '';
    num = num.toString();
            
    if (cmd == 'in') {
        var decIndex = num.indexOf('.');
		var dec = '';
		
		if (decIndex >= 0) { 
			dec = num.substring(decIndex);
			num = num.substring(0, decIndex);
		}
		
		num = num.split('').reverse();
            
        for (i = 0; i < num.length; i++) {
            if (i > 0 && i % 3 == 0) { output += ','; }
            output += num[i];
        }

        output = output.split('').reverse().join('') + dec;
    }

    else { output = num.split(',').join('') * 1; }
            
    return output;
}


