var fullImageURL = new Array();
var showFullSizeImages = true;
var selectedImageId = 0;

/*
 This function forward the place bid to confirm bid page
 For final confirmation
 */
function bidNow(formName, noticeText, minval, value){
    var formElements = document.getElementsByTagName("input"); //Collect all inputs
    var isErrorMsg = null;
    
    if ($("placeBid") != null) {
        $("placeBid").className = "form_label";
    }
    
    if ($("yourStartingBid") != null) {
        $("yourStartingBid").className = "form_label";
    }
    
    if ($("maxBid") != null) {
        $("maxBid").className = "";
    }
    if ($("startingBid") != null) {
        $("startingBid").className = "";
    }
    
    //check the button value
    $("eventType").value = value;
    
    if (value == 'BidNow') {
        document.lotform._eventId.value = "valBid";
    }
    else {
        document.lotform._eventId.value = "counterBid";
    }
    if (value == 'BidNow' || value == 'counterBid' || value == 'increaseBid') {
        for (var i = 0; i < formElements.length - 1; i++)//loop through collection of inputs
        {
            //Extract all inputs that are checkboxes and have the id of ids
            if (formElements[i].type == "text" && formElements[i].id != "stopEnterKey") {
                isErrorMsg = checkNumeric(formElements[i], minval);
                
                // In case any one of the text boxes has an error  
                // return to the page with the error.
                if (isErrorMsg != null) {
                    // Add an innerHTML td element content if there is a 
                    // validation failure.
                    
                    if (formElements[i].id == "maxBid") {
                        $("placeBid").className = $("placeBid").className + " alertredr";
                    }
                    if (formElements[i].id == "startingBid") {
                        $("yourStartingBid").className = $("yourStartingBid").className + " alertredr";
                    }
                    formElements[i].className = "error";
                    
                    var noticeTR = $("notice");
                    while (noticeTR.hasChildNodes()) {
                        noticeTR.removeChild(noticeTR.firstChild);
                    }
                    
                    var labelTd = $E({
                        tag: "td",
                        id: "noticeText",
                        className: "form_label"
                    });
                    var valueTd = $E({
                        tag: "td",
                        id: "noticetd",
                        className: "alertred"
                    });
                    $("notice").appendChild(labelTd);
                    $("notice").appendChild(valueTd);
                    $("noticeText").innerHTML = noticeText;
                    $("noticetd").innerHTML = isErrorMsg;
                    
                    return (false);
                }
                
            }
        }
        // In case of no error then submit the form.
        $(formName).submit();
        return (true);
    }
    else 
        if (value == 'payNow') {
            // TODO: Day 1+ payments
            document.location.href = 'info.html?view=Payments/openinvoices';
            return false;
        }
        else {
            if ($("maxBid") != null) {
                $("maxBid").value = "";
            }
            $(formName).submit();
            return (true);
        }
}

/*
 This function submit place bid from confirm bid page
 */
function submitBid(formName){
    $(formName).submit();
}

/**
 * Allow 0-9 to enter.
 * plus check empty/Null values
 * check the Min Value
 *
 */
function checkNumeric(objName, minval){
    var checkOK = "0123456789";
    var checkStr = objName;
    var allValid = true;
    var allNum = "";
    
    for (i = 0; i < checkStr.value.length; i++) {
        ch = checkStr.value.charAt(i);
        for (j = 0; j < checkOK.length; j++) 
            if (ch == checkOK.charAt(j)) 
                break;
        if (j == checkOK.length) {
            allValid = false;
            break;
        }
        allNum += ch;
    }
    if (!allValid) {
        if (checkStr.value.match(/[a-z]+/i)) {
            return (mustBeNumeric);
        }
        else {
            return (mustBeWholeNumbers);
        }
    }
    
    //check empty or Null
    if (allNum == null || allNum == "") {
        return (blankError);
    }
    //check minval
    if (allNum != "" && !(allNum >= minval)) {
        return (lessBidError);
    }
    return (null);
}

function formSubmit(formName, redirectURL){
    //$(formName).action = redirectURL;
    $(formName).submit();
}

function setFormValueAndSubmit(formName, redirectURL, field, value){
    $(field).value = value;
    formSubmit(formName, redirectURL);
}

function setFormValue(formName, field, value){
    $(field).value = value;
}

function lotPaging(formName, action){
    $(formName).method = "get";
    setFormValue(formName, 'lotDetailAction', action);
    formSubmit(formName, global.contextRoot + 'lotDetail.html');
}

function lotPagingWithId(formName, action, id){
    $(formName).method = "get";
    setFormValue(formName, 'lotDetailAction', action);
    setFormValue(formName, 'lotId', id);
    formSubmit(formName, global.contextRoot + 'lotDetail.html');
}

function addToWatchlist(lotId){
    document.lotform._eventId.value = "addToWatchlist";
    $("watchlistIds").value = lotId;
    $("lotform").submit();
}

/**
 * This function creates an XML string using the images information and passes
 * it a function that will create an XML document. This function also retrieves
 * and XSL document to which to format this XML with.
 */
function showLargeImagesDiv(){
    //Set the XSL path
    var xslFile = "info.html";
    //Turn the XML string into an XML document
    var xmlDoc = returnXMLDocument(xml);
    var xslDoc = "";
    //Make an AJAX request to get the XSL
    new Ajax.Request(xslFile, {
        method: "get",
        parameters: "view=Search/lotImagesLargeXsl",
        onSuccess: function(transport){
            //Take the XSL string and make an XML document
            xslDoc = returnXMLDocument(transport.responseText);
            //Pass the XML and XSL documents and get the display content
            var content = getFormattedXMLContent(xmlDoc, xslDoc);
            //IF we don't get an error that browser is not supported
            if (content != "error:not supported") {
                //Show the div that the images will reside in
                $("viewLargeImages").style.display = "block";
                //Change the text to the View Large Images link
                $("changeTxtLink").innerHTML = message.hideLarge;
                //Change its URL to point to the Hide Images function
                $("changeTxtLink").href = "javascript:hideLargeImagesDiv();";
                //If Mozilla/Opera
                if (document.implementation && document.implementation.createDocument) {
                    //Apend the data
                    $("largeImageLocation").appendChild(content);
                }
                //IE
                else 
                    if (window.ActiveXObject) {
                        //Write the content
                        $("largeImageLocation").innerHTML = content;
                    }
                    else {
                        //Else its a non supported broswer.
                        $("largeImageLocation").innerHTML = message.unsupportedBrowser;
                    }
            }
            else //We have an error
            {
                alert(message.unsupportedBrowser);
            }
        }
    });
}

/**
 * This function hides the div with the large images and clears the content. It
 * also resets the text for the View Large Images link and its href.
 */
function hideLargeImagesDiv(){
    $("changeTxtLink").innerHTML = message.showLarge;
    $("changeTxtLink").href = "javascript:showLargeImagesDiv();";
    $("viewLargeImages").style.display = "none";
    $("largeImageLocation").innerHTML = "";
}

/**
 * Added for Lot detail Popup thumb nail images
 */
function getImageDiv(controller, view, lot_ID, largeImage, title, imgUrl){
    var params = "lotId=" + lot_ID + "&imgUrl=" + imgUrl;
    createInfoDivPopup(controller, view, params, false);
}

/**
 * This function is used to display the large Image on the left side of the lot detail pop-up page.
 */
function SetImage(i){
    document.mainimage.src = fullImageURL[i];
}

/**
 * This object will be populated by pages that need to register the Enter key
 * form submission
 */
var functionParams = new Object();
functionParams["formName"] = "";
functionParams["msg"] = "";
functionParams["val"] = "";
functionParams["actionType"] = "";

/**
 * This method binds the onkeyup event to a function.
 */
document.onkeyup = KeyCheck;

/**
 * This function will capture on page event, looking specifically for the Enter key.
 * If the enter key is pressed and the page has prefilled the object functionParams
 * then it will take and submit form for validation via javascript.
 * @param e - the event that was triggered by the client.
 */
function KeyCheck(e){
    var KeyID = (window.event) ? event.keyCode : e.keyCode;
    if (KeyID == 13) {
        if (functionParams.formName != "") {
            return bidNow(functionParams.formName, functionParams.msg, functionParams.val, functionParams.actionType);
        }
    }
}

function goToPayments(){
    window.location.href = "openInvoices.html";
}

function showLargeImages(lotId, isOnSale){
    var d = new Date();
    var imagesURL = message.lotImagesAjax + "?lotId=" + lotId + "&isFullsize=true&isOnSale=" + isOnSale + "&d=" + d.getTime();
    var imageXslDoc = ""
    var imageXmlDoc = ""
    if (showFullSizeImages && $("lotLargeImages").innerHTML == "") {
        if (lotId != "") {
            new Ajax.Request(message.lotImagesLargeXsl, {
                method: "get",
                onSuccess: function(transport){
                    imageXslDoc = transport.responseText;
                    //imageXslDoc = returnXMLDocument(imageXslDoc);
                    new Ajax.Request(imagesURL, {
                        method: "get",
                        onSuccess: function(xmlTransport){
                            imageXmlDoc = xmlTransport.responseText;
                            //imageXmlDoc = returnXMLDocument(imageXmlDoc);
                            
                            if (imageXslDoc != "" && imageXmlDoc != "") {
                                var content = getFormattedXMLContent(imageXmlDoc, imageXslDoc);
                                if (content != "error: not supported") {
                                    if (showFullSizeImages) {
                                        showFullSizeImages = false;
                                        $("showImagesLink").innerHTML = message.hideLargeImages;
                                        $("showImagesLink2").innerHTML = message.hideLargeImages;
                                        $("lotLargeImages").style.display = "block";
                                        $("lotInfo").style.display = "block";
                                        if (document.implementation && document.implementation.createDocument) {
                                            //Apend the data
                                            $("lotLargeImages").appendChild(content);
                                        }
                                        //IE
                                        else {
                                            if (window.ActiveXObject) {
                                                //Write the content
                                                $("lotLargeImages").innerHTML = content;
                                            }
                                            else {
                                                //Else its a non supported broswer.
                                                $("lotLargeImages").innerHTML = message.unsupportedBrowser;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    });
                }
            });
        }
    }
    else {
        if (showFullSizeImages) {
            showFullSizeImages = false;
            $("showImagesLink").innerHTML = message.hideLargeImages;
            $("showImagesLink2").innerHTML = message.hideLargeImages;
            $("lotLargeImages").style.display = "block";
            $("lotInfo").style.display = "block";
        }
        else {
            showFullSizeImages = true;
            $("showImagesLink").innerHTML = message.showLargeImages;
            $("showImagesLink2").innerHTML = message.showLargeImages;
            $("lotLargeImages").style.display = "none";
            $("lotInfo").style.display = "none";
        }
    }
}

function getViewFinder(imageId, lotId, isOnSale){
    selectedImageId = imageId;
    var d = new Date();
    var imgViewerURL = message.imageViewerAjax + "?selectedImage=" + imageId + "&lotId=" + lotId + "&isOnSale=" + isOnSale + "&d=" + d.getTime();
    createInfoDivPopup(imgViewerURL, "Search/imageViewer", "img=" + imageId, false, "get");
}

function setLargeImage(imageId){
    $("imageViewerMainImage" + selectedImageId).style.display = "none";
    selectedImageId = imageId;
    $("imageViewerMainImage" + selectedImageId).style.display = "block";
}



function showPopupDiv(chk)
{
    if ($(chk).checked == 1) 
	{
        getInfoDivPage(null, 'Divs/pullServiceFee');
    }
    
}

