
// Add indexOf for IE6 browsers
// ref: http://stackoverflow.com/questions/143847/best-way-to-find-an-item-in-a-javascript-array
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(obj, fromIndex) {
		if (fromIndex == null) {
			fromIndex = 0;
		} else if (fromIndex < 0) {
			fromIndex = Math.max(0, this.length + fromIndex);
		}
		for (var i = fromIndex, j = this.length; i < j; i++) {
			if (this[i] === obj)
				return i;
		}
		return -1;
	};
}

function isUndefined(x){var u; return x === u;}

function useV2CSS() {
	var UpdatedTemplates = []; //[2,15,26];  //
	try {
		return (UpdatedTemplates.indexOf(TemplateID) != -1);
	} catch (e) {
		return false;
	}
}


var heightControl 
var ArrayFrameName = new Array();
var ArrayFrameHeight = new Array();

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 removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

function getViewportHeight() {
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() {
    if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 	
	if (document.body) return document.body.clientWidth; 
	
	return 500;	
}

/**
 * Gets the real scroll top
 */
function getScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}
function getScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}
function RefreshFromSiteEdit()
{
	var sLocation;
	
	sLocation = document.location.toString();
    sLocation = sLocation.toLowerCase();

	if ((sLocation.indexOf('?refresh=true') != -1) || (sLocation.indexOf('&refresh=true') != -1))
	{
		document.location = document.location;
	}
	else
	{
		if (sLocation.indexOf('?') != -1)
		{
			document.location = document.location + '&Refresh=True';
		}
		else
		{
			document.location = document.location + '?Refresh=True';
		}
	}
}

function TopWindow() {
	// The top window of My-PTA, even if it is embedded in an iframe
	// Will be either index.asp (or /), content.asp, or admin.asp; these files include TopHead.asp, where TopHead = true

	for (var win = window; true; win = win.parent ) {
		//alert('Testing ' + win.location.toString());
		try {
			if (win.TopHead) {
				//alert(win.location.toString() + ' is top window');
				return win;
			}
		}
		catch (e) {
		}
		if (win == win.parent) return win;
	}
}

function RefreshParent()
{
	var sLocation;
	var sOpener = window.opener
	if (sOpener && sOpener != window)  {
		try {
			sLocation = sOpener.document.location.toString();
			sLocation = sLocation.toLowerCase();

			if ((sLocation.indexOf('?refresh=true') != -1) || (sLocation.indexOf('&refresh=true') != -1)) {
				window.opener.document.location = window.opener.document.location;
			}
			else {
				if (sLocation.indexOf('?') != -1) {
					window.opener.document.location = window.opener.document.location + '&Refresh=True';
				}
				else {
					window.opener.document.location = window.opener.document.location + '?Refresh=True';
				}
			}
		} catch (e) { }
	}
}

function ClosePopupRefreshParent()
{
	var sLocation;
	
	sLocation = window.opener.document.location.toString();
	sLocation = sLocation.toLowerCase();
	
	if ((sLocation.indexOf('?refresh=true') != -1) || (sLocation.indexOf('&refresh=true') != -1))
	{
		window.opener.document.location = window.opener.document.location;
	}
	else
	{
		if (sLocation.indexOf('?') != -1)
		{
			window.opener.document.location = window.opener.document.location + '&Refresh=True';
		}
		else
		{
			window.opener.document.location = window.opener.document.location + '?Refresh=True';
		}
	}
	
	window.close(false);
}
function CloseModalRefreshParent()
{
	var sLocation;
	
	sLocation = parent.document.location.toString();
	sLocation = sLocation.toLowerCase();
	
	parent.hidePopWin(false);
	
	if ((sLocation.indexOf('?refresh=true') != -1) || (sLocation.indexOf('&refresh=true') != -1))
	{
		parent.document.location = parent.document.location;
	}
	else
	{
		if (sLocation.indexOf('?') != -1)
		{
			parent.document.location = parent.document.location + '&Refresh=True';
		}
		else
		{
			parent.document.location = parent.document.location + '?Refresh=True';
		}
	}
}

function CloseModalRefreshFrame(sFrameID)
{
	var TopWin = TopWindow();
	var oFrame = TopWin.frames[sFrameID];

	TopWin.hidePopWin(false);
	if (oFrame) oFrame.location = oFrame.location;

/*
	if ((sLocation.indexOf('?refresh=true') != -1) || (sLocation.indexOf('&refresh=true') != -1))
	{
		parent.document.location = parent.document.location;
	}
	else
	{
		if (sLocation.indexOf('?') != -1)
		{
			parent.document.location = parent.document.location + '&Refresh=True';
		}
		else
		{
			parent.document.location = parent.document.location + '?Refresh=True';
		}
	}
	*/
}

// Add refresh=true and template=true if not already there
function CloseTemplateRefreshParent()
{
	var sLocation, sLocationOriginal;
	
	sLocationOriginal = parent.document.location.toString();
	sLocation = parent.document.location.toString().toLowerCase();

	if ((sLocation.indexOf('?refresh=true') == -1) && (sLocation.indexOf('&refresh=true') == -1))   // No refresh=true? Add it
	{
	    if (sLocation.indexOf('?') == -1)
		    sLocationOriginal = sLocationOriginal + '?refresh=true';
		else
		    sLocationOriginal = sLocationOriginal + '&refresh=true';
	}
		
	if ((sLocation.indexOf('?template=t') == -1) && (sLocation.indexOf('&template=t') == -1))       // No template=true? Add it
	{
	    if (sLocation.indexOf('?') == -1)
		    sLocationOriginal = sLocationOriginal + '?Template=T';
		else
		    sLocationOriginal = sLocationOriginal + '&Template=T';
	}
	
	parent.document.location = sLocationOriginal;
}



function HideContentIFrames()
{
	var oContent;
	var aoIFrames;
	var sSource;
	var x;
	
	oContent = parent.document.getElementById("content");
	
	if (oContent != null)
	{
		aoIFrames = oContent.getElementsByTagName('iframe');
		
		if (aoIFrames != null)
		{
			for (x=0;x<aoIFrames.length;x++)
			{
				sSource = aoIFrames[x].src;
				sSource = sSource.toLowerCase();
				
				if ((sSource.indexOf('.pdf') != -1) || (sSource.indexOf('getdocument.asp') != -1))
				{
					aoIFrames[x].style.display = 'none';
				}
			}
		}
	}
}
function Popup(theURL, Name, popW, popH, scroll, resize) 
{
	//alert(theURL);
	Name = Name.replace(/[^a-zA-Z]+/g,'');
	var winleft = (screen.width - popW) / 2;
	var winUp = (screen.height - popH) / 2;
	winProp = 'width='+popW+',height='+popH+',left='+winleft+',top='+winUp+',scrollbars='+scroll+',resizable='+resize;
	Win = window.open(theURL, Name, winProp);
	if (Win)
		Win.window.focus();
	return false;
}
function QueryString_Parse(sQuery)
{
	var pairs = sQuery.substring(1).split("&");

	for (var i=0;i<pairs.length;i++)
	{
		var pos = pairs[i].indexOf('=');
		if (pos >= 0)
		{
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			
			QueryString.keys[QueryString.keys.length] = argname;
			QueryString.values[QueryString.values.length] = value;
		}
	}
}
function QueryString(key)
{
	var value = null;
	for (var i=0;i<QueryString.keys.length;i++)
	{
		if (QueryString.keys[i]==key)
		{
			value = QueryString.values[i];
			break;
		}
	}
	return value;
} 
function SwapAdminMode(sImage, sStatus, sFrame, sPage, lIndex, sApprove, sGuest,nMode)
{	

	var oAdminImage  = document.getElementById(sImage);
	var oAdminStatus = document.getElementById(sStatus);
	var bToAdmin;
	//News/viewnews.asp
	bToAdmin = (oAdminImage.src.indexOf('admin.gif') >= 0);
	if (bToAdmin)
	{
		oAdminImage.src = 'images/user.gif';
		oAdminStatus.style.display = '';
		if(sPage == 'News/viewnews.asp')
		{
			window.frames[sFrame].document.location = sPage + '?ContentIndex=' + lIndex + '&Nmode=' + nMode + '&Mode=' + sApprove + '&Status=0';
		}
		else
		{
			window.frames[sFrame].document.location = sPage + '?ContentIndex=' + lIndex + '&Mode=' + sApprove + '&Status=0';
		}
		
	}
	else
	{
		oAdminImage.src = 'images/admin.gif';
		oAdminStatus.style.display = 'none';
		if(sPage == 'News/viewnews.asp')
		{
			window.frames[sFrame].document.location = sPage + '?ContentIndex=' + lIndex + '&Nmode=' + nMode + '&Mode=' + sGuest + '&Status=0';
		}
		else
		{
			window.frames[sFrame].document.location = sPage + '?ContentIndex=' + lIndex + '&Mode=' + sGuest + '&Status=0';
		}
		
	}
}
function SwapAdminMode1(sImage, sStatus, sFrame, sPage, lIndex, nPageID, sApprove, sGuest, nMode, sAnchor, sUAnchor) 
{

	var oAdminImage  = document.getElementById(sAnchor);
    var oUserImage  = document.getElementById(sUAnchor);
	var oAdminStatus = document.getElementById(sStatus);
	if (sImage == 'Admin') {
		oAdminImage.style.display = 'none';
		oUserImage.style.display = '';
		if (sPage == 'News/viewnews.asp' || sPage == 'PhotoGallery/ShowPhotos.asp' || sPage == 'DocumentStore/ShowDocument.asp') {
			window.frames[sFrame].document.location = sPage + '?UniqueHTMLID='+ sFrame +'&ContentIndex=' + lIndex + '&Nmode=' + nMode + '&Mode=' + sApprove + '&Status=0&Visible=Admin';
		}
		else if (sPage == 'Directory/events.asp') {
			window.frames[sFrame].document.location = sPage + '?UniqueHTMLID='+ sFrame +'&ContentIndex=' + lIndex + '&mode=edit';				
		}
		else if (sPage == 'eCommerce/Catalog.aspx' || sPage == 'Sponsors/SponsorList.aspx') {
		    window.frames[sFrame].document.location = sPage + '?UniqueHTMLID=' + sFrame + '&ContentIndex=' + lIndex + '&PageID=' + nPageID + '&Mode=' + nMode + '&Status=0&Visible=Admin';
        }
		else {
		    window.frames[sFrame].document.location = sPage + '?UniqueHTMLID=' + sFrame + '&ContentIndex=' + lIndex + '&Mode=' + sApprove + '&Status=0';
		}
		
	}
	else {

        oAdminImage.style.display = '';
		oUserImage.style.display = 'none';
		if (sPage == 'News/viewnews.asp' || sPage == 'PhotoGallery/ShowPhotos.asp' || sPage == 'DocumentStore/ShowDocument.asp') {
			window.frames[sFrame].document.location = sPage + '?UniqueHTMLID='+ sFrame +'&ContentIndex=' + lIndex + '&Nmode=' + nMode + '&Mode=' + sGuest + '&Status=0&Visible=User';
		}
		else if (sPage == 'eCommerce/Catalog.aspx' || sPage == 'Sponsors/SponsorList.aspx') {
		    window.frames[sFrame].document.location = sPage + '?UniqueHTMLID=' + sFrame + '&ContentIndex=' + lIndex + '&PageID=' + nPageID + '&Mode=' + nMode + '&Status=0&Visible=User';		
        }
		else {
			window.frames[sFrame].document.location = sPage + '?UniqueHTMLID='+ sFrame +'&ContentIndex=' + lIndex + '&Mode=' + sGuest + '&Status=0';
		}
		
	}	
	setTall() ;
}

function setTall() {
	if (typeof jQuery != 'undefined') {
	//if (true) {
		setTallNew();
	} else {
		setTallOld();
	//alert('old');
	}
}

function setTallNew() {
	var minHeight = 800;
	var columns = $('#left,#middle,#right');
	columns.each(function () {
		// Get the Contents
		var $this = $(this);
		$this.css('height', 'auto');
		var height = $this.contents().height() ? $this.contents().height() : $this.height();
		height = $this.height() > height ? $this.height() : height;
		//var height = $(this).contents().innerHeight() ? $(this).contents().innerHeight() : $(this).innerHeight();
		//height = $(this).innerHeight() > height ? $(this).innerHeight() : height;
		minHeight = Math.max(minHeight, height);
	});
	columns.each(function() {
		$(this).css('height', minHeight + 'px');
	});
	
}

function setTallOld() {
	if (useV2CSS()) return;
	
	if (document.getElementById) {

		var divs = new Array(
			//document.getElementById('pageContent'), 
			document.getElementById('middle'), 
			document.getElementById('left'),
			document.getElementById('right')
			);

		var maxHeight = 0;
		for (var i = 0; i < divs.length; i++) {
			
			if (divs[i] && divs[i].offsetHeight > maxHeight) 
			{
 				maxHeight = divs[i].offsetHeight;
				
			}
		}
		
		if(maxHeight < 600)
		{
			maxHeight = 600;
		}
		
		
		for (var i = 0; i < divs.length; i++) {
			if (divs[i]) {
				divs[i].style.height = maxHeight + 'px';

				if (divs[i].offsetHeight > maxHeight) {
					maxHeight = divs[i].offsetHeight;  //+ 10;	  //Chrome and Safari resize the offset on the fly (without padding!)
				}
				
			}
		}
	}
	
	
	heightControl = maxHeight;
	
}

var timeOutSession;

function setSessionTimeOut(nTimeOut)
{
	clearTimeout(timeOutSession);
	timeOutSession = self.setTimeout('showTimeOutPopUp()', nTimeOut)
}

function setSessionTimeOutDefault() {
	setSessionTimeOut(1200000);
}

function showTimeOutPopUp()
{
   showPopWin('../ContentEditor/Timeout.asp', 300, 120, null,false);
}


function getQuerystring(key, default_)
{
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}


function setTallFrames( frameHeight, frameId ) {
	var  htDifference = 0;		
	if (!document.getElementById) 
		return;
		
	var divs = new Array(
		//document.getElementById('pageContent'), 
		document.getElementById('middle'), 
		document.getElementById('left'),
		document.getElementById('right')
		);

	// Find the max height of the three content panels
	var maxHeight = 0;
	for (var i = 0; i < divs.length; i++)
	{
		if ((divs[i]!=null) && (divs[i].offsetHeight > maxHeight) )
		{
			maxHeight = divs[i].offsetHeight + 10;
		}
	}	
		
	if (ArrayitemExists(frameId, ArrayFrameName) == false)
	{		  
		var maxlength = parseInt(ArrayFrameName.length);		   
		ArrayFrameName[maxlength] = frameId;
		ArrayFrameHeight[maxlength] = frameHeight;		   	   		   		   
	}
  
	if(heightControl > 0)
	{
		for(var i = 0; i < ArrayFrameName.length; i++)
		{		   
			if(ArrayFrameName[i] == frameId)
			{		      
				htDifference = parseInt(frameHeight) - parseInt(ArrayFrameHeight[i]);		     
				if(parseInt(htDifference) < 0)
					htDifference = 0 - parseInt(htDifference);
	   //ArrayFrameHeight[i]=htDifference;
	   
				htDifference = heightControl + htDifference;  
			}
		}

		for (var i = 0; i < divs.length; i++) 
		{            			     
            //if(maxHeight<htDifference)
            divs[i].style.height= htDifference + "px";
            //else
            //  divdd[i].style.height= maxHeight + "px";
        }
	}		
	 ////*****************************************************///	
}

function ArrayitemExists(frameid,arrayName)
{
  var status=false; //Ites does not exists in the array 
  for(var i=0;i<arrayName.length;i++)
  {  
   if(arrayName[i]==frameid)
    {      
      status=true;
      break;
    }
  } 
  return status;
}


function autoResize(id)
{
    var newheight;

    if(document.getElementById){
    newheight=document.getElementById(id).contentWindow.document.body.scrollHeight;
    }

    document.getElementById(id).height= (newheight) + "px";
}


function SetStatusImageBorder(frameId,StatusImageame)
{
	//alert(frameId);
	alert(StatusImageame);
	
	}

	function setCookie(c_name, value, expiredays) {
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + expiredays);
		var c = c_name + "=" + encodeURIComponent(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
		//var c = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
		document.cookie = c;
		return c;
	}
	
	function getCookie(c_name) {
		if (document.cookie.length > 0) {
			c_start = document.cookie.indexOf(c_name + "=");
			if (c_start != -1) {
				c_start = c_start + c_name.length + 1;
				c_end = document.cookie.indexOf(";", c_start);
				if (c_end == -1) c_end = document.cookie.length;
				return decodeURIComponent(document.cookie.substring(c_start, c_end));
				//return unescape(document.cookie.substring(c_start, c_end));
			}
		}
		return "";
	}
