function changeInputType(oldObject, oType) {
  var newObject = document.createElement('input');
  newObject.type = oType;
  if(oldObject.size) newObject.size = oldObject.size;
  if(oldObject.value) newObject.value = oldObject.value;
  if(oldObject.name) newObject.name = oldObject.name;
  if(oldObject.id) newObject.id = oldObject.id;
  if(oldObject.className) newObject.className = oldObject.className;
  if(oldObject.tabIndex) newObject.tabIndex = oldObject.tabIndex;
  oldObject.parentNode.replaceChild(newObject,oldObject);
  return newObject;
}

function changeInputError(oldObject) {
  var newObject = document.createElement('div');
  if(oldObject.size) newObject.size = oldObject.size;
  if(oldObject.value) newObject.value = oldObject.value;
  if(oldObject.name) newObject.name = oldObject.name;
  if(oldObject.id) newObject.id = oldObject.id;
  if(oldObject.tabIndex) newObject.tabIndex = oldObject.tabIndex;
  newObject.innerHTML = oldObject.innerHTML;
  newObject.className = "header_input_error ie6";
  newObject.onclick = oldObject.onclick;
  oldObject.parentNode.replaceChild(newObject,oldObject);
  return newObject;
}

function changeRulesNumber(oldObject) {
  var newObject = document.createElement('img');
  if(oldObject.width) newObject.width = oldObject.width;
  if(oldObject.height) newObject.height = oldObject.height;
  if(oldObject.src) newObject.src = oldObject.src;
  if(oldObject.id) newObject.id = oldObject.id;
  if(oldObject.className) newObject.className = oldObject.className;
  oldObject.parentNode.replaceChild(newObject,oldObject);
  return newObject;
}

function langSelOnOff() {
	if ($('language_selector_box').style.display == 'none') {
		$('language_selector_box').appear({duration: 0.3});
	} else {
		$('language_selector_box').fade({duration: 0.3});
	}
}

//this function runs when the page is loaded, switch the checkbox with an image
//image is at header.php
function replaceChecks() {
	
	//get all the input fields on the page
	inputs = document.getElementsByTagName('input');

	//cycle trough the input fields
	for(var i=0; i < inputs.length; i++) {

		//check if the input is a checkbox
		if(inputs[i].getAttribute('type') == 'checkbox') {
			//create a new image
			var img = document.createElement('img');
			//img.className = inputs[i].className;

			//check if the checkbox is checked
			if(inputs[i].checked) {
				img.src = imgTrue;
			} else {
				img.src = imgFalse;
			}

			//set image ID and onclick action
			img.id = 'checkImage'+i;
			//set image 
			if(typeof inputs[i].onclick == 'function') {

			} else {
				img.onclick = new Function('checkChange('+i+')');
			}
			//place image in front of the checkbox
			inputs[i].parentNode.insertBefore(img, inputs[i]);
			
			//hide the checkbox
			inputs[i].style.display='none';
			img.style.cursor = 'pointer';
			//img.style.border = '1px solid #c5c5c5';
			img.style.border = '0';
			if(typeof inputs[i].onclick == 'function') {
				img.onclick = function(){
					img_id = img.id.split("checkImage")[1];
					checkChange(img_id);
				};
				Event.observe(img, "click", $(img.id).next('input').onclick);
			} else {
				img.onclick = new Function('checkChange('+i+')');
			}

		}
	}
}

//change the checkbox status and the replacement image
function checkChange(i) {
	if(inputs[i].checked) {
		inputs[i].checked = '';
		$('checkImage'+i).src = imgFalse;
	} else {
		inputs[i].checked = 'checked';
		$('checkImage'+i).src = imgTrue;
	}
}

/*
Script for adding bookmark
*/
function CreateBookmarkLink(title, url) {
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true;
	}
}

/*
This function handles the dropdown
dropdown span: id_elem
dropdown input hidden: id_elem + '_input'
*/
function setDropDown(id_elem, label, value) {
	$(id_elem).update(label);
	$(id_elem + '_input').value= value;
	return false;
}

//Sidebar Button function
function launch_sidebarbtn(btn_number) {
	alert(btn_number);
}


//Opens GameWindow (specific game)
function gameWindow(page_url) {
	var WindowFull = window.open(page_url,'game_window',"left=0,screenX=0,top=0,screenY=0,titlebar=no,toolbar=no,directory=no,resizable=1,scrolling=yes,scrollbar=yes,scroll=yes,width=" + screen.width + ",height=" + (screen.height-50));
	WindowFull.focus();
}

//Opens HitechWindow
function hitechWindow(page_url) {
	var WindowFull = window.open(page_url,'hitech_window',"left=0,screenX=0,top=0,screenY=0,titlebar=no,toolbar=no,directory=no,resizable=1,scrolling=yes,scrollbar=yes,scroll=yes,width=" + screen.width + ",height=" + (screen.height-50));
	WindowFull.focus();
}

//Opens CashierWindow
function cashierWindow(page_url) {
	var WindowFull = window.open(page_url,'cashier_window',"left=0,screenX=0,top=0,screenY=0,titlebar=no,toolbar=no,directory=no,scrolling=yes,scrollbars=yes,scroll=yes,width=" + screen.width + ",height=" + (screen.height-50));
	WindowFull.focus();
}

//Opens New window in full width and height
function openWindowFull(page_url,window_name) {
	var WindowFull = window.open(page_url,window_name,"left=0,screenX=0,top=0,screenY=0,titlebar=no,toolbar=no,directory=no,resizable=1,scrolling=yes,scrollbar=yes,scroll=yes,width=" + screen.width + ",height=" + (screen.height-50));
	WindowFull.focus();
}

//Opens New window in almost half width and height
function openWindowHalf(page_url,window_name) {
	var WindowHalf = window.open(page_url,window_name,"left=" + (screen.width - screen.width*0.60 - 50) + ",screenX=" + (screen.width - screen.width*0.6 -50) + ",top=60,screenY=60,titlebar=no,toolbar=no,directory=no,resizable=1,scrolling=yes,scrollbar=yes,scroll=yes,width=" + screen.width*0.6 + ",height=" + (screen.height-50)*0.7);
	WindowHalf.focus();
}

//function called by Flash HomePage and Sidebar flash Play Now button
function flash_Home_PlayNow_Button() {
	try {
		pageTracker._trackPageview("/"+current_lang+"/home-play-now");
	} catch(err) {
		console.log(err);
	}
	console.log(current_lang);
	launchBingoLobby();
}

//Gets absolute position of an element.
function getAbsolutePosition(element) {
  var r = { x: element.offsetLeft, y: element.offsetTop };
  if (element.offsetParent) {
	var tmp = getAbsolutePosition(element.offsetParent);
	r.x += tmp.x;
	r.y += tmp.y;
  }
  return r;
};

//Php equivalent ucWords
function ucwords( str ) {
	// Uppercase the first character of every word in a string 
	//
	// version: 812.316
	// discuss at: http://phpjs.org/functions/ucwords
	// +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
	// +   improved by: Waldo Malqui Silva
	// +   bugfixed by: Onno Marsman
	// *     example 1: ucwords('kevin van zonneveld');
	// *     returns 1: 'Kevin Van Zonneveld'
	// *     example 2: ucwords('HELLO WORLD');
	// *     returns 2: 'HELLO WORLD'
	str = (str+'').toLowerCase();
	return (str+'').replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase ( ); } );
}


 function explode(separator, text) {
	var r = text.split(separator);

	var ending = r.slice().join(separator);
	//r.length = count;
	//r[count-1] = ending;

	return r;
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}
Element.observe(document, 'dom:loaded', function(){
	if($('popjv') != null) {
		new Effect.Move($('popjv'), { x: 100, y: 180, mode: 'absolute' });
		//delaiCLOSE = setTimeout("closepop()",25000);
	}
});
function closepop() {
	$('popjv').onclick = function(){
		return false;
	};
	$('popjv').style.visibility = 'hidden';
	//clearTimeout(delaiCLOSE);
}
function goToPopUp(url, lang, pagetracker) {
	if($('popjv').style.visibility != 'hidden') {
		if(pagetracker != ''){
			pageTracker._trackPageview(pagetracker);
		}
		closepop();
		window.location.href = url;
	}
}
	
//checks for enter key and submits the form
function checkEnterAndSubmit(e, formId){ //e is event object passed from function invocation
	var characterCode;// literal character code will be stored in this variable
	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		logUserIn('login','pass'); return false;
		return false;
	}
	else{
		return true;
	}
}

function launchBingoLobby(new_lang) {
	//regex = /\?sorinspa$/  && regex.test(window.location)
	if(typeof(logged_in)!="undefined" && !logged_in ){
		launchBingoLobby2();
		return false;
	}
	
	/*
	if (current_lang == 'fr')
		openWindowFull('http://banner.bingo770.com/flash/fr/live/bingo/bingo.php', 'bingo_lobby');
	else
		openWindowFull('http://banner.bingo770.com/flash/live/bingo/bingo.php', 'bingo_lobby');
	*/
	if(typeof(new_lang)=="undefined"){
		var new_lang = "";
	}
	openWindowFull('http://www.bingo770.com/launch_bingo/'+new_lang, 'bingo_lobby');
	if (pageTracker)
		pageTracker._trackPageview('/launch_lobby/' + current_lang);
}


function center(element, offsetX, offsetY){
	//set default values
	offsetX = offsetX || 0;
	offsetY = offsetY || 0;
    try{
        element = $(element);
    }catch(e){
        return;
    }

    var my_width  = 0;
    var my_height = 0;

    if ( typeof( window.innerWidth ) == 'number' ){
        my_width  = window.innerWidth;
        my_height = window.innerHeight;
    }else if ( document.documentElement && 
             ( document.documentElement.clientWidth ||
               document.documentElement.clientHeight ) ){
        my_width  = document.documentElement.clientWidth;
        my_height = document.documentElement.clientHeight;
    }
    else if ( document.body && 
            ( document.body.clientWidth || document.body.clientHeight ) ){
        my_width  = document.body.clientWidth;
        my_height = document.body.clientHeight;
    }

    element.style.position = 'absolute';
    element.style.zIndex   = 99;

    var scrollY = 0;

    if ( document.documentElement && document.documentElement.scrollTop ){
        scrollY = document.documentElement.scrollTop;
    }else if ( document.body && document.body.scrollTop ){
        scrollY = document.body.scrollTop;
    }else if ( window.pageYOffset ){
        scrollY = window.pageYOffset;
    }else if ( window.scrollY ){
        scrollY = window.scrollY;
    }

    var elementDimensions = Element.getDimensions(element);

    var setX = ( my_width  - elementDimensions.width  ) / 2;
    var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;

    setX = ( setX < 0 ) ? 0 : setX;
    setY = ( setY < 0 ) ? 0 : setY;

    element.style.left = setX + offsetX + "px";
    element.style.top  = setY + offsetY + "px";

    element.style.display  = 'block';
}


function lobbyLogin () {
	var msg_elem = $$('#lobby_popup_left .description').first();

	//Message: checking login
	if ($(msg_elem).hasClassName('popup_login_error'))
		$(msg_elem).removeClassName('popup_login_error');
		
	$(msg_elem).update(popup_error_message_login_wait);
	
	//If logged in, it will be redirected automatically
	//If not, it will display errror message on left box.
	
	var url = "/auth/ajax_login/";
	var loginx = $('popup_login').value;
	var passwordx = ($('popup_pass').value.toLowerCase());
						
	new Ajax.Request(url, {method: 'post', parameters: {
								login: loginx,
								password: passwordx
					},
			onComplete:function(text){
					var login_data = text.responseText.evalJSON();
					if(login_data['result'] == true) {
						logged_in = true;
						var win_opened = window.open('http://www.bingo770.com/launch_bingo/','bingo_lobby');
						//problem with popup blocker - MOHSIN
						/*if(win_opened) {*/
							//refresh page
							//document.location.href = '/';
							window.location.reload();
						/*} else {
							//show popup
							//new Ajax.Updater($('header'), 'http://www.bingo770.com/Auth/ajax_login_header/');
							closeLobbyPopup();
							center('no_popup');
							$('no_popup').show();
							$('click_here_open').observe('click', openNoPopup_handler);
						}*/
					} else {
						$(msg_elem).addClassName('popup_login_error');
						$(msg_elem).pulsate({ pulses: 1, duration: 0.5 });
						$(msg_elem).update(popup_error_message_incorect_credentials);
					}
			}
		}
	);
	return false;
}

function openNoPopup_handler(event) {
	window.open('http://www.bingo770.com/launch_bingo/','bingo_lobby');
	document.location.href = '/';
}


function launchBingoLobby2(){
	center('lobby_popup');
	$('lobby_popup').show();		
}

function closeLobbyPopup() {
	$('lobby_popup').hide();
}

function removeRowFromTable(id){
	var tbl = document.getElementById(id);
	var lastRow = tbl.rows.length;
	if (lastRow > 1) tbl.deleteRow(lastRow - 1);
}

function addRowToTable(id, colData){
	var tbl = document.getElementById(id);
	var lastRow = tbl.rows.length;
	var iteration = lastRow;
	var row = tbl.insertRow(lastRow);

	for(ci=0; ci<colData.length; ci++){
		var cellLeft = row.insertCell(ci);
		var textNode = document.createTextNode(colData[ci]);
		cellLeft.appendChild(textNode);
	}
}	