// JavaScript Document
var r = {
    'special': /[\W]/g,
    'quotes': /['\''&'\"']/g,
    'singlequote': /['\'']/g,
    'notnumbers': /[^\d]/g
}
var numArr = "0123456789";
var validChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@.-_";
var invalidChars = "/\\[]\"':;|<>+=,?*";

var valid = function (o, w) {
    o.value = o.value.replace(r[w], '');
}

countMe = function () {
    var max = $('max_chars');
    var characters = $('characters');
    var counter = $('counter');
    var remaining = $('remaining');
    counter.value = characters.value.length;
    remaining.value = max.value - characters.value.length;
    if (characters.value.length > max.value) {
        counter.style.color = "#fff"
        counter.style.font = "bold"
        counter.style.background = "Red"
    }
    else {
        counter.style.color = "#fff"
        counter.style.background = "#0DFF00"
    }
}

replaceQuotes = function (str) {
    return str.replace(/'/g, "\'");
}
autoFill = function (src, dest) {
    $(dest).value = $(src).value;
}

addOption = function (selectbox, text, value) {
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    selectbox.options.add(optn);
}

isNumber = function (noValue) {
    var temp;
    var isvalid = true;

    for (var i = 0; i < noValue.length; i++) {
        temp = "" + noValue.substring(i, i + 1);
        if (numArr.indexOf(temp) == "-1") {
            isvalid = false;
        }
    }
    return isvalid;
}

validInput = function (intext) {
    var isvalid = true;
    var temp;
    for (var i = 0; i < intext.length; i++) {
        temp = "" + intext.substring(i, i + 1);
        if (validChars.indexOf(temp) == "-1") {
            isvalid = false;
        }
    }
    return isvalid;
}

editor_date_month_change = function (month_el, day_el, year_el) {
    var month_el = ge(month_el);
    var day_el = ge(day_el);
    var year_el = year_el ? ge(year_el) : false;
    var new_num_days = _month_get_num_days(month_el.value, year_el.value && year_el.value != -1 ? year_el.value : false);
    var b = day_el.options[0].value == -1 ? 1 : 0;
    for (var i = day_el.options.length; i > new_num_days + b; i--) {
        remove_node(day_el.options[i - 1]);
    }
    for (var i = day_el.options.length; i < new_num_days + b; i++) {
        day_el.options[i] = new Option(i + (b ? 0 : 1), i + (b ? 0 : 1));
    }
}

function _month_get_num_days(month, year) {
    var temp_date;
    if (month == -1) {
        return 31;
    }
    temp_date = new Date(year ? year : 1912, month, 0);
    return temp_date.getDate();
}

hasChars = function (intext) {
    var invalid = false;
    var temp;
    for (var i = 0; i < intext.length; i++) {
        temp = "" + intext.substring(i, i + 1);
        if (invalidChars.indexOf(temp) == "-1") {
            invalid = true;
        }
    }
    return invalid;
}

textCounter = function (field, cntfield, maxlimit) {

    if (field.value.length > 612) {
        smsCount.innerHTML = '5 Messages';
    } else if (field.value.length > 459) {
        smsCount.innerHTML = '4 Messages';
    } else if (field.value.length > 306) {
        smsCount.innerHTML = '3 Messages';
    } else if (field.value.length > 160) {
        smsCount.innerHTML = '2 Messages';
    } else {
        smsCount.innerHTML = '';
    }
    if (field.value.length > maxlimit) // if too long...trim it!
    field.value = field.value.substring(0, maxlimit);
    // otherwise, update 'characters left' counter
    else cntfield.value = maxlimit - field.value.length;
}

toggle = function(item){
	animatedcollapse.toggle(item);
}

encode_cookie = function (cookie_value) {

    // This variable holds the encoded cookie characters
    var coded_string = ""

    // Run through each character in the cookie value
    for (var counter = 0; counter < cookie_value.length; counter++) {

        // Add the character's numeric code to the string
        coded_string += cookie_value.charCodeAt(counter)

        // Separate each code with a plus sign (+)
        if (counter < cookie_value.length - 1) {
            coded_string += "+"
        }
    }
    return coded_string
}

decode_cookie = function (coded_string) {

    // This variable holds the decoded cookie value
    var cookie_value = ""

    // Use + to split the coded string into an array
    var code_array = coded_string.split("+")

    // Loop through the array
    for (var counter = 0; counter < code_array.length; counter++) {

        // Convert the code into a character and 
        // add it to the cookie value string
        cookie_value += String.fromCharCode(code_array[counter])
    }
    return cookie_value
}

UTFConvert = function (inputText) {
    inputText = inputText.replace(/&#33;/g, "!");
    inputText = inputText.replace(/&#36;/g, "$");
    inputText = inputText.replace(/&#38;/g, "\&");
    inputText = inputText.replace(/&#39;/g, "\'");

    inputText = inputText.replace(/&#58;/g, ":");
    inputText = inputText.replace(/&#59;/g, ";");
    inputText = inputText.replace(/&#63;/g, "?");
    inputText = inputText.replace(/&#64;/g, "@");

    inputText = inputText.replace(/&#91;/g, "[");
    inputText = inputText.replace(/&#92;/g, "\\");
    inputText = inputText.replace(/&#93;/g, "]");
    inputText = inputText.replace(/&#94;/g, "^");
    inputText = inputText.replace(/&#95;/g, "¼");
    inputText = inputText.replace(/&#96;/g, "`");

    inputText = inputText.replace(/&#126;/g, "~");
    inputText = inputText.replace(/&#128;/g, "€");
    inputText = inputText.replace(/&#134;/g, "†");
    inputText = inputText.replace(/&#153;/g, "™");
    inputText = inputText.replace(/&#156;/g, "œ");

    inputText = inputText.replace(/&#161;/g, "¡");
    inputText = inputText.replace(/&#162;/g, "¢");
    inputText = inputText.replace(/&#163;/g, "£");
    inputText = inputText.replace(/&#164;/g, "¤");
    inputText = inputText.replace(/&#165;/g, "¥");
    inputText = inputText.replace(/&#169;/g, "©");

    inputText = inputText.replace(/&#174;/g, "®");
    inputText = inputText.replace(/&#175;/g, "°");
    inputText = inputText.replace(/&#181;/g, "µ");
    return inputText;



}
toggleAll = function () {
    for (var i = 1; i <= 36; i++) {
        animatedcollapse.addDiv('faq' + i, 'fade=1');
    }
    animatedcollapse.init();
}