/**********************************

SCAinteractive
Promotion Engine JS Library
Use: common js functions
Author: Jason Hale

Version: 1.1
Modified: 11/23/06

***********************************/

/** Table Object **/
var Table = {
	init : function() {
        if (YAHOO.util.Dom) {
	    	var tables = YAHOO.util.Dom.getElementsByClassName('data');
    		for (var t=0;t<tables.length;t++) {
			    var rows = tables[t].getElementsByTagName('tr');
		    	for (var i=1,len=rows.length;i<len;i++) {
    				if (i % 2 == 0) rows[i].className = 'even';
	    			YAHOO.util.Event.addListener(rows[i],'mouseover',Table.mouseover);
			        YAHOO.util.Event.addListener(rows[i],'mouseout',Table.mouseout);
			    }
		    }   
        }
	},
	mouseover : function(row) {
		classname = this.className; 
		this.className='over';
	},
	mouseout : function(row) {
		classname = this.className = classname;
	},
	stripe : function(table) {
		var rows = table.getElementsByTagName('tr');
		for (var i=1,len=rows.length;i<len;i++) {
			if (i % 2 == 0) rows[i].className = 'even';
		}
	}
}

var Login = {
        username : Object,
        pwd : Object,
        init : function() {
                if(document.getElementById('login_form')) {
                        this.username = document.getElementById('username');
                        this.pwd = document.getElementById('password');
                        if (!this.username.value) this.username.style.background = '#fff url(/contests/html/master/img/login_username.gif) no-repeat left center;';
                        if (!this.pwd.value) this.pwd.style.background = '#fff url(/contests/html/master/img/login_password.gif) no-repeat left center;';
                        YAHOO.util.Event.addListener(this.username,'focus',this.focus);
                        YAHOO.util.Event.addListener(this.username,'blur',this.user_blur);
                        YAHOO.util.Event.addListener(this.pwd,'focus',this.focus);
                        YAHOO.util.Event.addListener(this.pwd,'blur',this.pwd_blur);
                }
        },
        focus : function() {
                this.style.background = '#fff';
        },
        user_blur : function() {
                if (!this.value) this.style.background = '#fff url(/contests/html/xhtml_master/img/login_username.gif) no-repeat bottom left;';
        },
        pwd_blur : function() {
                if (!this.value) this.style.background = '#fff url(/contests/html/xhtml_master/img/login_password.gif) no-repeat bottom left;';
        }
}

var ErrorBox = {
    // Adapted from Lightbox JS: Fullsize Image Overlays 
    //by Lokesh Dhakar - http://www.huddletogether.com
    show : function(error) {
    
        var objOverlay = document.getElementById('overlay');
        var objErrorBox = document.getElementById('errorBox');
        var objError = document.getElementById('error');
        var height = 200;
        var width = 342;

        var arrayPageSize = Utils.getPageSize();
        var arrayPageScroll = Utils.getPageScroll();    
        
        // set height of Overlay to take up whole page and show
        objOverlay.style.height = (arrayPageSize[1] + 'px');
        objOverlay.style.display = 'block';

        // center lightbox and make sure that the top and left values are not negative
        var errorBoxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - height) / 2);
        var errorBoxLeft = ((arrayPageSize[0] - 20 - width) / 2);

        objErrorBox.style.top = (errorBoxTop < 0) ? "0px" : errorBoxTop + "px";
        objErrorBox.style.left = (errorBoxLeft < 0) ? "0px" : errorBoxLeft + "px";
        
        objError.innerHTML = error;
        objErrorBox.style.display = 'block';
    },
    hide : function() {
        objOverlay = document.getElementById('overlay');
        objErrorBox = document.getElementById('errorBox');
        objError = document.getElementById('error');        
        objOverlay.style.display = 'none';
        objErrorBox.style.display = 'none'; 
        objError.innerHTML = "";
    }
}

var Utils = {
    popup : function(URL,Type,Width,Height) {
        var options="";
        if (Type=="console") options="resizable,scrollbars,height="+Height+",width="+Width;
        if (Type=="scrollable") options="scrollbars,height="+Height+",width="+Width; // added by PRD
        if (Type=="fixed") options="status,height="+Height+",width="+Width;
        if (Type=="elastic") options="toolbar,menubar,scrollbars,resizable,location,height="+Height+",width="+Width;
        window.open(URL, 'newWin', options);
    },
    swapDiv : function(hide, show) {
        document.getElementById(hide).style.display = "none";
        document.getElementById(show).style.display = "block";        
    },
    swapTable : function(hide,show) {
        document.getElementById(hide).style.display = "none";
        document.getElementById(show).style.display = "table";
    },
    getPageScroll : function() {
        // Returns array with x,y page scroll values.
        // Core code from - quirksmode.org
        var yScroll;
        if (self.pageYOffset) {
            yScroll = self.pageYOffset;
        } else if (document.documentElement && document.documentElement.scrollTop){  // Explorer 6 Strict
            yScroll = document.documentElement.scrollTop;
        } else if (document.body) {// all other Explorers
            yScroll = document.body.scrollTop;
        }
        
        arrayPageScroll = new Array('',yScroll) 
        return arrayPageScroll;
    },
    getPageSize : function() {
        // Returns array with page width, height and window width, height
        // Core code from - quirksmode.org
        var xScroll, yScroll;
        
        if (window.innerHeight && window.scrollMaxY) {  
            xScroll = document.body.scrollWidth;
            yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }
        
        var windowWidth, windowHeight;
        if (self.innerHeight) { // all except Explorer
            windowWidth = self.innerWidth;
            windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
        }   
        
        if(yScroll < windowHeight){
            pageHeight = windowHeight;
        } else { 
            pageHeight = yScroll;
        }
        
        if(xScroll < windowWidth){  
            pageWidth = windowWidth;
        } else {
            pageWidth = xScroll;
        }

        arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
        return arrayPageSize;
    }
}



/** Loading Function **/
function pageLoaders() {
	Table.init();
	Login.init();
}

YAHOO.util.Event.on(window,'load',pageLoaders); 
