// HBC base javaScript
//
//
// ====================================================== open_window()

var winCheck = null;

function open_window(url) {
    if (winCheck != null) {
	mywin.close();
    }
	mywin = open(url,"mywin",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=640,height=480,left=10,top=20,screeny=10,screenx=20');
	winCheck = 1;
}

// ====================================================== help_window()
var helpCheck = null;
function help_window(url) {
   if (helpCheck != null) {
	helpwin.close();
    }
      helpwin = open(url,"helpwin","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=480,height=550,titlebar=0,left=1,top=1,screeny=1,screenx=1");
      helpCheck = 1;
}

// ====================================================== newsWindow()
var newsCheck = null;
function newsWindow(url) {
   if (newsCheck != null) {
	newswin.close();
    }
      newswin = open(url,"mywin","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=800,height=550,left=1,top=1,screeny=1,screenx=1");
      newsCheck = 1;
}


function open_forum(){
    mywin = window.open('http://piazza.evalued.co.uk//cgi-bin/yabb2/YaBB.pl?action=login');
}
// ============ LAYER POPUP MESSAGES ==========================================

var baseText = null;
var PopUpList =[];
var showHPIReportButton = 0 ;
var HPIvrm;
var HPIchksum;

function showPopup(content,IDname,w,minh,offset){
    var popUp = document.getElementById(IDname);
    var overwrite = 0;
    // var scrH = screen.availHeight; # not IE
    // var scrW = screen.availWidth; # not IE
    var scrH = document.body.clientHeight;
    var scrW = document.body.clientWidth;

    // Grey out form
    var gf = document.getElementById("greyFilter");
    gf.style.left = "0px";
    gf.style.top = "0px";
    gf.style.width =  "100%";
    gf.style.height = "100%";
    // gf.style.width = scrW + "px";
    // gf.style.height = scrH + "px";
    gf.style.visibility = "visible";
    gf.style.zIndex = 1 ;

    // Make other popUps grey
    for(var i = 0; i < PopUpList.length; i++){
        var NpopUp = document.getElementById(PopUpList[i]);
        if ( PopUpList[i] == IDname ){
            overwrite = 1;
        }else{
            NpopUp.style.backgroundColor = "#DFE3E3";
        }
    }
    // Put this popups ID on the popup stack unless it is already there
    if ( ! overwrite){
        PopUpList.push(IDname);
    }
    var Left = (scrW - w) * .5;
    // Left = Left + offset;
    popUp.style.left = Left + "px";
    popUp.style.width = w + "px";
    popUp.style.zIndex = (PopUpList.length +1) ;

   if (baseText == null) baseText = popUp.innerHTML;
    var IDtext = IDname + "text";
    var IDbar = IDname + "bar";

    var text = '<div id="' + IDtext + '" align="center"><font face="arial, verdana, helvetica" size=2> <b>HBC On Line Auctions - Warning Message</b><br><br>' +content+ '</font></div>' ;

    var ButtonBar = "<div id='" + IDbar + "' align=\"center\" >" ;
    ButtonBar = ButtonBar + "<button onclick=\"hidePopup('" + IDname + "');\">Close window</button>";
    if ( showHPIReportButton){
         ButtonBar = ButtonBar + "&nbsp;&nbsp;&nbsp;&nbsp;<button onclick=\"HPIButton('" + HPIvrm + "','" + HPIchksum + "');\">Full HPI Report</button>";
    }
    ButtonBar = ButtonBar + "</div>";
    // popUp.innerHTML = text + "<div id='" + IDbar + "' align=\"center\"
    // ><button onclick=\"hidePopup('" + IDname + "');\">Close
    // window</button></div>";
    popUp.innerHTML = text + ButtonBar ;

    var textBlock = document.getElementById(IDtext);
    // offsetHeight rather than height required for IE as div "textBlock" is
    // not yet part of DOM
    var textHeight = textBlock.offsetHeight;
    if ( textHeight < minh ){
        textHeight = minh ;
    }
    popUp.style.height = (textHeight + 60) + "px";
    var Top = (scrH - textHeight +60) * .5;
    Top = Top + offset;
    popUp.style.top = Top + "px";
    var sbar = document.getElementById(IDbar);
    sbar.style.marginTop = 20 + "px";
    popUp.style.visibility = "visible";
}

function hidePopup(Xname){
    var popUp = document.getElementById(Xname);
    var gf = document.getElementById("greyFilter");
    if (Xname == PopUpList[PopUpList.length - 1 ]) {
        popUp.style.visibility = "hidden";
        PopUpList.pop();
        // make next box white
        var NextBox = document.getElementById(PopUpList[PopUpList.length - 1 ]);
        if (NextBox){
            NextBox.style.backgroundColor = "white";
        }
        if (PopUpList.length == 0){
            // no more boxes ...
            gf.style.visibility = "hidden";
            gf.style.zIndex = -1;
        }
    }

}

function HPIButton(VRM, chksum){
    VRM = VRM.toUpperCase() ;
    if(VRM.length <= 1){
        alert("HPIButton(): Registration Number Required");
        return ;
    }
    var mode = "live" ;
    var force = 'N' ;
    var URL = '/cgi-bin/appstar/D=nodb/V=HPI_display/REG=' + VRM + '/MODE=' + mode + '/FORCE=' + force + '/CHK=' + chksum + '/USER=' + LogHPIReportUser;
    newsWindow(URL);
}

// ============ END LAYER POPUP MESSAGES =======================================
// ============ START OF FUNCTIONS USED BY form_fields_include.tt ==============
function ClearError(field, OKstyle){
    var errfield = "ERR_" + field ;
    window.document.getElementById(errfield).innerHTML = "&nbsp;" ;
    window.document.getElementById(field).className = OKstyle ;
}
// ============ END OF FUNCTIONS USED BY form_fields_include.tt ================
