﻿// * =================================================================================================
// * Location:      content/property/js/property.js
// * Created:	    February 2010
// * Created By:	Ross Gratton 
// * Description:   Property Javascript Library
// * Copyright:     Student Pad Limited. All Rights Reserved
// * =================================================================================================
// *
// * Version History:
// *  No:       Date:               Modified By:        Modification Details:
// *  #1        February 2010       Ross Gratton (STP)  Initial Version
// *
// * =================================================================================================

// function to evaluate a returned XML packet
function xmlLoad(xml_string) {

    xml_string = unescape(xml_string);

    if (document.implementation.createDocument) {
        // Firefox
        var xml = xml_string;
    } else {
        // IE
        var xml = new ActiveXObject('Microsoft.XMLDOM');
        xml.loadXML(xml_string);
        xml = $(xml).children(0);
    }

    var status = $(xml).children('status').text();

    switch (status) {

        case "OKAY":
            break;

        case "FAIL":
            break;

        case "SERR":

            //  Redirect to the serious error page
            window.location.href($(xml).find('url').text());
            break;
    }

    return xml;
}

// function to create XML packet from oEntries object
function xmlPacket(oData) {

    var arrXML = new Array();
    arrXML[arrXML.length] = "<xml><data>";

    for (var i in oData) {

        if (oData[i].type == "collection") {

            arrXML[arrXML.length] = "<collection collection_type='" + i + "'>";
            for (var j in oData[i]) {

                if (typeof (oData[i][j]) == "object") {
                    var xml = buildXMLString(oData[i][j], j);
                    arrXML[arrXML.length] = xml;
                }
            }
            arrXML[arrXML.length] = "</collection>";

        } else {
            var xml = buildXMLString(oData[i], i);
            arrXML[arrXML.length] = xml;
        }
    }
    arrXML[arrXML.length] = "</data></xml>";
    return arrXML.join("");
}

function buildXMLString(object, key) {

    // Creates a list of standard attributes we do not want to pass up to the server 
    var skipAttr = new Array();
    var arrXML = new Array();
    var attributeString = "";

    skipAttr[skipAttr.length] = "language";
    skipAttr[skipAttr.length] = "dataFld";
    skipAttr[skipAttr.length] = "class"
    skipAttr[skipAttr.length] = "lang"
    skipAttr[skipAttr.length] = "id"
    skipAttr[skipAttr.length] = "hideFocus"
    skipAttr[skipAttr.length] = "dir"
    skipAttr[skipAttr.length] = "title"
    skipAttr[skipAttr.length] = "contentEditable"
    skipAttr[skipAttr.length] = "dataFormatAs"
    skipAttr[skipAttr.length] = "style"
    skipAttr[skipAttr.length] = "disabled"
    skipAttr[skipAttr.length] = "accessKey"
    skipAttr[skipAttr.length] = "dataSrc"
    skipAttr[skipAttr.length] = "tabIndex"
    skipAttr[skipAttr.length] = "implementation"
    skipAttr[skipAttr.length] = "start"
    skipAttr[skipAttr.length] = "readOnly"
    skipAttr[skipAttr.length] = "height"
    skipAttr[skipAttr.length] = "cache"
    skipAttr[skipAttr.length] = "alt"
    skipAttr[skipAttr.length] = "hspace"
    skipAttr[skipAttr.length] = "loop"
    skipAttr[skipAttr.length] = "width"
    skipAttr[skipAttr.length] = "dynsrc"
    skipAttr[skipAttr.length] = "src"
    skipAttr[skipAttr.length] = "accept"
    skipAttr[skipAttr.length] = "vrml"
    skipAttr[skipAttr.length] = "align"
    skipAttr[skipAttr.length] = "useMap"
    skipAttr[skipAttr.length] = "lowsrc"
    skipAttr[skipAttr.length] = "vspace"
    skipAttr[skipAttr.length] = "size"
    skipAttr[skipAttr.length] = "value"
    skipAttr[skipAttr.length] = "border"
    skipAttr[skipAttr.length] = "name"

    var skipAttributeList = "|" + skipAttr.join("|") + "|";

    for (var j = 0; j < object.attributes.length; j++) {

        var skip = false;

        // Drops all "ON" event attributes
        if (object.attributes[j].nodeName.substr(0, 2).toLowerCase() == "on") {
            skip = true;
        }

        // Drops all "STANDARD HTML" attributes 
        if (skipAttributeList.indexOf("|" + object.attributes[j].nodeName + "|") > -1) {
            skip = true;
        }

        if (!skip) {
            attributeString += " " + object.attributes[j].nodeName + "='" + object.attributes[j].nodeValue + "'";
        }
    }

    arrXML[arrXML.length] = "<" + key + attributeString + ">";
    if ((object.rows) && (object.type != "textarea")) {
        arrXML[arrXML.length] = $("#" + object.id + " input:radio:checked").val();
    }
    else {
        if (object.type == "checkbox") {

            if (object.checked) {
                arrXML[arrXML.length] = "on";
            } else {
                arrXML[arrXML.length] = "off";
            }

        }else if (object.value) {
        arrXML[arrXML.length] = object.value;
        }
        else {
            arrXML[arrXML.length] = "NULL";
        }
    }
    arrXML[arrXML.length] = "</" + key + ">";
    return arrXML.join("");

}
