var mxmlnode = [];
var msWhere = [];
var mndRecordSet = [];
var mxmlDoc;
var miIndex;
var msWebsite;

function SetRestoreButtonText(sText, sFileName) {

    $find("btnRestoreConfirmText").set_ConfirmText("Restore Web Site from " + sText);
    document.getElementById("txtDelete").innerText = sFileName;

}

function DeleteRestorePoint(sTitle, sFileName) {

    var ans;
    var txtDelete;

    if (document.getElementById("lblProgress").innerHTML == "0%") {
        ans = window.confirm("Delete restore point " + sTitle + "?");

        if (ans == true) {
            txtDelete = document.getElementById("txtDelete");
            txtDelete.innerText = sFileName;
            __doPostBack('btnDelete', '');
        }
    }
}

function ReturnObject(sShortDescription, sFile) {

    this.sShortDescription = sShortDescription;
    this.sFile = sFile;
}

function ContactUsLoad() {

    var txtOther;

    txtOther = document.getElementById("txtOther");
    txtOther.style.visibility = "hidden";
}

function KeyPressCurrency() {

	var iKeyCode;

	iKeyCode = window.event.keyCode;

	// 0 To 9 = 48 to 57
	// $ , . = 36 and 44 and 46

	if (iKeyCode < 48 || iKeyCode > 57) {
	    if (iKeyCode != 36 && iKeyCode != 44 && iKeyCode != 46) {
            window.event.keyCode = false;
        }
    }
}

function LoadBackupRestore() {
    
    document.getElementById("btnDelete").style.visibility = "hidden"
    document.getElementById("txtDelete").style.visibility = "hidden"
}

function LoadDBMaintenance() {

    SetPriceVisible();

}

function SetPriceVisible() {
    
    var chkForSale;

    chkForSale = document.getElementById("chkForSale");

    if (chkForSale.checked == true) {
        document.getElementById("txtPrice").style.visibility = "visible";
    }

    if (chkForSale.checked == false) {
        document.getElementById("txtPrice").style.visibility = "hidden";
    }
}

function ContactUsOptionSelect(sSelect) {

    var txtOther;
    var lblPicture;
    var fuPicture;
    var lblName;
    var lblEmail;
    var lblNote;
    var lblOther;

    txtOther = document.getElementById("txtOther");
    lblOther = document.getElementById("lblOther");
    lblPicture = document.getElementById("lblPicture");
    fuPicture = document.getElementById("fuPicture");
    lblName = document.getElementById("lblName");
    lblEmail = document.getElementById("lblEmail");
    lblNote = document.getElementById("lblNote");

    txtOther.style.visibility = "visible";
    lblPicture.style.visibility = "visible";
    fuPicture.style.visibility = "visible";

    if (sSelect =="1") {
        lblName.innerHTML = "Name*";
        lblEmail.innerHTML = "Email Address*";
        lblPicture.innerHTML = "Picture to upload*";
        lblNote.innerHTML = "Note to Artist";
        lblOther.innerHTML = "Other";
        txtOther.style.visibility = "hidden";
    }

    if (sSelect =="2") {
        lblName.innerHTML = "Name";
        lblEmail.innerHTML = "Email Address";
        lblPicture.innerHTML = "Picture to upload";
        lblNote.innerHTML = "Note to Artist*";
        lblOther.innerHTML = "Other";
        lblPicture.style.visibility = "hidden";
        fuPicture.style.visibility = "hidden";
        txtOther.style.visibility = "hidden";
    }

    if (sSelect =="3") {
        lblName.innerHTML = "Name*";
        lblEmail.innerHTML = "Email Address*";
        lblPicture.innerHTML = "Picture to upload";
        lblNote.innerHTML = "Note to Artist*";
        lblOther.innerHTML = "Other*";
    }
}

function LoadIndexPage() {

    var iCount;
    var iControlCount;
    var iRecordCount;
    var iGalleryCount;
    var sKey;
    var sWhere = [];
    var sPicPath;
    var sReturn;
    var sPicFolder = '/pictures/';
    var sCategory;
    var imgSmall;
    var lnkSmall;
    var sXMLPath = "/database/shannonspalette.xml";

    msWebsite = "http://" + window.location.host;

    OpenXMLDB(msWebsite + sXMLPath);

    for (iGalleryCount = 1; iGalleryCount <= 5; iGalleryCount++) {
        OpenRecordSetByKey("Category", iGalleryCount);
        sCategory = FieldValue("Description");

        sWhere[0] = "[Category_Key] = " + iGalleryCount;
        sWhere[1] = "ORDER BY Date";


        OpenRecordSet("Art", sWhere);
        iRecordCount = RecordCount();
        MoveTo(iRecordCount - 1);
        iControlCount = 1;

        for (iCount = iRecordCount; iCount >= 0; iCount--) {
            if (iControlCount == 4) {
                break;
            }
            sCategory = sCategory.replace(" & ","_")
            if (iCount > 0) {
                sPicPath = msWebsite + sPicFolder + FieldValue("Small_File");
                lnkSmall = document.getElementById("lnk" + sCategory + iControlCount);
                lnkSmall.href = msWebsite + "/item.html?inum=" + FieldValue("Item_Number");
                imgSmall = document.getElementById("img" + sCategory + iControlCount);
                imgSmall.src = sPicPath;
                ResizeImageControl(imgSmall, 145, 145, 45, 45)
                imgSmall.style.visibility = "visible";
                iControlCount = iControlCount + 1;
                MovePrevious();
            }
        }
    }
}

function LoadItemPage() {

    var iPosition;
    var iCurWidth;
    var iCurHeight;
    var sXMLPath = "/database/shannonspalette.xml";
    var sCriteria;
    var sPicPath;
    var sPicFolder = '/pictures/';
    var sWhere = [];
    var sTest;
    var sReturn;
    var sPrice;
    var imgControl;
    var lblDescription;
    var lblItemNumber;

    sCriteria = window.location.href;
    iPosition = sCriteria.indexOf("?");
    if (iPosition > -1) {
        sCriteria = sCriteria.substr(iPosition + 6, sCriteria.length);
        msWebsite = "http://" + window.location.host;
        mndRecordSet[0] = null;

        sCriteria = sCriteria.substr(0, 5);
        sWhere[0] = "[Item_Number]=" + sCriteria;

        OpenXMLDB(msWebsite + sXMLPath);
        OpenRecordSet("Art", sWhere);
        lblItemNumber = document.getElementById("item_number");
        imgControl = document.getElementById("picItem");
        lblDescription = document.getElementById("txtDescription");
        sPicPath = msWebsite + sPicFolder + FieldValue("File");
        imgControl.src = sPicPath;
        lblItemNumber.innerHTML = "Item # - " + FieldValue("Item_Number");

        sPrice =  FieldValue("Price");
        if (FieldValue("For_Sale") == "True") {
            document.getElementById("itmForSale").innerHTML = "This item is for sale by the artist";

            if (sPrice == "$0.00") {
                document.getElementById("itmForSale").innerHTML = document.getElementById("itmForSale").innerHTML + ".";
            }

            if (sPrice != "$0.00") {
                document.getElementById("itmForSale").innerHTML = document.getElementById("itmForSale").innerHTML + " for " + sPrice + ".";
            }
        }
        imgControl = document.getElementById("picItem");
        lblDescription.innerHTML = FieldValue("Short_Desc");
        sReturn = FieldValue("Size");
        iPosition = sReturn.indexOf(",");
        iCurWidth = parseInt(sReturn.substr(0, iPosition));
        iCurHeight = parseInt(sReturn.substr(iPosition + 1, sReturn.length));

        sTest = lblDescription.innerHTML.toUpperCase();
        if (sTest == "N/A") {
            lblDescription.innerHTML = "*Please contact the artist for more information.*";
        }
        ResizeImageControl(imgControl, iCurWidth, iCurHeight, 600, 400);
    }
    ShowRandomSmallPics();
}

function ResizeImageControl(ctlControl, iCurWidth, iCurHeight, iNewWidth, iNewHeight) {

    var dblRatio;

    dblRatio = iNewWidth / iCurWidth;

    if (dblRatio > 1) {
        dblRatio = iNewHeight / iCurHeight;
    }

    if (dblRatio < 1) {
        ctlControl.width = parseInt(iCurWidth * dblRatio);
        ctlControl.height = parseInt(iCurHeight * dblRatio);
    }
}

function LoadGalleryPage() {

    var iPosition;
    var iCount;
    var iControlCount;
    var iStart;
    var iRecordCount;
    var iPage;
    var iNextPage;
    var iPrevPage;
    var sWhere = [];
    var sPicFolder = '/pictures/';
    var sXMLPath = "/database/shannonspalette.xml";
    var sCriteria;
    var sPicPath;
    var sPageNumber;
    var psReturnArray = [];
    var imgControl;
    var imgSmall;
    var lblDescription;
    var ctlLink;
    var lblCategory;

    sCriteria = window.location.href;
    iPosition = sCriteria.indexOf("?");
    if (iPosition == -1) {
        sCriteria = "ct=Equine";
    }

    if (iPosition > -1) {
        sCriteria = sCriteria.substr(iPosition +1,sCriteria.length);
    }

    iPosition = sCriteria.indexOf(";");
    if (iPosition > 1) {
        sCriteria = sCriteria.substr(0,iPosition);
    }

    sPageNumber = window.location.href;
    iPosition = sPageNumber.indexOf("pg=");
    if (iPosition == -1) {
        iPage = 1;
    }

    if (iPosition > -1) {
        iPage = sPageNumber.substr(iPosition + 3, sPageNumber.length);
    }

    msWebsite = "http://" + window.location.host;
    miIndex = 0;
    mndRecordSet[0] = null;
    OpenXMLDB(msWebsite + sXMLPath);

    sCriteria = sCriteria.substr(3, sCriteria.length);

    if (sCriteria == "bw") {
        sCriteria = "Black & White";
    }

    lblCategory = document.getElementById("Category");
    lblCategory.innerHTML = "Category - " + sCriteria;

    sWhere[0] = "[Description]=" + sCriteria;
    OpenRecordSet("Category", sWhere);

    sWhere[0] = "[Category_Key]=" + Key();
    OpenRecordSet("Art", sWhere);

    iRecordCount = RecordCount();

    iStart = ((iPage - 1) * 12) + 1;

    if (iStart > 1) {
        for (iCount = 1; iCount < iStart; iCount++) {
            MoveNext();
        }
    }

    iControlCount = 0;
    for (iCount = iStart; iCount < iStart + 12; iCount++) {
        iControlCount = iControlCount + 1;
        imgControl = document.getElementById("pic" + iControlCount);
        lblDescription = document.getElementById("Item" + iControlCount);
        ctlLink = document.getElementById("lnkPicLink" + iControlCount);
        
        if (iCount <= iRecordCount) {
            sPicPath = msWebsite + sPicFolder + FieldValue("Small_File");
            imgControl.src = sPicPath;
            lblDescription.innerHTML = FieldValue("Item_Number");
            if (FieldValue("For_Sale") == "True") {
                lblDescription.innerHTML = lblDescription.innerHTML + " - For Sale";
            }
            imgControl.style.visibility = "visible";
            ctlLink.href = msWebsite + "/item.html?inum=" + lblDescription.innerHTML.substr(0,5);
            MoveNext();
        }

        if (iCount > iRecordCount) {
            //imgControl.src = msWebsite + "/images/blank.jpg";
            imgControl.style.visibility = "hidden";
            imgControl.disabled = "true";
            lblDescription.innerHTML = "";
        }
    }


    iNextPage = parseInt(iPage) + 1;
    iPrevPage = parseInt(iPage) - 1;

    if (iPrevPage == 0) {
        iPrevPage = 1;
    }

    if (iRecordCount > 12) {
        document.getElementById("itmPageNumber").innerHTML = "<SPAN class=style5>Page: " + iPage + " </SPAN> " ;
    }

    if (iRecordCount <= 12) {
        document.getElementById("itmPageNumber").innerHTML = "<SPAN class=style5> Page: 1 </SPAN> ";
        document.getElementById("lnkNext").href = "";
        document.getElementById("lnkPrevious").href = "";
        document.getElementById("imgNext").style.visbility = "hidden";
        document.getElementById("imgPrevious").style.visbility = "hidden";
    }

    if ((iNextPage * 12) < (iRecordCount + 12)) {
        document.getElementById("lnkNext").href = msWebsite + "/gallery.html?ct=" + sCriteria + ";pg=" + iNextPage;
        document.getElementById("imgNext").style.visbility = "visbile";
    }

    if ((iNextPage * 12) >= (iRecordCount + 12)) {
        document.getElementById("lnkNext").href = msWebsite + "/gallery.html?ct=" + sCriteria + ";pg=" + iPage;
        document.getElementById("imgNext").style.visbility = "hidden";
    }

    if (iPrevPage == 1) {
        document.getElementById("lnkPrevious").href = msWebsite + "/gallery.html?ct=" + sCriteria + ";pg=1";
    }

    if (iPrevPage > 1) {
        document.getElementById("lnkPrevious").href = msWebsite + "/gallery.html?ct=" + sCriteria + ";pg=" + iPrevPage;
    }

    ShowRandomSmallPics();
}

function ResizeArray(iArray,iIndex) {
    var iNewArray = [];
    var iCount;
    var iArrayCount;

    iArrayCount = 0;
    for (iCount = 0; iCount <= iArray.length - 1; iCount++) {
        if (iCount!=iIndex){
            iNewArray[iArrayCount] = iArray[iCount];
            iArrayCount = iArrayCount + 1;
        }
    }
    return iNewArray;
}

function ShowRandomSmallPics() {
    var iRecords = [];
    var iCount;
    var iRandno;
    var lKey;
    var sWhere = [];
    var sPicPath;
    var sReturn;
    var sPicFolder = '/pictures/';
    var imgSmall;
    var lnkSmall;

    OpenRecordSet("Art", sWhere);

    for (iCount = 0; iCount <= RecordCount()-1; iCount++) {
        iRecords[iCount] = Key();
        MoveNext();
    }

    for (iCount = 1; iCount <= 9; iCount++) {
        iRandno = Math.floor(Math.random() * iRecords.length);
        if (iRandno >= iRecords.length) {
            iRandno = iRecords.length - 1;
        }

        lKey = iRecords[iRandno];
        OpenRecordSetByKey("Art", lKey);
        sPicPath = msWebsite + sPicFolder + FieldValue("Small_File");
        lnkSmall = document.getElementById("lnkMiniPicLink" + iCount);
        lnkSmall.href = msWebsite + "/item.html?inum=" + FieldValue("Item_Number");
        imgSmall = document.getElementById("picMini" + iCount);
        imgSmall.src = sPicPath;
        ResizeImageControl(imgSmall, 145, 145, 45, 45)
        imgSmall.style.visibility = "visible";
        iRecords = ResizeArray(iRecords, iRandno);
    }
}

function FieldValue(sField){
    var ndChild;
    var iCount;
    var sReturn;

    if (miIndex < mndRecordSet.length) {
        for (iCount = 0; iCount < mndRecordSet[miIndex].childNodes.length; iCount++) {
            ndChild = mndRecordSet[miIndex].childNodes[iCount];
            if (ndChild.nodeName == sField) {
                if (ndChild.attributes.length > 0) {
                    sReturn = ndChild.attributes[0].value;
                    break;
                }
            }
        }
    }
    return sReturn;
}

function OpenXMLDB(sFile) {

    if (navigator.appName == "Microsoft Internet Explorer") {
        mxmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0");
        mxmlDoc.async = false;
        mxmlDoc.load(sFile);
    }
    else {
        var xmlhttp = new window.XMLHttpRequest();
        xmlhttp.open("GET", sFile, false);
        xmlhttp.send(null);
        mxmlDoc = xmlhttp.responseXML.documentElement;
        mxmlDoc = mxmlDoc.parentNode;
    }
}

function EOF() {
    if (miIndex == mndRecordSet.length){
        return true;
    }
}

function BOF() {
    if (miIndex == 0){
        return true;
    }
}

function RecordCount() {
    return mndRecordSet.length;
}

function MoveNext() {
    if (miIndex < mndRecordSet.length){
        miIndex = miIndex + 1;
    }
}

function MoveTo(iIndex) {
    miIndex = iIndex;
}

function MovePrevious() {
    if (miIndex > 0) {
        miIndex = miIndex - 1;
    }
}
function Search(sField, sSearchType, sValue) {
    this.sField = sField;
    this.sSearchType = sSearchType;
    this.sValue = sValue;
}

function Key(){
    var sKey;

    if (mndRecordSet.length > 0) {
        sKey = mndRecordSet[miIndex].nodeName;
        return sKey.substr(1, sKey.length);
    }
}

function OpenRecordSetByKey(sTableName, lKey){

    var iCount;
    var ndTable;
    var ndRecord;

    miIndex = 0;
    msTableName = sTableName;
    mlKey = lKey;
    miOpenRecordset = 2;
    mndRecordSet = [];

    for (iNodeCount = 0; iNodeCount < mxmlDoc.lastChild.childNodes.length; iNodeCount++) {
        ndTable = mxmlDoc.lastChild.childNodes[iNodeCount];
        if (ndTable.nodeName == sTableName) {
            for (iCount = 1; iCount < ndTable.childNodes.length; iCount++) {
                ndRecord = ndTable.childNodes[iCount];
                if (ndRecord.nodeName == "R" + lKey){
                    mndRecordSet[0] = ndRecord;
                    break;
                }
            }
        }
    }

}

 function SortArray(sField) {

        var bSorted;
        var iCount;
        var iCount2;
        var iCount3;
        var ndTemp;

        for (iCount = 0; iCount <= mndRecordSet.length - 1; iCount++) {
            bSorted = true;
            for (iCount2 = 0; iCount2 <= mndRecordSet.length - 2; iCount2++) {
                iCount3 = iCount2 + 1;
                if (ReturnFieldFromArray(iCount2, sField) > ReturnFieldFromArray(iCount3, sField)) {
                    bSorted = false;
                    ndTemp = mndRecordSet[iCount2];
                    mndRecordSet[iCount2] = mndRecordSet[iCount3];
                    mndRecordSet[iCount3] = ndTemp;
                }
            }
            if (bSorted = true) {
                break;
            }
        }

}

function ReturnFieldFromArray(iArray, sFieldName) {
    
    var iCount;

    for (iCount = 0; iCount <= mndRecordSet[iArray].childNodes.length - 1; iCount++) {
        if (mndRecordSet[iArray].childNodes[iCount].nodeName == sFieldName) {
            return mndRecordSet[iArray].childNodes[iCount].attributes[0].value;
            break;
        }
    }
}

function OpenRecordSet(sTableName, sWhere) {

    var bAdded;
    var iCount;
    var iArrayLength;
    var iNodeCount;
    var iSearchCount;
    var iFieldCount;
    var iPosition;
    var sValue;
    var sORDERBY;
    var ndTable;
    var ndRecord;
    var ndField;
    var psSearch = new Object();

    miIndex = 0;
    iArrayLength = 0;
    mndRecordSet = [];
    sORDERBY = "";

    if (sWhere.length > 0) {
        sValue = sWhere[sWhere.length - 1];
        sValue = sValue.toUpperCase();

        if (sValue.indexOf("ORDER BY") == 0) {
            sValue = sWhere[sWhere.length - 1];
            sORDERBY = sValue.substr(9, sValue.length);
            iArrayLength = sWhere.length - 2;
        }

        if (sValue.indexOf("ORDER BY") == -1) {
            iArrayLength = sWhere.length - 1;
            sORDERBY = "";
        }

        for (iCount = 0; iCount <= iArrayLength; iCount++) {
            msWhere[iCount] = sWhere[iCount];
        }
    }

    if (sWhere.length > 0) {
        for (iCount = 0; iCount <= iArrayLength; iCount++) {
            sValue = sWhere[iCount];
            iPosition = sValue.indexOf(']', 1);
            psSearch[iCount] = new Search("", "", "")
            psSearch[iCount].sField = sValue.substr(0, iPosition);
            psSearch[iCount].sField = psSearch[iCount].sField.substr(1);
            sValue = sValue.substr(iPosition + 1)
            if (sValue.substr(0, 1) == " ") {
                sValue = sValue.substr(1, sValue.length);
            }

            if (sValue.substr(sValue.length - 1, 1) == " ") {
                sValue = sValue.substr(0, sValue.length - 1);
            }

            psSearch[iCount].sSearchType = sValue.substr(0, 1);
            iPosition = psSearch[iCount].sSearchType.length;
            sValue = sValue.substr(iPosition);

            if (sValue.substr(0, 1) == " ") {
                sValue = sValue.substr(1, sValue.length);
            }

            if (sValue.substr(sValue.length - 1, 1) == " ") {
                sValue = sValue.substr(0, sValue.length - 1);
            }

            psSearch[iCount].sValue = sValue;
        }
    }

    for (iNodeCount = 0; iNodeCount < mxmlDoc.lastChild.childNodes.length; iNodeCount++) {
        ndTable = mxmlDoc.lastChild.childNodes[iNodeCount];
        if (ndTable.nodeName == sTableName) {
            if (sWhere.length == 0) {
                for (iCount = 1; iCount < ndTable.childNodes.length; iCount++) {
                    mndRecordSet[iCount - 1] = ndTable.childNodes[iCount];
                }
            }
            else
                for (iCount = 1; iCount < ndTable.childNodes.length; iCount++) {
                    ndRecord = ndTable.childNodes[iCount];
                    bAdded = false;
                    for (iSearchCount = 0; iSearchCount <= iArrayLength; iSearchCount++) {
                        for (iFieldCount = 0; iFieldCount < ndRecord.childNodes.length; iFieldCount++) {
                            ndField = ndRecord.childNodes[iFieldCount];
                            if (ndField.nodeName == psSearch[iSearchCount].sField) {
                                if (ndField.attributes.length > 0) {
                                    if (psSearch[iSearchCount].sSearchType == '=') {
                                        if (psSearch[iSearchCount].sValue == ndField.attributes[0].value) {
                                            bAdded = true;
                                            break;
                                        }
                                    }
                                    if (psSearch[iSearchCount].sSearchType == '<>') {
                                        if (psSearch(iSearchCount).sValue != ndField.attributes(0).value) {
                                            bAdded = true;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        if (bAdded == true) {
                            break;
                        }
                    }
                    if (bAdded == true) {
                        if (mndRecordSet[0] == null)
                            {
                            mndRecordSet[0] = ndRecord;
                            }
                        else
                            mndRecordSet[mndRecordSet.length] = ndRecord;
                        }
                    }
                }
        }

        if (sORDERBY.length > 0) {
            SortArray(sORDERBY);
        }
}
