// CONSTANTS 
var LOADER_URL = "index.cfm?action=chat.showLoader";
var CHAT_URL = "index.cfm?action=chat.loadChatFrameset"

// global vars
var up_chat_window = null; 			// the reference to the actual popup window
var up_chat_window_name = null;		// the (string) name of the popup window

var refreshPassword = true;

var initMedallion = function() {
	// check the form 
	// if the user is logged in - we don't need to clear username
	var oForm = document.getElementById("up_launchChatForm");
	var isLoggedIn = document.getElementById("up_isLoggedIn").value;
	isLoggedIn = parseInt(isLoggedIn);
	
	if (isNaN(isLoggedIn)) {
		isLoggedIn = 0;
	}
	
	if (! isLoggedIn) {
		// set an event onChange to clear the password field...
		Event.observe('up_username', 'change', setRefreshPassword);
		Event.observe('up_password', 'focus', clearPassword);	
	}
	
	// register onclick events to elements on the page
	if (document.getElementById('signup_link')) {
		Event.observe('signup_link', 'click', createUserProfile);
	}
}
var clearPassword = function() {
	if (refreshPassword) {
		document.getElementById( 'up_password' ).value = '';
		refreshPassword = false;
	}	
}

var setRefreshPassword = function() {
	refreshPassword = false; 
}
	
var eraseValue = function(field) {
	if ( field.value == 'Screen Name') {
		field.value = '';
	}
	if ( field.value == 'Password') {
		field.value = '';
	}
}

var toggleUID = function() {
	var checkbox = document.getElementById( 'cb' );

	if ( checkbox.checked ) {
		checkbox.checked = false;
		toggleLabel( checkbox );
	}
	else {
		checkbox.checked = true;
		toggleLabel( checkbox );	
	}
}

var	toggleUID2 = function() {
	var checkbox = document.getElementById( 'cb' );

	checkbox.checked = true;
	//toggleLabel( checkbox );
}

var toggleLabel = function(checkbox) {
	var oUsername = document.getElementById( 'up_username' )
	// var username = .value;
	
	if ( checkbox.checked ) {
		if ( oUsername.value == '' ) {
			oUsername.value = 'Screen Name';
		}
		document.getElementById( 'content-tos' ).style.display   = 'none';
		document.getElementById( 'password-toggle' ).style.display  = 'block';	
		
		// turn the logintype to userplaneid
		document.getElementById('up_loginType').value = 'userplaneid';
	}
	else {
		if ( oUsername.value == '' ) {
			oUsername.value = 'Screen Name';
		}
		document.getElementById( 'content-tos' ).style.display   = 'block';
		document.getElementById( 'password-toggle' ).style.display  = 'none';
		
		// turn the logintype to userplaneid
		document.getElementById('up_loginType').value = 'guest';
		
		clearPassword();
	}
}

var createUserProfile = function(e) {
	var username = cleanupUsername( document.getElementById( 'up_username' ).value );
	var dc = document.getElementById('up_domainCode').value;
	createProfile = window.open( rootPath + 'index.cfm?action=user.createUser&DomainCode=' + dc + '&fromMedallion=1&userHandle='+username, 'createprofile', 'height=600,width=450,toolbar=0,directories=0,menubar=0,status=0,location=0,scrollbars=1,resizable=1' );
	createProfile.focus();
}

var popTOS = function() {
	void( window.open( rootPath + 'index.cfm?action=domain.viewTOS', 'Terms', 'width=400,height=500,toolbar=0,directories=0,menubar=0,status=0,location=no,scrollbars=1,resizable=1,' ) );
}


var cleanupUsername = function(strIn) {
	var strOut = "";
	for( var i = 0 ; i < strIn.length ; i++ ){
		var cChar = strIn.charAt(i);
		if( ( cChar >= 'A' && cChar <= 'Z' )
			|| ( cChar >= 'a' && cChar <= 'z' )
			|| ( cChar >= '0' && cChar <= '9' )
			|| cChar == ' '
			|| cChar == '@'
			|| cChar == '_'
			|| cChar == '-'
			|| cChar == '.' )
		{
			strOut += cChar;
		}
	}

	return strOut;
}



/* 
 * BEGIN NEW AJAX FUNCTIONS 
 */

// 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;
	}
	
	chat_window_name = domainCode + "_" + app;
	
	/*
	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;
}

// launches the popup window with a loading icon.
// we have to do this to avoid popup blockers.
// will fire off the validateCreds function
var loadChat = function(oForm) {
	var features = "";
	var qs = "";
	var w = null;
	var h = null;
	
	var app = null;
	var domainCode = null;
	var adultStats = null;
	var restrictGuests = null;
	var initialRoom = null;
	var oUsername = document.getElementById("up_username");
	var oPassword = document.getElementById("up_password");
	var isLoggedIn = document.getElementById("up_isLoggedIn").value;
	
	isLoggedIn = parseInt(isLoggedIn);
	if (isNaN(isLoggedIn)) {
		isLoggedIn = 0;
	}
	
	
	// if app is not passed in AND there's a up_app value on the page (medallion)
	if (document.getElementById("up_app")) {	
		app = document.getElementById("up_app").value;
	}
	
	// if there's a domainCode value on the page (medallion)
	if (document.getElementById("up_strippedDomainCode")) {
		domainCode = document.getElementById("up_strippedDomainCode").value;
	}
	
	// if there's a adultStatus value on the page (medallion)
	if (document.getElementById("up_domainAdultStatus")) {
		adultStatus = document.getElementById("up_domainAdultStatus").value;
	}
	
	// if there's a up_restrictGuests value on the page (medallion)
	if (document.getElementById("up_restrictGuests")) {
		restrictGuests = document.getElementById("up_restrictGuests").value;
		if (isNaN(parseInt(restrictGuests))) {
			restrictGuests = 0;
		}
	}
	
	// if there's a up_initialRoom value on the page (medallion)
	if (document.getElementById("up_initialRoom")) {
		initialRoom = document.getElementById("up_initialRoom").value;
	}
	
	// cleanup the username
	oUsername.value = cleanupUsername(oUsername.value);
	if (! oUsername.value.length || oUsername.value == "") {
		alert("Trying to be sneaky? >:] Enter a screen name pl0x!");
		return false;
	}

	if (! oPassword.value.length || oPassword.value == "") {
		alert("Trying to be sneaky? >:] Enter a password pl0x!");
		return false;
	}
	
	if (! parseInt(isLoggedIn) ) {
		if (parseInt(restrictGuests) ) {
			if ( (!oUsername.value.length || !oPassword.value.length) ) {
				alert("!! This chat requires a valid ID.\nPlease login or create a user account to begin chatting!");
				return false;
			}
		}
	}
	
	up_chat_window_name = nameWindow(app,domainCode,adultStatus);
	
	/*
	if (oForm) {
		oForm.target = up_chat_window_name;	
	}
	* */
	
	if (app == "ul") {
		w = 220;
		h = 650;	
	}
	else {
		w = 738;
		h = 585;
	}
	
	features += "width=" + w + ",height=" + h + ",toolbar=0,directories=0,menubar=0 ,status=0,location=0,scrollbars=0,resizable=1,left=-1000,top=-1000";
	up_chat_window = window.open( "", up_chat_window_name, features );
 	up_chat_window.name = up_chat_window_name;
	up_chat_window.focus();
	oForm.submit();
	
	// now - run validate creds
	// var validated = validateCreds();
	//alert(validated);
	//if (!validated) {
	//	up_chat_window.close();
	//	return false;
	//}
	
	//validateLogin();
	
	return false;
}

var launchChat = function(app, initialRoom, domainCode) {
	console.log("launching chat window....");
	// open the chat window
	var features = "";
	var oForm = document.getElementById("up_launchChatForm");	
	var oApp = document.getElementById("up_app");				
	var oInitialRoom = document.getElementById("up_initialRoom");
	var oDomainCode = document.getElementById("up_companyID");
	
	var url = null;
	
	
	var qs = "&app=" + oApp.value + "&initialRoom=" + oInitialRoom.value + "&domainCode=" + oDomainCode.value;

	// if the window is avail - clear the contents of the window
	if (up_chat_window) {
		var o = up_chat_window.document.getElementsByTagName("body")[0];
		if (o) {
			var f = o.firstChild;
			if (f) {
				// remove anything in the document before we resize!
			 	o.removeChild(f);
			}
		}
		up_chat_window.location.href = "" ;
		// up_chat_window.resizeTo(w,h);
		up_chat_window.location.href = CHAT_URL + qs ;
	}
	else {
		nameWindow();
		
		features += "width=" + w + ",height=" + h + ",toolbar=0,directories=0,menubar=0,status=0,location=0,scrollbars=0,resizable=1";
		myWin = window.open( CHAT_URL + qs, up_chat_window_name, features );
	}
	
	/*
	if (oApp.value == "ul") {
		features += "width=220,height=650";
	}
	else {
		features += "width=738,height=585";
	}
	features += "toolbar=0,directories=0,menubar=0,status=0,location=0,scrollbars=0,resizable=1";
	
	url = 'blank.html';
	myWin = window.open( url, up_chat_window_name, features );
	*/
	
	return false;
}

var validateCreds = function()	{
	// pull the values being sent
	var oUsername = document.getElementById('up_username');
	// var screenname = document.getElementById('up_username').value;
	var oPassword = document.getElementById('up_password');
	//var oCompanyID = document.getElementById('up_companyID');
	var restrictGuests = parseInt(document.getElementById('up_restrictGuests').value);
	
	// make sure restrict guests is a number
	if (isNaN(restrictGuests)) {
		restrictGuests = 0;
	}
		
	// cleanup the username
	oUsername.value = cleanupUsername(oUsername.value);
	
	// if the cleaned username has no length.
	// I think they did it this way to not mess up the default "Screen Name" text...
	if (!oUsername.value.length || oUsername.value == "Screen Name") {
		// fire error
		alert("You must enter a valid screen name.\nOnly alpha-numeric characters are allowed.\nPlease re-enter your screen name and click Login.")
		return false;
	}
	
	// if this domain does not allow guests and the user did not enter a password
	if (restrictGuests && ( !oUsername.value.length || !oPassword.value.length)) {
		 // let the user know this is a login required chat.
		alert("This chat requires a login.\nPlease enter a screen name and password.\nIf you need an account, please click on the register button.");
		return false;
	}
	
	// launchChat();
	
	return true;
}


// // set an event onChange to clear the password field...
Event.observe(window, 'load', initMedallion);

 