/**** $Id: login.js,v 1.4 2008/06/30 16:09:07 rhorn Exp $ ****/

function hideSubmitButton()
{
	document.getElementById('login_button').style.display = 'none';
	document.getElementById('login_processing').style.display = 'block';
	return true;
}

function showSubmitButton()
{
	document.getElementById('login_button').style.display = 'block';
	document.getElementById('login_processing').style.display = 'none';
	return true;
}

function login_xml_response_handler(req)
{
	// split result by |
	var result = req.responseText.split('|');

	if (result[0] == 'OK')
	{
		// result was ok
		// result[1] has bounceto value
		try
		{
			if (result[1].length > 0 && result[1].match(/^javascript:/))
			{
				var js_to_eval = result[1].replace(/^javascript:/, '');
				eval(js_to_eval);
			}
			else if (result[1].length > 0 && result[1].indexOf('undefined') == -1)
			{
				window.location.href = result[1];
			}
			else
			{
				window.location.reload();
			}
		}
		catch (e)
		{
			window.location.reload();
		}
		//hide_login();
	}
	else
	{
		showSubmitButton();
		// invalid user or password
		document.getElementById('login_error').innerHTML = result[1];
		document.getElementById('login_error').style.display = 'block';
	}
}

function logout_xml_response_handler(req)
{
	// split result by |
	var result = req.responseText.split('|');

	if (result[0] == 'OK')
	{
		try
		{
			window.location.reload();
		}
		catch (e)
		{
			window.location.href = result[1];
		}
	}
}

function logout_send_xml_http_request()
{
	var url = Syn.Config.PortalRoot + 'files/charter/ajaxapi/login.php';
	params = 'action=logout';

	new SynAjax().request(url,
	{
		method: 'post',
		parameters: params,
		onComplete: logout_xml_response_handler
	}
	);
	return false;
}

function login_send_xml_http_request(username, passwd, bounceto, remember_me)
{
	document.getElementById('login_error').style.display = 'none';
	var url = Syn.Config.PortalRoot + 'files/charter/ajaxapi/login.php'; 
	var params  = 'username=' + encodeURIComponent(username);
	params += '&passwd=' + encodeURIComponent(passwd);
	params += '&bounceto=' + encodeURIComponent(bounceto);
	params += '&remember_me=' + encodeURIComponent(remember_me);
	params += '&action=login';
	new SynAjax().request(url,
	{
		method: 'post',
		parameters: params,
		onComplete: login_xml_response_handler
	}
	);
}

function viewport_width()
{
	return self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
}

function viewport_height()
{
	return self.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);
}

function scrollsize_height()
{
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if (test1 > test2)
	{
		return document.body.scrollHeight;
	}
	else
	{
		return document.body.offsetHeight;
	}
}

function scrollsize_width()
{
	return self.scrollWidth || (document.documentElement.scrollWidth || document.body.scrollWidth);
}

function my_scroll_top()
{
	return self.scrollTop || (document.documentElement.scrollTop || document.body.scrollTop);
}

function viewport_size()
{
	return { width: viewport_width(), height: viewport_height() };
}

function hide_adwrapper()
{
	var hadwrapper = document.getElementById('hide_adwrapper');
	if (hadwrapper)
	{
		if (hadwrapper.style.visibility = 'visible')
		{
			hadwrapper.style.visibility = 'hidden';
		}
	}
}

function show_adwrapper()
{
	var hadwrapper = document.getElementById('hide_adwrapper');
	if (hadwrapper)
	{ 
		if (hadwrapper.style.visibility = 'hidden')
		{
			hadwrapper.style.visibility = 'visible';
		}
	}
}

function hide_naughty_wmps()
{
	var wmps = new Array('wmp_vid_player', 'fl_vid_player');
	for (var i = 0; i < wmps.length; i++)
	{
		var owmps = document.getElementById(wmps[i]);
		if (owmps)
		{
			owmps.style.visibility='hidden';
		}
	}		
}

function reveal_naughty_wmps()
{
	var wmps = new Array('wmp_vid_player', 'fl_vid_player');
	for (var i = 0; i < wmps.length; i++)
	{
		var owmps = document.getElementById(wmps[i]);
		if (owmps)
		{
			owmps.style.visibility='visible';
		}
	}	
}

function hide_naughty_iframes()
{
	var ad_frames = document.getElementsByTagName('iframe');
	for (var h = 0; h < ad_frames.length; h++)
	{
		var f = ad_frames[h];
		if (f.style.visibility != 'hidden')
		{
			f.setAttribute('forced_hidden', 'true');
			f.style.visibility = 'hidden';
		}
	}
}

function reveal_naughty_iframes()
{
	var ad_frames = document.getElementsByTagName('iframe');
	for (var h = 0; h < ad_frames.length; h++)
	{
		var f = ad_frames[h];
		if (f.getAttribute('forced_hidden') == 'true')
		{
			f.style.visibility = 'visible';
			f.removeAttribute('forced_hidden');
		}
	}
}

function hide_naughty_tags()
{
	var types = ['embed'];
	for (var i=0;i<types.length;i++)
	{
		var tags = document.getElementsByTagName(types[i]);
		for (var j=0;j<tags.length;j++)
		{
			var tag = tags[j];
			if (tag.style.visibility = 'visible')
			{
				tag.style.visibility = 'hidden';
				tag.setAttribute('forced_hidden',true);
			} 
		}
	}
}

function reveal_naughty_tags()
{
	var types = ['embed'];

	for (var i=0;i<types.length;i++)
	{
		var tags = document.getElementsByTagName(types[i]);
		for (var j=0;j<tags.length;j++)
		{
			var tag = tags[j];
			if (tag.getAttribute('forced_hidden'))
			{
				tag.style.visibility = 'visible';
				tag.setAttribute('forced_hidden',false);
			} 
		}
	}
}

function hide_login()
{
	var loginCont = document.getElementById('login_cont');
	var modalCover = document.getElementById('modal_cover');
	reveal_naughty_iframes();
	reveal_naughty_wmps();
	reveal_naughty_tags();
	show_adwrapper();
	loginCont.style.display  = 'none';
	modalCover.style.display = 'none';

	// for IE 6, we hide the dropdowns
	if (navigator.userAgent.indexOf("MSIE 6") > 0)
	{
		login_show_dropdowns();
	}
}

function show_login()
{
	//var vW = 2500;
	var vW = viewport_width();
	var vH = viewport_height();
	var vS = viewport_size();
	var sH = scrollsize_height();
	var sW = scrollsize_width();
	var sT = my_scroll_top();
	var loginCont = document.getElementById('login_cont');
	var modalCover = document.getElementById('modal_cover');
	var closeLogin = document.getElementById('close_login');
	closeLogin.onclick = hide_login;
	window.onscroll = window.onresize = function (e)
	{
		var Client =
		{
			myScrollTop: function()
			{
				return self.scrollTop || (document.documentElement.scrollTop || document.body.scrollTop);
			}
		};
		var sT = Client.myScrollTop();
		loginCont.style.top = ((vH - 295)/2) + sT + 'px';
	};

	hide_naughty_iframes();
	hide_naughty_wmps();
	hide_naughty_tags();
	hide_adwrapper();

	// for IE 6, we hide the dropdowns.
	if (navigator.userAgent.indexOf("MSIE 6") > 0)
	{
		login_hide_dropdowns();
	}

	document.getElementById('login_error').innerHTML = '';
	document.getElementById('login_error').style.display = 'block';
	loginCont.style.width = '600px';
	loginCont.style.height = '295px';
	loginCont.style.clear = 'all';
	loginCont.style.left = (vW - 600) / 2 + 'px';
	loginCont.style.top = (vH - 295) / 2 + 'px';
	loginCont.style.display = 'block';

	if (sH < vH)
	{
		modalCover.style.height = vH + 'px';
	}
	else
	{
		modalCover.style.height = sH + 'px';
	}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize()
{
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY)
	{	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{
		// all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else 
	{ 
		// Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) 
	{	
		// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) 
	{ 
		// Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} 
	else if (document.body) 
	{ 
		// other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if (yScroll < windowHeight)
	{
		pageHeight = windowHeight;
	} 
	else 
	{ 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if (xScroll < windowWidth)
	{	
		pageWidth = windowWidth;
	} 
	else 
	{
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// -----------------------------------------------------------------------------------

var arrayPageSize = getPageSize();
//modalCover.setWidth('modal_cover', arrayPageSize[0]);
//modalCover.style.width = 100 + '%';
//modalCover.style.width = 'auto';
		
if (navigator.userAgent.indexOf("MSIE 6") > 0)
{
	modalCover.style.width = arrayPageSize[0] + 'px';
	//modalCover.style.width = '100%';
}
else
{
	modalCover.style.width = 100 + '%';
	modalCover.style.minWidth = 968 + 'px';
}

modalCover.style.position = 'absolute';
modalCover.style.top = '0px';
modalCover.style.left = '0px';
modalCover.style.display = 'block';
modalCover.style.overflow = 'hidden';
loginCont.style.display = 'block';
//Element.toggle(modalCover);
//Element.toggle(loginCont);

document.getElementById('__syn_login_username').focus();
}

function login_hide_dropdowns()
{
	var elements = document.getElementsByTagName('select');
	var i = 0;
	for (i = 0; i < elements.length; i++)
	{
		if (elements[i].type == 'select-one')
		{
			elements[i].style.display = 'none';
		}
	}
	if (typeof window.pldv_hide_overlay == 'function')
	{
		pldv_hide_overlay()
	};
}

function login_show_dropdowns()
{
	var elements = document.getElementsByTagName('select');
	var i = 0;
	for (i = 0; i < elements.length; i++)
	{
		if (elements[i].type == 'select-one')
		{
			elements[i].style.display = '';
		}
	}
	if (typeof window.pldv_show_overlay == 'function')
	{
		pldv_show_overlay()
	};
}

function auto_login()
{
	if (!__LOGGED_IN__ && document.location.search.indexOf("message=secondary_login") != -1)
	{
		var login_form_div = document.getElementsByTagName('DIV');
		var len = login_form_div.length;
		while (len--)
		{
			if (login_form_div[len].className == 'login_form')
			{
				login_form_div = login_form_div[len];
				break;
			}
		}
		if (!login_form_div)
		{
			alert("Login Disabled, define class 'login_form'");
			return;
		}
		var myLoginForm = login_form_div.getElementsByTagName('form');
		if (!myLoginForm || myLoginForm.length == 0)
		{
			alert("Login Disabled, define element 'form' in class 'login_form'");
			return;
		}

		var regexS = "[\\?&]bounceto=([^&#]*)";
		var regex = new RegExp( regexS );
		var tmpURL = window.location.href;
		var results = regex.exec( tmpURL );
		var bounceto = window.location.href;

		if ( results != null )
		{
			bounceto = results[1]; // do not unescap
		}

		myLoginForm[0].action = bounceto;
		myLoginForm[0].onsubmit = _login_form_onsubmit;
		show_login();
	}
}

function _login_form_onsubmit()
{
	var username;
	var password;
	var remember_me = (document.getElementById('__syn_login_remeber_me').checked ? 'Y' : '');
	var inputs = this.getElementsByTagName('input');
	for (l = 0; l < inputs.length; l++)
	{
		if (inputs[l].className == 'username')
		{
			username = inputs[l].value;
		}
		if (inputs[l].className == 'passwd')
		{
			password = inputs[l].value;
		}
	}
	login_send_xml_http_request(username, password, this.action, remember_me);
	return false;
}

function _login_a_onclick()
{
	var login_form_div = document.getElementsByTagName('DIV');
	var len = login_form_div.length;
	while (len--)
	{
		if (login_form_div[len].className == 'login_form')
		{
			login_form_div = login_form_div[len];
			break;
		}
	}
	if (!login_form_div)
	{
		alert("Login Disabled, define class 'login_form'");
		return;
	}
	var myLoginForm = login_form_div.getElementsByTagName('form');
	if (!myLoginForm || myLoginForm.length == 0)
	{
		alert("Login Disabled, define element 'form' in class 'login_form'");
		return;
	}
	myLoginForm[0].action = this.href;
	myLoginForm[0].onsubmit = _login_form_onsubmit;
	show_login();
	return false;
}

function _login_catcher(e)
{
	var target = e.target || e.srcElement;
	var a_element = null;
	var l_element = null;
	var parent = target;

	for (var i=0; i < 6; i++)
	{
		if (!a_element && parent.tagName == "A")
		{
			a_element = parent;
			if (a_element.login_capture)
			{
				return;
			}
		}
		if (!l_element && parent.className == "site_login")
		{
			l_element = parent;
			break;
		}
		parent = parent.parentNode ? parent.parentNode : parent.parentElement;
		if (parent == null)
		{
			return;
		}
	}

	if (!a_element || !l_element)
	{
		return;
	}

	a_element.onclick = _login_a_onclick;
	a_element.login_capture = true;
	return true;
}

if (!__LOGGED_IN__)
{
	if (window.document && window.document.addEventListener)
	{
		window.document.addEventListener('mousedown', _login_catcher, false);
	}
	else if(window.document && window.document.attachEvent)
	{
		window.document.attachEvent('onmousedown', _login_catcher);
	}
}

function createRequestObject() 
{
	FORM_DATA = new Object();
	// The Object ("Array") where our data will be stored.
	separator = ',';
	// The token used to separate data from multi-select inputs
	query = '' + this.location;
	// Get the current URL so we can parse out the data.
	// Adding a null-string '' forces an implicit type cast
	// from property to string, for NS2 compatibility.
	query = query.substring((query.indexOf('?')) + 1);
	// Keep everything after the question mark '?'.

	if (query.length < 1) 
	{ 
		return false; 
	}  // Perhaps we got some bad data?

	keypairs = new Object();
	numKP = 1;
	// Local vars used to store and keep track of name/value pairs
	// as we parse them back into a usable form.

	while (query.indexOf('&') > -1) 
	{
		keypairs[numKP] = query.substring(0,query.indexOf('&'));
		query = query.substring((query.indexOf('&')) + 1);
		numKP++;
		// Split the query string at each '&', storing the left-hand side
		// of the split in a new keypairs[] holder, and chopping the query
		// so that it gets the value of the right-hand string.
	}

	keypairs[numKP] = query;
	// Store what's left in the query string as the final keypairs[] data.

	for (i in keypairs) 
	{
		keyName = keypairs[i].substring(0,keypairs[i].indexOf('='));
		// Left of '=' is name.
		keyValue = keypairs[i].substring((keypairs[i].indexOf('=')) + 1);
		// Right of '=' is value.
		while (keyValue.indexOf('+') > -1) 
		{
			keyValue = keyValue.substring(0,keyValue.indexOf('+')) + ' ' + keyValue.substring(keyValue.indexOf('+') + 1);
			// Replace each '+' in data string with a space.
		}

		keyValue = unescape(keyValue);
		// Unescape non-alphanumerics

		if (FORM_DATA[keyName]) 
		{
			FORM_DATA[keyName] = FORM_DATA[keyName] + separator + keyValue;
			// Object already exists, it is probably a multi-select input,
			// and we need to generate a separator-delimited string
			// by appending to what we already have stored.
		} 
		else 
		{
			FORM_DATA[keyName] = keyValue;
			// Normal case: name gets value.
		}
	}
	return FORM_DATA;
}
FORM_DATA = createRequestObject();

if (FORM_DATA['message'] == 'bad_password')
{
	addLoadEvent(_login_a_onclick);
}

function focusGoogleBar()
{
	if (document.getElementById('q'))
	{
		// use try to eliminate IE7 error
		try
		{
			document.getElementById('q').focus();
		}
		catch (e)
		{
		
		}
	}
}
function addLoadEvent(fn)
{
	if (window.addEventListener)
	{
		window.addEventListener('load', fn, false)
	}
	else if (window.attachEvent)
	{
		window.attachEvent('onload', fn);
	}
}
