// Javascript COPYRIGHT PowerofPixels.com 2005.
// UNLAWFUL DUPLICATION WILL RESULT IN PROSECUTION

squareCounter = 0;
selectedSquares = new Array(3);

function addImage(parent, src, alt, id, dbid, left, top, width, height, zIndex) {
    
    // validation
    if (isNaN(parseInt(dbid))) {
        dbid = -1;
    }
    if (isNaN(left)) {
        left = 0;
    }
    if (isNaN(top)) {
        top = 0;
    }
    if (isNaN(width)) {
        width = 0;
    }
    if (isNaN(height)) {
        height = 0;
    }
    if (isNaN(zIndex)) {
        zIndex = 1;
    }
    
    
    var oImg = document.createElement('img');
    oImg.src = src;
    oImg.id = id;
    
    if (parseInt(dbid) > 0) {
        oImg.onclick = function() { 
            winID = id + '_' + dbid;
            //var clickWin = null;
            var clickWin = winID;
            clickWin = window.open('', winID);
            clickWin.document.location.href = 'index.php?action=click&id=' + dbid;
            clickWin.focus();
            
            // grey out the "just clicked" box
            //overlayStripe(parent, dbid, alt, left, top, width, height, zIndex);
        };
        oImg.onmouseover = function() {
            this.className = "hand";
            window.status = alt;
        }
        oImg.alt = alt;
        oImg.title = alt;
    } else {
        oImg.alt = "This ad space is reserved, pending approval";
        oImg.title = "This ad space is reserved, pending approval";
    }
    parent.appendChild(oImg);
    oImg.style.position = 'absolute';
    oImg.style.top = top;
    oImg.style.left = left;
    oImg.style.width = width;
    oImg.style.height = height;
    oImg.style.zIndex = zIndex;
}

function addImageSelect(parent, src, alt, id, dbid, left, top, width, height, zIndex) {
    
    // validation
    if (isNaN(parseInt(dbid))) {
        dbid = -1;
    }
    if (isNaN(left)) {
        left = 0;
    }
    if (isNaN(top)) {
        top = 0;
    }
    if (isNaN(width)) {
        width = 0;
    }
    if (isNaN(height)) {
        height = 0;
    }
    if (isNaN(zIndex)) {
        zIndex = 1;
    }
    
    
    var oImg = document.createElement('img');
    oImg.src = src;
    oImg.id = id;
    
    //oImg.onclick = location.reload();
    this.className="hand";
    oImg.alt = "This ad space is selected to buy";
    oImg.title = "This ad space is selected to buy";
    
    parent.appendChild(oImg);
    oImg.style.position = 'absolute';
    oImg.style.top = top;
    oImg.style.left = left;
    oImg.style.width = width;
    oImg.style.height = height;
    oImg.style.zIndex = zIndex;
}

function overlayStripe(parent, dbid, alt, left, top, width, height, zIndex) {
    // validation
    if (isNaN(left)) {
        left = 0;
    }
    if (isNaN(top)) {
        top = 0;
    }
    if (isNaN(width)) {
        width = 0;
    }
    if (isNaN(height)) {
        height = 0;
    }
    if (isNaN(zIndex)) {
        zIndex = 5;
    }
    
    var oDiv = document.createElement('div');
    oDiv.className="overlay";
    
    if (parseInt(dbid) > 0) {
        oDiv.onclick = function() { 
            // var clickWin = null;
            clickWin = window.open('', 'clickWin');
            clickWin.document.location.href = '/adclick/?id=' + dbid;
            clickWin.focus();
        };
    }
    
    parent.appendChild(oDiv);
    oDiv.style.position = 'absolute';
    oDiv.style.top = top;
    oDiv.style.left = left;
    oDiv.style.width = width;
    oDiv.style.height = height;
    oDiv.style.zIndex = zIndex;
}

function doSubmit(){
    // check rectangle first
    rectangleTest = checkRectangle();
    
    if (! rectangleTest ) {
        alert('You must enter squares that form a rectangle.\nPlease alter your selection so that you form a perfect rectangle.');
        return false;
    }
    
    document.pixelMapForm.submit();
}

function changeMessage(layer, message, isError){
    layer.innerHTML = message;
    if (isError) {
        layer.style.color= 'red';
        layer.style.fontWeight= 'bold';
    } else {
        layer.style.color = 'black';
        layer.style.fontWeight= 'normal';
    }
}
