// -- PURPOSE: to check/uncheck all checkboxes in a form
// -- Usage: checkAll(checkbox that checks/unchecks all, string limiting checkboxes to those whose name or id begins with it)
function checkAll(checkallbox, beginsWith) {
	var myForm = checkallbox.form;
	for (var i=0;i<myForm.elements.length;i++) {
		var myElement = myForm.elements[i];
		if (myElement != checkallbox && myElement.type == "checkbox") {
			if ((beginsWith && (myElement.id.indexOf(beginsWith) == 0 || myElement.name.indexOf(beginsWith) == 0)) || (!beginsWith)) {
				myElement.checked = checkallbox.checked;
			}
		}
	}
}

// -- PURPOSE: to collapse all elements
// -- Usage: collapseAll(list of items to collapse)
function collapseAll() {
	for (var i=0; i<collapseAll.arguments.length; i++) {
		var element = document.getElementById(collapseAll.arguments[i]);
		if (element) {
			element.style.display = "none";
		}
	}
}

function deleteConfirm(delete_message) {
	return window.confirm(delete_message);
}

// -- PURPOSE: to expand all elements
// -- Usage: expandAll(list of items to expand)
function expandAll() {
	for (var i=0; i<expandAll.arguments.length; i++) {
		var element = document.getElementById(expandAll.arguments[i]);
		if (element) {
			element.style.display = "block";
		}
	}
}

// -- PURPOSE: to expand collapsed elements or collapse expanded elements
// -- Usage: expandCollapse(list of items to expand/collapse)
function expandCollapse() {
	for (var i=0; i<expandCollapse.arguments.length; i++) {
		var element = document.getElementById(expandCollapse.arguments[i]);
		if (element) {
		
			//toggle visible or invisible
			element.style.display = (element.style.display == "none") ? "block" : "none";
			
			//if we are showing the popup dialog, then also center it
		  if ((element.id == 'pop1') && (element.style.display == "block")) {
				centerDialog(element.id);
			}
		} else {
		  alert('could get element for '+expandCollapse.arguments[i]);
		}
	}
}

// -- PURPOSE: to expand an item and collapse many others
// -- Usage: expandFirst(item to expand, list of items to collapse)
function expandFirst() {
	expandAll(expandFirst.arguments[0]);
	for (var i=1; i<expandFirst.arguments.length; i++) {
		collapseAll(expandFirst.arguments[i]);
	}
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// -- PURPOSE: to open a new window or focus on it if it is opened
// -- Usage: openWindow(url to open, name of window object, width of window - number of pixels or "max" for maximum available, height of window - number of pixels or "max" for maximum available)
var myWindows = new Array();
function openWindow(windowURL, windowName, width, height, startX, startY) {
	var change = false;
	if (myWindows[windowName]){
		if (myWindows[windowName].closed) {
			change = true;
		}
		else if (myWindows[windowName].document.location.href.indexOf(windowURL) == -1) {
			change = true;
		}
	}
	else {
		change = true;
	}

	if (change) {
		if (width == "max") {
			width = screen.availWidth;
		}
		if (height == "max") {
			height = screen.availHeight - 50;
		}
		if (!startX) {
			startX = 0;
		}
		if (!startY) {
			startY = 0;
		}
		var args = "scrollbars=yes,toolbar=no,directories=no,menubar=no,resizable=yes,status=yes,width=" + width + ",height=" + height + ",top=" + startY + ",left=" + startX;
		myWindows[windowName] = window.open(windowURL, windowName, args);
	}
	myWindows[windowName].focus();
}

function setiframeContent(iframe,filename) {	
	var element = document.getElementById(iframe);
	if (element) {
		element.src = filename;
	}
}

//Disable Functions
function init() {
	if (!document.layers) return;
	var box = document.forms[0].elements;
	for (var i=0;i<box.length;i++)
	{
		box[i].disabled = false;
		document.getElementById("email").style.backgroundColor='#c00';
	}
}

function disableIt(obj) {
	obj.disabled = !(obj.disabled);
	var z = (obj.disabled) ? 'disabled' : 'enabled';
	document.getElementById("email").className='inputon';
}

function fadeOut(i,j,fadeObject) { 
	document[fadeObject].filters.alpha.opacity=i;
	i--;

	if (i>j) setTimeout("fadeOut("+i+","+j+",'"+fadeObject+"')", 0);
}

//Hide layer after
var timerID;
function HideTimedLayer(id) {
	clearTimeout(timerID);
	var e = document.getElementById(id);
	e.style.display = "none";
}

function timedLayer(id) {
	setTimeout("HideTimedLayer(\"" + id + "\")", 50000);
}

//this function is used to change the color style sheets on the
//iframes on the web editor page.
//it looks for any iFrame with name that starts with 'wes' and
//changes it's style sheet to what you pass in as 'title'
function setIframeActiveStyleSheet(title) {
	var f, element, elementname, i, a;
	for (f=0; (element = frames[f]); f++) {
		elementname = element.name.substr(0,3).toLowerCase();
		if (elementname == 'wes') {
			for (i=0; (a = element.document.getElementsByTagName("link")[i]); i++) {
				if (a.getAttribute("rel") &&
				a.getAttribute("rel").indexOf("style") != -1 &&
				a.getAttribute("title")) {
					a.disabled = true;
					if(a.getAttribute("title") == title) a.disabled = false;
				}
			}
		}
	}
}

function toggleStyleInIframe(title, on_or_off) {
	var i, f, a, element, elementname;
	for (f=0; (element = frames[f]); f++) {
		elementname = element.name.substr(0,3).toLowerCase();
		if (elementname == 'wes') {
			for (i=0; (a = element.document.getElementsByTagName("link")[i]); i++) {
				if(a.getAttribute("title") == title) {
					a.disabled = !on_or_off;
					break;
				}
			}
		}
	}
}

function toggleStyle(title, on_or_off) {
	var i, a, main;
	for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("title") == title) {
			a.disabled = !on_or_off;
			break;
		}
	}
}

// switch styles
function setActiveStyleSheet(title) {
	var i, a, main;
	for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if (a.getAttribute("rel") &&
		a.getAttribute("rel").indexOf("style") != -1 &&
		a.getAttribute("title")) {
			a.disabled = true;
			if(a.getAttribute("title") == title) a.disabled = false;
		}
	}
}

function getActiveStyleSheet() {
	var i, a;
	for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if (a.getAttribute("rel") &&
		a.getAttribute("rel").indexOf("style") != -1 &&
		a.getAttribute("title") &&
		!a.disabled
		) return a.getAttribute("title");
	}
	return null;
}

function getPreferredStyleSheet() {
	var i, a;
	for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if (a.getAttribute("rel") &&
		a.getAttribute("rel").indexOf("style") != -1 &&
		a.getAttribute("rel").indexOf("alt") == -1 &&
		a.getAttribute("title")
		) return a.getAttribute("title");
	}
	return null;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


//This function expects the name of an iframe to be passed in.
//It will find all mediaplayer objects and turn of autostart,
//It will also scale all the images.
//Ihis is used for the thumbnail site display in weditor and signup
function scaleMyImages(myname) {

	var scaleTo = 0.55;
	//var safari=(navigator.userAgent.indexOf('Safari')!=-1);
	
	var i,a,origWidth,origHeight;
	
	//check for any media player objects inside and turn AUTOSTART OFF
	for (i=0; a = frames[myname].document.getElementsByTagName('object')[i]; i++) {
		if (a.id.toUpperCase() == 'MEDIAPLAYER') {
			a.autoStart = false;
		}
	}
	
	
	//check for any images and scale them
	for (i=0; a = frames[myname].document.getElementsByTagName('img')[i]; i++) {
		
		//grab original height & width, allowing a style.height to override what's in the IMG tag
		origWidth = a.width;
		origHeight = a.height;

		if (parseInt(a.style.width) > origWidth) origWidth = parseInt(a.style.width);
		if (parseInt(a.style.height) > origHeight) origHeight = parseInt(a.style.width);
		
//if (a.id == 'personal_photo_small') {
//	alert(myname + '.' + a.id + ' origwidth:'+origWidth+' origHeight:'+origHeight);
//}
		//resize the image, using style instead of IMG attributes because style
		//attributes trump tab attributes
		//the > 1000 thing is a kludge to prevent different browsers
		//from resizing images that shouldn't be resized 
		//in a perfect world it would be > 0
		if ((origWidth*origHeight) > 1000) {
			a.style.width = (origWidth * scaleTo)+"px";
			a.style.height = (origHeight * scaleTo)+"px";
		}
	}
}



//this function is called for the pop1 div to center it in the screen. 
//it's called from expandcollapse when it senses 'pop1'
function centerDialog(divToCenter) {
  var st;
	var popup = document.getElementById(divToCenter);
	if (popup) {
		st = document.body.scrollTop;
		if (document.documentElement.scrollTop > st) {
	  	st = document.documentElement.scrollTop;
		}
		
		//for some reason, ie needs a bigger offset value than mozilla does
		if (navigator.appName == "Microsoft Internet Explorer"){
			st -= 100;
		} else {
			st -= 50;
		}
		popup.style.top = st + 'px';
		doscroll(); //doscroll is in top.php
	}
}

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// -- PURPOSE: to show all elements inline
// -- Usage: showInline(list of items to expand)
function showInline() {
	for (var i=0; i<showInline.arguments.length; i++) {
		var element = document.getElementById(showInline.arguments[i]);
		if (element) {
			element.style.display = "inline";
		}
	}
}

// -- PURPOSE: to expand an item and collapse many others
// -- Usage: showInlineFirst(item to expand, list of items to collapse)
function showInlineFirst() {
	showInline(showInlineFirst.arguments[0]);
	for (var i=1; i<showInlineFirst.arguments.length; i++) {
		collapseAll(showInlineFirst.arguments[i]);
	}
}

//for inline objects
function toggleInline() {
	for (var i=0; i<toggleInline.arguments.length; i++) {
		var element = document.getElementById(toggleInline.arguments[i]);
		element.style.display = (element.style.display == "none") ? "inline" : "none";
	}
}

function uncheckAll(myForm) {
	for (var i=0;i<myForm.elements.length;i++) {
		var myElement = myForm.elements[i];
		if (myElement.type == "checkbox") {
			myElement.checked = false;
		}
	}
}

//for Yahoo Maps pop-ups
function popUpMap(addr, city, state, country) {
	var url = "http://maps.yahoo.com/py/maps.py?BFCat=&Pyt=Tmap&newFL=Use+Address+Below&addr=" + escape(addr) + "&csz=" + escape(city)  + "%2C+" + escape(state) + "&Country=" + escape(country) + "&Get%C2%A0Map=Get+Map"
	var features = "status=1,scrollbars=1,location=1,toolbar=1,width=800,height=550,left=50,top=30,resizable=1";
	openWindow(url, "yahoo_map", 800, 550, 20, 20);
}
