
var IE4 = ( document.all && !document.getElementById ) ? true : false;
var NS4 = ( document.layers ) ? true : false;
var IE5 = ( document.all && document.getElementById ) ? true : false;
var N6 = ( document.getElementById && !document.all ) ? true : false;


var domainName = "panachallenge.com";	// Your site's domain.
var websiteName = "Panachallenge";		// Your site's name.
var webroot = "";	// If your site is at the "public_html" level, set this to "" (empty string).

var anyChanged = false;
var aboutWin = null;
var disclaimWin = null;
var memberWin = null;
var rankWin = null;
var ssWin = null;
var testWin = null;
var tourWin = null;
var voteWin = null;


function AdminCommentDelete( cmtid ) {

	var url = webroot + "/admin/admin_comment_delete.php?id=" + escape( cmtid );
	var cmtWin = open( url, "commentWindow", "width=700,height=800,resizable=1,scrollbars=1" );
	cmtWin.focus();
}


function AdminCommentEdit( cmtid ) {

	var url = webroot + "/admin/admin_comment_edit.php?id=" + escape( cmtid );
	var cmtWin = open( url, "commentWindow", "width=700,height=800,resizable=1,scrollbars=1" );
	cmtWin.focus();
}


function AdminUserInfo( userid ) {

	var url = webroot + "/admin/user_info.php?id=" + escape( userid );
	var userWin = open( url, "userWindow", "width=900,height=800,resizable=1,scrollbars=1" );
	userWin.focus();
}


function CheckAnyChanged() {
	if ( ! anyChanged ) {
		alert( "Nothing has changed" );
		return false;
	}

	else return true;
}


function DeleteCookies() {
	deleteCookie( "userPasswd", "/", "www." + domainName );
	deleteCookie( "userName", "/", "www." + domainName );

	deleteCookie( "userPasswd", "/", domainName );
	deleteCookie( "userName", "/", domainName );

	deleteCookie( "userPasswd", "/" );
	deleteCookie( "userName", "/" );

	deleteCookie( "topic_visit", "/" );
	deleteCookie( "topic_id", "/" );
}


function Disclaimer() {

	if ( ! disclaimWin || disclaimWin.closed )
		disclaimWin = open( webroot + "/disclaimer.htm", "disclaimWindow", "width=600,height=700,resizable=1,scrollbars=1" );

	disclaimWin.focus();
}


function FixUrl( txtField ) {

	if ( txtField.value.length > 0 && txtField.value.substr( 0, 4 ) != "http" )
		txtField.value = "http://" + txtField.value;
}


function GetPasswd() {
	return getCookie( "userPasswd" );
}


function GetUser() {
	return getCookie( "userName" );
}


function HelpFavs() {
	alert( "Before you can record a vote, you must select Favorites. When viewing the entries, you'll" +
	       " see a button that will add an entry to your Favorites, or remove an entry from your" +
	       " Favorites.  Click the 'Favorites' button to arrange them in order.\n\n" +
	       "Once you have ten or more Favorites selected, you can record your vote" +
	       " (multiple times, if necessary)" );
}


function HelpInactiveSubmit() {
	alert( "Your account is currently inactive, so you cannot upload an entry.  Update your profile" +
	       " to reactivate your account." );
}


function HelpPastChlgFast() {
	alert( "Thumbnails of winning entries are shown with past Challenges because you selected" +
	       " a fast connection in your profile.  To hide the thumbnails, update your profile and" +
	       " uncheck the fast connection box." );
}


function HelpPastChlgSlow() {
	alert( "To display thumbnails of winning entries with past Challenges, update your profile and" +
	       " check the fast connection box." );
}


function HelpStatsMinChlg() {
	alert( "The minimum number of Challenges (in the selected category) that a member must have" +
	       " entered in order to be included in the stats." );
}


function HelpStatsMinChlgUser() {
	alert( "The minimum number of Challenges that a member must have" +
	       " entered (in the selected category) in order to be included in the stats.\n\n" +
	       "Choose 'Mine' to compile stats using ONLY the challenges you have entered." );
}


function HelpVote() {
	alert( "To vote, view the entries and select ten or more Favorites. Click the 'Favorites' button" +
	       " to arrange them in top-ten order, then click the 'Record Vote' button.\n\n" +
	       "You may record your vote any number of times by adding or removing favorites, or rearranging" +
	       " your favorites, then clicking the 'Record Vote' button again." );
}


function InactiveMsg() {
	alert( "Your membership is inactive!\nThis caused by bounced email from your primary address or not visiting the web site for a long time.\n" +
	       "Update your profile to re-activate your membership." );
}


function ItemChanged( obj ) {
	obj.style.background = '#FFFF66';
	anyChanged = true;
}


function MemberEmail( memberid ) {
	var tmpwin = open( webroot + "/member_email.php?member=" + escape( memberid ), "tmpwindow", "width=500,height=100" );
	tmpwin.focus();
}


function PageLoad() {
	if ( document.passwdForm ) document.passwdForm.member.focus();
	else if ( document.loginForm ) {
		if ( document.loginForm.username.value.length == 0 ) document.loginForm.username.focus();
		else document.loginForm.passwd.focus();
	}
}


function PreviewHTML( txtField ) {

	if ( ! txtField || ! txtField.value || txtField.value.length == 0 ) {
		alert( "Nothing to preview!" );
		return;
	}

	if ( ! testWin || testWin.closed )
		testWin = open( null, "testWindow", "width=500,height=600,scrollbars=1,resizable=1" );

	var newVal = txtField.value;
	while ( newVal.indexOf( "\r\n" ) >= 0 )
		newVal = newVal.replace( "\r\n", "<br>" );

	var d = testWin.document;
	d.open();
	d.writeln( "<html><head><title>" + websiteName + " HTML Preview</title>\n" +
	           "<link rel='stylesheet' type='text/css' href='" + webroot + "/styles.css'></head>\n" +
	           "<body bgcolor='#COCOCO' text='#303030'>" );

	d.writeln( "<p class='small'>" + newVal + "</p>" );

	d.writeln( "<hr size='1' noshade><center><form><input type='button' class='buttonBold' value='Close' onClick='self.close()'></form>" );
	d.close();

	testWin.focus();
}


function SelAll( frmName, selName, selected ) {

	var sel = eval( "document." + frmName + "." + selName );
	for ( var i = 0 ; i < sel.length ; i++ )
		sel[i].selected = selected;
}


function ShowMember( imgsrc, pagetitle, imgx, imgy ) {

	var winx = imgx + 20;
	var winy = imgy + 75;

	if ( memberWin && ! memberWin.closed ) memberWin.close();
	memberWin = open( webroot + "/blank.htm", "memberWindow", "width=" + winx + ",height=" + winy + ",resizable=1" );

	var d = memberWin.document;
	d.open();
	d.writeln( "<html><head><title>" + pagetitle + "</title>\n" +
	           "<link rel='stylesheet' type='text/css' href='" + webroot + "/styles.css'></head>\n" +
	           "<body bgcolor='#DFDFDF'><center>" );

	d.writeln( "<img src='" + imgsrc + "' width='" + imgx + "' height='" + imgy + "' border='0'>" );

	d.writeln( "<form><input type='button' class='buttonTiny' value='Close' onClick='if ( opener ) opener.focus(); self.close()'></form>" );
	d.writeln( "</body>\n</html>" );
	d.close();

	memberWin.focus();
}


function ShowUserVote( userid, chlgid ) {

	var url = webroot + "/challenge/show_vote.php?member=" + escape( userid ) + "&challenge=" + escape( chlgid );
	if ( ! voteWin || voteWin.closed ) voteWin = open( url, "voteWindow", "width=600,height=750,scrollbars=1,resizable=1" );
	else voteWin.location.href = url;

	voteWin.focus();
}


function Slideshow( chlg, favs, sortby, srch, cam, cat ) {

	var url = webroot + "/challenge/entry_slideshow.php?challenge=" + ( chlg ? escape( chlg ) : "" ) +
	          "&favs=" + ( favs ? "y" : "" ) +
	          "&sortby=" + ( sortby ? escape( sortby ) : "" ) +
	          "&srch=" + ( srch ? escape( srch ) : "" ) + "&interval=5&cam=" + cam + "&cat=" + cat;
	if ( ! ssWin || ssWin.closed )
		ssWin = open( url, "ssWindow", "width=800,height=800,resizable=1,scrollbars=1" );
	else ssWin.location.href = url;

	ssWin.focus();
}


function TestUrl( txtField ) {

	if ( txtField.value.length == 0 ) {
		alert( "Enter a URL to test!" );
		txtField.focus();
	}

	else {
		FixUrl( txtField );
		if ( ! testWin || testWin.closed ) testWin = open( txtField.value, "testWindow", "" );
		else testWin.location.href = txtField.value;
		testWin.focus();
	}
}


function Tour() {

	if ( ! tourWin || tourWin.closed )
		tourWin = open( webroot + "/tour/index.php", "tourWindow", "width=800,height=600,scrollbars=1,resizable=1" );
	tourWin.focus();
}


function UserLogIn( username, passwd, email, remember ) {

	if ( remember ) expires = expDate;
	else expires = null;

	setCookie( "userName", username, expires, "/", "www." + domainName );
	setCookie( "userPasswd", passwd, expires, "/", "www." + domainName );

	setCookie( "userName", username, expires, "/", domainName );
	setCookie( "userPasswd", passwd, expires, "/", domainName );
}


function UserLogOff( askUser ) {
	if ( ! askUser || ( GetUser() && confirm( "Log off as " + GetUser() + "?" ))) {
		DeleteCookies();
		self.location.replace( webroot + "/index.php" );
	}
}


// Cookie functions.
function setCookie(name, value, expires, path, domain, secure) {

	var curCookie = name + "=" + escape(value) +
	                ((expires) ? "; expires=" + expires.toGMTString() : "") +
	                ((path) ? "; path=" + path : "") +
	                ((domain) ? "; domain=" + domain : "") +
	                ((secure) ? "; secure" : "");
	document.cookie = curCookie;

}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
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));

}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {

	if (getCookie(name)) {
		document.cookie = name + "=" +
		                  ((path) ? "; path=" + path : "") +
		                  ((domain) ? "; domain=" + domain : "") +
		                  "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}

}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {

	var base = new Date(0);
	var skew = base.getTime();
	if (skew > 0) date.setTime(date.getTime() - skew);

}

// Cookie expiration date: +6 months.
var expDate = new Date();
expDate.setTime( expDate.getTime() + ( 180 * 24 * 60 * 60 * 1000 ));
