addEvent(window, "load", preparePage);

var newWindow = null;

function preparePage() {
    // We don't actually need createElement, but we do
    // need good DOM support, so this is a good check.
    if (!document.createElement) return;
    if(document.getElementById("limitedTextarea")){
    	prepareTextarea(document.getElementById("limitedTextarea"));
    	textCounter(document.getElementById("limitedTextarea"),"textareaCounter",250);
    }
    uls = document.getElementsByTagName("textarea");
    for (uli=0;uli<uls.length;uli++) {
        theT = uls[uli];
        if (theT.nodeName == "TEXTAREA" && theT.className.indexOf("limitedTextarea") > -1){
            prepareTextarea(theT);
            textCounter(theT,"",250);
        }
    }
	PageLoader();
	ParseATags();

	if(document.getElementsByTagName("body")[0].id == "PoppaImage"){
		PoppaResizer(document.getElementsByTagName("body")[0]);
	}
	checkFrames();
	//DivPop();
}

function checkFrames(){
	if (self != top) {
		//if (document.images)
		//	top.location.replace(window.location.href);
		//else
			top.location.href = window.location.href;
	}
}

function prepareTextarea(textarea){
	textarea.onkeydown = function(){
		textCounter(textarea,"textareaCounter",250);return true;
    }
    textarea.onkeyup = function() {
    	textCounter(textarea,"textareaCounter",250);return true;
    }
}
function textCounter(field,cntfield,maxlimit) {	
	if (field.value.length > maxlimit)	field.value = field.value.substring(0, maxlimit);
	var st = (maxlimit - field.value.length) + " Characters Left";
	var node;
	if(cntfield.length > 0 && document.getElementById(cntfield)){
	    node = document.getElementById(cntfield);
	}else{
	    node = field.nextSibling;
	}
	if(node.innerHTML){
    	node.innerHTML = st;
    }else{
	    if(node.firstChild)node.firstChild.nodeValue = st;
    }
}

function confirm_entry(direct,message)
{
	input_box=confirm(message);
	if (input_box==true)
	{ 
		// Output when OK is clicked
		this.window.location=direct; 
	}
}



function ParseATags(){
	closeWin();
	uls = document.getElementsByTagName("a");
    for (uli=0;uli<uls.length;uli++) {
        theA = uls[uli];
        if (theA.nodeName == "A"){
            if (theA.className == "poppa" || theA.className == "popup"){
                theA.setAttribute('href', '#');
			    IdValue = theA.id.split("-");
			    if(IdValue.length > 2 && IdValue[0] == "ID"){
				    theA.onclick = function() {
					    IdValue = this.id.split("-");
					    newWindow = window.open("/image.aspx?p=" + IdValue[2] + "&m=" + IdValue[1] + "&","poppa_popup","height=100,width=100,scrollbars=no,toolbar=no,directories=no,menubar=yes,resizable=yes,status=yes");
					    newWindow.focus();
					    return false;
				    }
			    }else if(IdValue.length > 2 && IdValue[0] == "FILE"){
				    theA.onclick = function() {
					    IdValue = this.id.split("-");
					    newWindow = window.open("/image.aspx?f=" + IdValue[1] + "." + IdValue[2] + "&w=" + IdValue[3] + "&h=" + IdValue[4] + "&c=" + IdValue[5] + "&","poppa_popup","height=' + (IdValue[4]*1 + 20) + ',width=' + (IdValue[3]*1 + 20) + ',scrollbars=no,toolbar=no,directories=no,menubar=yes,resizable=yes,status=yes");
					    newWindow.focus();
					    return false;
				    }
			    }else{
			         theA.onclick = function() {
                        if(this.firstChild.src){
    					    var st = this.firstChild.src;
    					    newWindow = window.open("/image.aspx?f=" + st.replace('-th','') + "&","poppa_popup","height=100,width=100,scrollbars=no,toolbar=no,directories=no,addressbar=yes,menubar=yes,resizable=yes,status=yes");
	    				    newWindow.focus();
		    			    return false;
		    			}
				    }
			    }
			    theA.onmouseover = function() {
		            window.status = "Click for larger image";
				    return true;
		        }
		        theA.onmouseout = function() {
		            window.status = "";
				    return true;
		        }
		     }
		 }
    }
}

function closeWin(){
	if (newWindow != null){
		if(!newWindow.closed)
			newWindow.close();
	}
}

function PoppaResizer(body){
	if(document.images[0]){
		img = document.images[0];
		var ws = ICJ_browserWindowSize();
		var widthpadding = body.style.paddingLeft.replace("px","")*1 + body.style.paddingRight.replace("px","")*1;
		//if(body.style.marginLeft.replace("px","")*1 > 0){
		//	widthpadding = widthpadding + body.style.marginLeft.replace("px","")*1 + body.style.marginRight.replace("px","")*1;
		//}
		var heightpadding = body.style.paddingTop.replace("px","")*1 + body.style.paddingBottom.replace("px","")*1;
		//if(body.style.marginTop.replace("px","")*1 > 0){
		//	heightpadding = heightpadding + body.style.marginTop.replace("px","")*1 + body.style.marginBottom.replace("px","")*1;
		//}
		
		var heightAdjust = 0;
		uls = document.getElementsByTagName("div");
		for (uli=0;uli<uls.length;uli++) {
			if(uls[uli].className == "BossCloseWindow"){
				heightAdjust = 20;
				break;
			}
		}
		
		window.resizeBy((img.width + widthpadding) - ws['width'], (img.height + heightpadding + heightAdjust) - ws['height']);
		//window.resizeTo(img.width + widthpadding, img.height + heightpadding + heightAdjust);
		self.focus();
	}
}

function ICJ_browserWindowSize() {
	var r = new Array();
	r['width'] = 0;
	r['height'] = 0;
	if(typeof(window.innerWidth) == 'number' ) {
		//Non-IE
		r['width'] = window.innerWidth;
		r['height'] = window.innerHeight;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		r['width'] = document.documentElement.clientWidth;
		r['height'] = document.documentElement.clientHeight;
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		r['width'] = document.body.clientWidth;
		r['height'] = document.body.clientHeight;
	}
	return r;
}


//Depreciated
function pops(u,w,h){
	newwindow=open(u,"_blank","scrollbars=no, toolbar=no,directories=no, menubar=yes, resizable=yes,status=yes, width=" + w + ", height=" + h + "");
}
//Depreciated



// adds an eventListener for browsers which support it.
function addEvent(obj, evType, fn){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, false);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}

function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent){
		curtop += obj.offsetHeight;
		while (obj.offsetParent){
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y){
		curtop += obj.y;
		curtop += obj.height;
	}
	return curtop;
}

function openClose(itemName,itemNameB,itemBack){
	if (document.getElementById(itemName)){
			if (document.getElementById(itemNameB)){
				if (document.getElementById(itemBack)){
					if (document.getElementById("switch").innerHTML == itemNameB){
						document.getElementById(itemName).innerHTML = document.getElementById(itemBack).innerHTML;
						document.getElementById("switch").innerHTML = itemBack;
					}else{
						document.getElementById(itemName).innerHTML = document.getElementById(itemNameB).innerHTML;
						document.getElementById("switch").innerHTML = itemNameB;
					}
				}
			}
		}
}




/*
function DivPop() {
	uls = document.getElementsByTagName("a");
    for (uli=0;uli<uls.length;uli++) {
        theA = uls[uli];
        if (theA.nodeName == "A" && theA.className == "popped") {
            setAs(theA);
        }
    }
    if(document.getElementById("picwindow")){
		pw = document.getElementById("picwindow");
		pw.firstChild.onclick = function() {
			this.parentNode.className = "thehid";
		}
	}
}

function setAs(theA) {
	theA.setAttribute('href', '#');
	theA.onclick = function() {
		if(theA.firstChild.firstChild.src){
			stringBits = theA.firstChild.firstChild.src.split("/");
			stringBits = stringBits[(stringBits.length - 1)];
			while (stringBits.indexOf("-th") > -1)
 				stringBits = stringBits.replace("-th","");
	       	piccie = document.getElementById("picwindow");
	       	
	       	if(piccie.firstChild.firstChild.firstChild.src.indexOf(stringBits) > 0){
		       	piccie.firstChild.firstChild.firstChild.src = "/bossassets/loader.gif";
		       	piccie.className = "thehid";
		    }else{
		    	theA.firstChild.src = "/bossassets/loader.gif";
		       	piccie.className = "thepop";
		       	piccie.firstChild.firstChild.firstChild.src = theA.firstChild.firstChild.src.replace("-th","");
		      	//alert(theA.firstChild.firstChild.src);
		       	//newx = (findPosY(theA.firstChild) - theA.firstChild.width/2);
		       	//newy = (findPosX(theA.firstChild) - theA.firstChild.height/2);
		       	//if(newx < 0) newx = 0;
		       	//if(newy < 0) newy = 0;
		       	//piccie.style.top = newx+"px";
		        //piccie.style.left = newy+"px";
		   	}
    	}
		return true;
    }

    theA.onmouseover = function() {
        window.status = "Click for larger image";
		return true;
    }
    theA.onmouseout = function() {
        window.status = "";
		return true;
    }
    
}
*/
