var http = (navigator.appName == "Microsoft Internet Explorer") ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();

var bodyClass = '';

/** Send to Friend Section **/
function send2Friend(URL, gameName){
	var params = "width=400, height=300";
	preview = window.open('/send2friend.php?show=true&game='+ gameName + '&url=' + URL, 'SendToFriend', params);
	return preview;
}

function send2FriendLightbox(URL, gameName){
	window.scrollTo(0,0);
	bodyClass = document.body.className;
	document.body.className = bodyClass + ' show-popup';
	document.getElementById('send-to-friend-form').style.display = 'block';
	document.getElementById('s2f-url').value = URL;
	document.getElementById('s2f-game').value = gameName;
	if (document.addEventListener) {
		document.addEventListener("keydown", onEscape, false);
	} else {
		document.attachEvent("onkeydown", onEscape);
	}
}

function hideSend2FriendForm() {
	document.getElementById('send-to-friend-form').style.display = 'none';
	document.body.className = bodyClass;
}

function onEscape(event) {
	if (event.keyCode == 27) {
		hideSend2FriendForm();
		hideForgotPasswordDialog();
		event.preventDefault();
		return false;
	}
}

function sendToFriendRequest() {
	http.open('post', '/send2friend.php?cmd=ajax_send_on&url='
	+ document.getElementById('s2f-url').value +
	'&game=' + document.getElementById('s2f-game').value +
	'&f[to]=' + document.getElementById('s2f-to').value +
	'&f[subject]=' + document.getElementById('s2f-subject').value);
	http.onreadystatechange = sendToFriendCallback;
	http.send(null);
}

function sendToFriendCallback() {
	if(http.readyState == 4 && http.responseText != ''){
		hideSend2FriendForm();
		var result = eval("(" + http.responseText + ")");
		document.getElementById('page_message').style.display = 'block';
		var html = '';
		for (i in result.messages) {
			html += result.messages[i] + '<br />';
		}
		document.getElementById('page_message_content').innerHTML = html;
	}
}

/****************************/



/** Forgot Password Section **/
function showForgotPasswordDialog(){
	bodyClass = document.body.className;
	document.body.className = bodyClass + ' show-popup';
	document.getElementById('forgot-password-form').style.display = 'block';
	if (document.addEventListener) {
		document.addEventListener("keydown", onEscape, false);
	} else {
		document.attachEvent("onkeydown", onEscape);
	}
}

function hideForgotPasswordDialog() {
	document.getElementById('forgot-password-form').style.display = 'none';
	document.body.className = bodyClass;
}

function emailSent(){
	var html = "<ul class='info'>" +
			   "<li>Message has been successfully sent.</li>" +
			   "</ul>";
	if (document.getElementById("page_message")) {
		document.getElementById("page_message").style.display = 'block';
		document.getElementById("page_message_content").innerHTML = html;
		document.getElementById("page_message_button_close").style.display = '';
		document.getElementById("page_message_button_ok").style.display = 'none';
	}
}

function forgotPasswordCallback(){
	var html = "<ul class='info'>" +
			   "<li>Confirmation link has been sent to your email.</li>" +
			   "</ul>";
	document.getElementById("page_message").style.display = 'block';
	document.getElementById("page_message_content").innerHTML = html;

}
/*****************************/



/** States Section **/
function getStates(country_id){
	if (country_id == '1') {
		document.getElementById('sign-states').style.visibility = 'visible';
		http.open('post', '/ajax/get_states.php?country_id=' + country_id);
		http.onreadystatechange = updateStates;
		http.send(null);
	} else {
		document.getElementById('sign-states').style.visibility = 'hidden';
	}
}

function updateStates(){
	if(http.readyState == 4 && http.responseText != ''){
		var result = eval("(" + http.responseText + ")");
		var states_list = document.getElementById("states").options;
        states_list.length = 0;
        var counter = 0;
        for (i in result){
			var option = new Option(String(result[i]), String(i));
		   	states_list.add(option);
		   	if(_userStateId == i){
		   		states_list.selectedIndex = counter;
		   	}
		   	counter++;
		}
	}
}
/****************************/


/** Categories Section **/
var contRequestCategoryList = '';

function getCategories(parent_id, listId, newCategoryId) {
	if (parent_id == 'Other') {
		document.getElementById(newCategoryId).style.display = "block";
		switch (listId) {
			case "cr-category":
				document.getElementById("cr-category").options.length = 0;
				var option = new Option("Select Category", "0");
				document.getElementById("cr-category").options.add(option);
				option = new Option("Other", "Other");
				document.getElementById("cr-category").options.add(option);
				document.getElementById("cr-category").options.selectedIndex = 1;
				document.getElementById("other-category-request").style.display = "block";
			case "cr-sub-category":
				document.getElementById("cr-sub-category").options.length = 0;
				var option = new Option("Select Category", "0");
				document.getElementById("cr-sub-category").options.add(option);
				option = new Option("Other", "Other");
				document.getElementById("cr-sub-category").options.add(option);
				document.getElementById("cr-sub-category").options.selectedIndex = 1;
				document.getElementById("other-sub-category-request").style.display = "block";
			case "cr-item":
				document.getElementById("cr-item").options.length = 0;
				var option = new Option("Select Category", "0");
				document.getElementById("cr-item").options.add(option);
				option = new Option("Other", "Other");
				document.getElementById("cr-item").options.add(option);
				document.getElementById("cr-item").options.selectedIndex = 1;
				document.getElementById("other-item-request").style.display = "block";
			break;
		}
	} else {
		document.getElementById(newCategoryId).style.display = "none";
		document.getElementById("other-category-request").style.display = "none";
		document.getElementById("other-sub-category-request").style.display = "none";
		document.getElementById("other-item-request").style.display = "none";
		if (listId) {
			document.getElementById('loadMask').style.display = "block";
			contRequestCategoryList = listId;
			http.open('post', '/ajax/get-categories.php?parent_id=' + parent_id);
			http.onreadystatechange = putCategoriesIntoList;
			http.send(null);
		}
	}
}

function putCategoriesIntoList() {
	if(http.readyState == 4 && http.responseText != ''){
		var result = eval("(" + http.responseText + ")");
		var list = document.getElementById(contRequestCategoryList).options;
		list.length = 0;
		var counter = 1;
		option = new Option("Select Category", "0");
		list.add(option);
		for (i in result) {
			if (typeof result[i] != 'function') {
				option = new Option(String(result[i]), String(i));
				list.add(option);
				counter++;
			}
		}
		option = new Option("Other", "Other");
		list.add(option);
		counter++;
	}
	document.getElementById('loadMask').style.display = "none";
}

function checkContributorForm() {
	var isValid = ((document.getElementById("cr-category").value != "0" && document.getElementById("cr-category").value != "Other") || ((document.getElementById("cr-master-category").value != "0" || document.getElementById("master_other").value != '') && document.getElementById("category_other").value != '')) ? true : false;
	if (!isValid) {
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScrollTop();
		document.getElementById('message-text').innerHTML = '<strong><font size="3" color="#FFFFFF">Please select at least Master Category and Category</font></strong>';
		document.getElementById('page_message').style.display = "block";
		document.getElementById('page_message').style.top = (arrayPageScroll[1] + (arrayPageSize[1]-476)/2)-145 + "px";
	}
	return isValid;
}
/****************************/


/** User Profile Section **/

var currentOpenedField;

function showEditField(field) {
	document.getElementById(field).style.display = "none";
	document.getElementById(field + '-edit').style.display = "block";
}

function saveProfileData(field, value, callback) {
	currentOpenedField = callback;
	document.getElementById('loadMaskProfile').style.display = "block";
	http.open('post', '/ajax/save-profile-data.php?field=' + field + '&value=' + value);
	http.onreadystatechange = saveProfileCallback;
	http.send(null);
}

function saveProfileCallback() {
	if (http.readyState == 4) {
		document.getElementById(currentOpenedField + '-edit').style.display = "none";
		document.getElementById(currentOpenedField).style.display = "block";
		if (http.responseText != '') {
			document.getElementById(currentOpenedField + '-value').innerHTML = http.responseText;
		}
	}
	document.getElementById('loadMaskProfile').style.display = "none";
}

function showPictureDialog() {
	document.getElementById('profile-picture-dialog').style.display = "block";
}

function showPasswordDialog() {
	document.getElementById('profile-password-dialog').style.display = "block";
}

/**************************/


/**** Typo Section ****/

function addTypoHotKey() {
	if (shortcut) {
		shortcut.add("Ctrl+Enter", sendTypoReport);
	}
}

function sendTypoReport() {
	var selection = null;
    if      (window.getSelection)   selection = window.getSelection().toString();
    else if (document.getSelection) selection = document.getSelection();
    else if (document.selection)   selection = document.selection.createRange().text;
	if (selection) {
		http.open('post', '/ajax/send-typo-report.php?typo=' + selection);
		http.onreadystatechange = sendTypoCallback;
		http.send(null);
	}
}

function sendTypoCallback() {
	if (http.readyState == 4) {
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScrollTop();
		document.getElementById('message-text').innerHTML = '<strong><font size="3" color="#FFFFFF">Thank you!</font></strong>';
		document.getElementById('page_message').style.display = "block";
		document.getElementById('page_message').style.top = (arrayPageScroll[1] + (arrayPageSize[1]-476)/2)-145 + "px";
	}
}

var typoTimeout;

function showInstruction() {
	var ua = navigator.userAgent.toLowerCase();
	isOpera = ua.indexOf("opera") > -1;
    isSafari = (/webkit|khtml/).test(ua);
    isSafari3 = isSafari && ua.indexOf('webkit/5') != -1;
    isIE = !isOpera && ua.indexOf("msie") > -1;
    isIE7 = !isOpera && ua.indexOf("msie 7") > -1;
    isGecko = !isSafari && ua.indexOf("gecko") > -1;

	var typoMsg = document.getElementById('typo-instruct');
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScrollTop();
	typoMsg.style.display = "block";
	if (isIE) {
		if (isIE7) {
			typoMsg.style.width = (arrayPageSize[0]-2) + "px";
		} else {
			typoMsg.style.width = arrayPageSize[0] + "px";
		}
	} else {
		typoMsg.style.width = (arrayPageSize[0]-18) + "px";
	}
	window.onscroll = position;
}

function hideInstruction() {
	document.getElementById('typo-instruct').style.display = "none";
}

function position() {
	if (document.getElementById('typo-instruct').style.display == "block") {
		var arrayPageScroll = getPageScrollTop();
		var topPosition = arrayPageScroll[1] + "px";
		document.getElementById('typo-instruct').style.top = topPosition;
	}
}

/**********************/


/*** Page Functions ***/

function getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
	arrayPageSize = new Array(w,h)
	return arrayPageSize;
}

function getPageScrollTop(){
	var yScrolltop;
	var xScrollleft;
	if (self.pageYOffset || self.pageXOffset) {
		yScrolltop = self.pageYOffset;
		xScrollleft = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){	 // Explorer 6 Strict
		yScrolltop = document.documentElement.scrollTop;
		xScrollleft = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScrolltop = document.body.scrollTop;
		xScrollleft = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScrollleft,yScrolltop)
	return arrayPageScroll;
}

/*************************/



