// GLOBAL VARS
var USERPLANE_WIN_NAME = "userplane_directory";
var USERPLANE_JS_LOADED = true;
var CHAT_LOADER_URL = "index.cfm?action=chat.showLoader";
var CHAT_FRAMESET_URL = "index.cfm?action=chat.drawFrameset";
var isPop;

if (isPop == undefined) {
	isPop = 0;
}
// set the window name
if (!isPop && this.window.name != USERPLANE_WIN_NAME) {
	this.window.name = USERPLANE_WIN_NAME;
}

function EditUserProfile(personaID)
{
	var winWidth = ( navigator.userAgent.indexOf( 'Mac' ) != -1 ) ? 415 : 420;
	// historically this has been the link that was passed to open a edit window, seek historical reason
	//http://www.userplane.com/chatlite/run/edit_profile.cfm?&roomSelection=179001&admin=1&fromChat=1
	window.open( 'index.cfm?action=user.editUser&personaID=' + personaID, 'editprofile', 'height=600,width=' + winWidth + ',toolbar=0,directories=0,menubar=0,status=0,location=0,scrollbars=1,resizable=1' );
}

/* TRIMS WHITESPACE FROM STRING */
function trim(theString) {
	if (theString != undefined) {
		return theString.replace(/^\s*/,"").replace(/\s*$/,"")
	}
}



/* CHAR COUNT - To Count Chars in a text/textarea input */
function charCount(e) {
	// expects this.oCounter, this.oMax, and this.oInput
	// oInput is the text area object
	// oMax is the hidden form field object declaring the max
	// oCounter is the counter text input
	 
	var iMax = parseInt(this.oMax.value);
	var iLeft = parseInt(iMax - this.oInput.value.length);

	if (!this.oInput || isNaN(iLeft))  {
		return;
	}

	if (iLeft <= 0) {
		alert("You have exceeded the maximum number of characters for this field.  Please try again.")
		this.oInput.value = this.oInput.value.substr(0, (this.oInput.value.length - 1) );
	}

	this.oCounter.value = iLeft;
}

function createUserProfile( domainID )
		{
		if ( navigator.userAgent.indexOf( 'Mac' ) != -1 )
			{
			window.open( 'index.cfm?action=user.createUser&DomainID=' + domainID, 'createprofile', 'height=600,width=440,toolbar=0,directories=0,menubar=0,status=0,location=0,scrollbars=1,resizable=1' );
			}
		else
			{
				window.open( 'index.cfm?action=user.createUser&DomainID=' + domainID, 'createprofile', 'height=600,width=444,toolbar=0,directories=0,menubar=0,status=0,location=0,scrollbars=1,resizable=1' );
			}
		}

function checkForm( myForm )
{
	//var valid_CC = true;

	//if ( typeof( myForm.Payment[0] ) != 'undefined' )
	//{
	//	if ( myForm.Payment[0].checked )
	//	{
	//		valid_CC = CheckCCNum( document.myForm );
	//	}
	//}

	//if ( valid_CC )
	//{
		if ( !myForm.agree2Terms.checked )
		{
			alert( "You must agree to our Terms of Service." );
			return false;
		}
		//else
	//	{
		//	return true;
		//}
	//}
	//else
	//{
	//	return false;
	//}
}

function isNonUS( domainCountry )
{
	var us_required = document.getElementById( 'USreq' );
	( domainCountry != 219 ) ? us_required.style.display = 'none' : us_required.style.display = 'block';
}

	

		function clearField( text )
		{
			var labelArr = [ 'Zip Code', 'City', 'Province' ];

			for ( i = 0 ; i < labelArr.length ; i++ )
			{
				if ( text.value == labelArr[ i ] )
				{
					text.value = '';
				}
			}
		}

		function countChars( desc )
		{
			var count = desc.length;
			var oContainer = document.getElementById('charCount');
			
			
			oContainer.innerHTML = count + '/500 maximum characters';
			if (count > 500) {
				oContainer.addClassName("error");
				alert("You have exceeded the maximum number of characters for this field.  Please try again.")
				var oTA = document.getElementById("personaDescription");
				// alert(oTA);
				
				oTA.value = oTA.value.substr(0, 500 );
				var count = oTA.value.length;
			
				oContainer.innerHTML = count + '/500 maximum characters';
			
			}
		}

		function showCool( cb )
		{
			( cb ) ? document.getElementById( 'cool' ).style.display = 'inline' : document.getElementById( 'cool' ).style.display = 'none';
		}

	// If user is editing profile from admin, refresh the page
		function refresh_admin()
		{
			if ( window.location.href.indexOf( "admin=1" ) != -1 )
			{
				if ( opener.location != null )
				{
						opener.location = opener.location;
				}
			}
		}

function regexReplace(in_string, re, replacement) {
  var re = new RegExp(re, "g");
  var result = in_string.replace(re, replacement);
  return result;
}


// names the popup window we will be using.
// if the window is already named - we will just return out.
// sets the var that is accessable everywhere - does not return anything.
var nameWindow = function(app,domainCode,adultStatus) {
	// var oStrippedDomainCode = document.getElementById("up_strippedDomainCode");
	//var adultStatus = parseInt(document.getElementById("up_domainAdultStatus").value);
	// var oApp = document.getElementById("up_app");
	var chat_window_name = "";

	if(isNaN(adultStatus)) {
		adultStatus = 0;
	}
	
	
	// name the window
	chat_window_name = domainCode + "_" + app;
	// regex out bad chars
	chat_window_name = regexReplace(chat_window_name, '[^a-zA-Z0-9_]', '');
	
	/*
	if (adultStatus) {
		chat_window_name = domainCode;
	}
	else {
		chat_window_name = "Userplane_Chatlite_" + domainCode;
	}

	if (app.value == "ul") {
		chat_window_name += "_ul";
	}
	*/

	return chat_window_name;
}

// used to load the chat from within the site.
// in order to load a chat from the site, the following parameters are required:
// app (required)
// domainCode (required)
// adultStatus (required)
// restrictGuests (optional - default false)
// intialRoom (optional)



var launchChat = function(app, domainCode, adultStatus, restrictGuests, initialRoom, isLoggedIn, windowTitle, adBanner) {
	var features = "";
	var qs = "";
	var w = null;
	var h = null;
	var up_chat_window_name = null;
	
	// required args:
	if (!app) {
		alert("A valid application was not passed!\nChat cannot launch.\nIf this issue persists, please contact support@userplane.com.");
		return;
	}

	if (!domainCode) {
		alert("A valid domain code was not passed!\nChat cannot launch.\nIf this issue persists, please contact support@userplane.com.");
		return;
	}
	
	/*if (!isLoggedIn) {
		alert("You must first login or choose a guest screen name.");
		return;
	}*/

	adultStatus = parseInt(adultStatus);
	if (isNaN(adultStatus)) {
		alert("A valid adult status was not passed for this domain!\nChat cannot launch.\nIf this issue persists, please contact support@userplane.com.");
		return;
	}

	// default args
	if (isNaN(restrictGuests)) {
		restrictGuests = 0;
	}

	// get the window name
	up_chat_window_name = nameWindow(app,domainCode,adultStatus);

	if (app == "ul") {
		w = 220;
		h = 650;
	}
	else {
		w = 728;
		h = 600;
	}

	features += "width=" + w + ",height=" + h + ",toolbar=0,directories=0,menubar=0 ,status=0,location=0,scrollbars=0,resizable=1,left=0,top=0";
	qs = "app=" + app + "&domainCode=" + domainCode + "&initialRoom=" + initialRoom + "&restrictGuests=" + restrictGuests + "&windowTitle=" + windowTitle + "&adbanner=" + adBanner;
	var url = CHAT_LOADER_URL + "&" + qs;
	
 	up_chat_window = window.open( url, up_chat_window_name, features );
 	up_chat_window.name = up_chat_window_name;
 	up_chat_window.focus();

	return false;
}

// Toggle-selects 'ALL' or 'None' of the checkboxes in the Users List
function toggle_select_all( state, check_name )
{
	var check_state = (state == "ALL") ? true : false;
	var checkbox    = eval("document.user." + check_name);
	if (  checkbox.length  == undefined ){
		checkbox.checked = check_state;
	}
	else {
		for (i = 0 ; i < checkbox.length ; i++)
		{
			checkbox[i].checked = check_state;
		}
	}
}

function go()
{
	box = document.forms[0].navi;
	destination = box.options[box.selectedIndex].value;
	if (destination) location.href = destination;
}

function checkAuth(username,password,user_session_guid) {
	if ($('guestLogin').checked==false) {
		DWREngine._execute(_ajaxConfig._cfscriptLocation + "/users.cfc", null, 'loginUser', username, password, user_session_guid, checkAuthResult);	
	}
	else {
		if ($('loginUsername').value == 'Screen Name') {
			alert('Matt: Please insert error handler (error# 5609) here. I can\'t get it to work. For the rest of you: You can\'t login with the Screen Name, \'Screen Name\'');
		} else {
			var oForm = document.getElementById('loginForm');
			oForm.action = 'index.cfm?action=security.processLogin';
			oForm.submit();	
		}
		
	}
}

function checkAuthResult(r) {
	$('nav-login-error').innerHTML='';
	if (r == 0) {
		$('loginPassword').value = '';
		var o = {messageCode:5608};
		appendMessage('nav-login-error',o);
	}
	else {
		var oForm = document.getElementById('loginForm');
		oForm.action = 'index.cfm?action=security.processLogin';
		oForm.submit();	
	}
}

function popForgetPassword(){
	var w = ( navigator.userAgent.indexOf( 'Mac' ) != -1 ) ? 415 : 420;
	var h = 300;
	// historically this has been the link that was passed to open a edit window, seek historical reason
	//http://www.userplane.com/chatlite/run/edit_profile.cfm?&roomSelection=179001&admin=1&fromChat=1
	window.open( 'index.cfm?action=user.forgotPassword', 'forgetPassword', 'height=' + h + ',width=' + w + ',toolbar=0,directories=0,menubar=0,status=0,location=0,scrollbars=1,resizable=1' );
}


function loadCommunity (htmlPath, fileSelf) {
	self.location= htmlPath + fileSelf + 'domain.profiles&domain_id=' + $('communityName').value;
}

