

var http_request = false;

// Window popup function
function popup(url, name, width, height) {
	
	var standard_width = 500;
	var standard_height = 600;
	
	if (!width || parseInt(width) < 100) width = standard_width;
	if (!height || parseInt(height) < 100) height = standard_height;
	
	if (!window.open(url, name, 'width=' + width + ', height=' + height + ', status=no, toolbar=no, menubar=no, location=no, scrollbars=yes, resizable=yes')) {

		alert("You probably have a popup blocker enabled.\nTry again after disabling it.");
	}
}

// A confirm for appling to a new site deal.
function confirmApply(lang) {
	
	var siteid = parseInt(document.getElementById('siteid').value);
	
	if (lang == 'se') { // SE
		
		var confirm_apply_msg = "Sajten kommer att läggas till under ditt konto.";
		var select_site_msg = "Du måste välja en sajt först.";
				
	} else { // EN
		
		var confirm_apply_msg = "The site will be added to your acccount.";
		var select_site_msg = "Please select a site to apply for.";
	}
	
	if (siteid > 0) {
		
		if (confirm(confirm_apply_msg)) {
			
			return true;
			
		} else {
			
			return false;
		}
		
	} else {
		
		alert(select_site_msg);
		return false;
	}
}

// A simple toogle function
function toogle(id) {

	var element = document.getElementById(id);

	if (element.style.display == 'none') {
		element.style.display = '';
	} else {
		element.style.display = 'none';
	}
}

	function popitup() {
		window.open("misc/freeroller/menu_freerollers.php",'free','height=600,width=195,scrollbars=yes');
	}

	function popitup2() {
		window.open("misc/freeroller/menu_freerollers2.php",'free','height=600,width=195,scrollbars=yes');
	}

	function popitup3() {
		window.open("misc/freeroller/menu_freerollers3.php",'free','height=200,width=400,scrollbars=yes');
	}

	function popitup4() {
		window.open("misc/freeroller/menu_freerollers4.php",'free','height=600,width=195,scrollbars=yes');
	}

	function subba(){
	 if(document.mail.email.value == document.mail.vemail.value){
	 	document.mail.submit();
	 }
	 else{ 
	 	alert("The mail address and the verify email address do not match!");
	 }
}

// Returns false if the user does not confirm the selection
function applyForSite() {

	var siteid = parseInt(document.getElementById('site').value);

	if (siteid > 0) {

		var confirmed = confirm("Would you like to apply for this site?");

		if (confirmed) {

			return true;

		} else {

			return false;
		}

	} else {

		alert("You must select a site!");
		return false;
	}

}

// Returns true if the form is appropriate filled out.
function editAccountIdentity(lang, msgObj) {

	var error = '';
	var message = "Failure!\nPlease look through the following field(s) again.\n";
	var defaultColor = "#FFFFFF";
	var markColor = "#FFE6E6";

	//var message_box = document.getElementById(msgObj);

	var firstname = document.getElementById('firstname');
	var lastname = document.getElementById('lastname');
	var email = document.getElementById('email');
	var country = document.getElementById('country');

	//message_box.style.display = 'none';

	firstname.style.backgroundColor = defaultColor;
	lastname.style.backgroundColor = defaultColor;
	email.style.backgroundColor = defaultColor;

	if (!firstname.value) {
		error = error + "- Firstname\n";
		firstname.style.backgroundColor = markColor;
	}
	if (!lastname.value) {
		error = error + "- Lastname\n";
		lastname.style.backgroundColor = markColor;
	}
	if ((email.value.length < 7) || (email.value.indexOf("@") == -1) 
			|| (email.value.indexOf(".") == -1)) {
		error = error + "- E-mail\n";
		email.style.backgroundColor = markColor;
	}
	if (country.value == '128') {
		error = error + "- Country\n";
		//country.style.backgroundColor = markColor;
	}


	if (error) {
		alert(message + "\n" + error + "\n");
		//message_box.style.display = '';
		return false;
	} else {
		return true;
	}

	return false;
}

// Returns true if the form is appropriate filled out.
function editAccountPayment(lang, msgObj) {

	var error = '';
	var message = "Failure!\nPlease look through the following field(s) again.\n";
	var defaultColor = "#FFFFFF";
	var markColor = "#FFE6E6";

	var method = document.getElementById('payment_method');
	var identifier = document.getElementById('payment_identifier');
	
	identifier.style.backgroundColor = defaultColor;
	
	if (!identifier.value) {
		error = error + "- " + method.options[method.selectedIndex].text + " value\n";
		identifier.style.backgroundColor = markColor;
	} else if (method.value == 1 && identifier.value && ((identifier.value.length < 7)
			|| (identifier.value.indexOf("@") == -1)
			|| (identifier.value.indexOf(".") == -1))) {
		error = error + "- NETeller value. Must be a valid E-mail address\n";
		identifier.style.backgroundColor = markColor;
	}

	if (error) {
		alert(message + "\n" + error + "\n");
		return false;
	} else {
		return true;
	}

	return false;
}

// Returns true if the form is appropriate filled out.
function editAccountLogin(lang) {

	var error = '';
	var message = "Failure!\nPlease look through the following field(s) again.\n";
	var defaultColor = "#FFFFFF";
	var markColor = "#FFE6E6";

	var username = document.getElementById('username');
	var email = document.getElementById('email');
	var password = document.getElementById('password');
	var new_password = document.getElementById('new_password');
	var re_password = document.getElementById('re_password');

	username.style.backgroundColor = defaultColor;
	password.style.backgroundColor = defaultColor;
	new_password.style.backgroundColor = defaultColor;
	re_password.style.backgroundColor = defaultColor;

	if (username.value !== email.value) {
		if ((username.value.length < 2) || (username.value.length > 15) 
			|| (username.value.indexOf("@") !== -1)) {
			error = error + "- Username. Must contain between 2-15 characters.\n";
			username.style.backgroundColor = markColor;
		}
	}

	if (password.value.length < 4) {
		error = error + "- Password. Required for edit.\n";
		password.style.backgroundColor = markColor;
	}

	if (new_password.value) {
		if ((new_password.value.length < 5) || (new_password.value.length > 15)) {
			error = error + "- New password. Must contain between 6-15 characters.\n";
			new_password.style.backgroundColor = markColor;
		} else if (new_password.value !== re_password.value) {
			error = error + "- New password & repeated password did not match.\n";
			new_password.style.backgroundColor = markColor;
			re_password.style.backgroundColor = markColor;
		}
	}

	if (error) {
		alert(message + "\n" + error + "\n");
		return false;
	} else {
		return true;
	}

	return false;
}

// Returns false if the form in inappropriate filled in
function submitMailForm() {

	var error = '';

	var name = document.getElementById('name').value;
	var email = document.getElementById('email').value;
	var receiver = document.getElementById('receiver').value;
//	var subject = document.getElementById('subject').value;
	var message = document.getElementById('message').value;

	// Name
	if (name.length < 2) {
		error = error + "- Name\n";
	}

	// Email
	if (!email) {
		error = error + "- Email\n";
	}

	// Receiver
	if (!receiver || receiver == '') {
		error = error + "- Receiver\n";
	}

	// Subject
//	if (subject.length < 3) {
//		error = error + "- Subject\n";
//	}

	// Message
	if (message.length < 10) {
		error = error + "- Message\n";
	} else if (message.length > 1000) {
		error = error + "- Message (" + message.length + "/1000)\nYour message cannot be longer than 1000 chars.";
	}

	if (error) {
		alert("Please correct the following fields:\n" + error);
		return false;

	} else {

		return true;
	}

	return false;
}

// Returns false if the user does not verify the alias
function addSiteIdentifier(alias_id, account_id, sitename, acc_needed) {

	alias = document.getElementById(alias_id).value;

	if (acc_needed)
		account = document.getElementById(account_id).value;

	if (sitename.length < 3) {
		sitename = "<missing sitename>";
	}

	if (alias.length < 1) {
		alert("You must enter your alias/username before submitting!\n" +
			"Please try again!");
		document.getElementById(alias_id).focus();
		return false;

	} else if (alias.length < 3) {
		alert("Your alias/username '" + alias + "' is inappropriate!\n" +
			"Please try again!");
		document.getElementById(alias_id).focus();
		return false;

	} else if (acc_needed && (account.length < 1)) {
		alert("You must enter your account number before submitting!\n" +
			"Please try again!");
		document.getElementById(account_id).focus();
		return false;

	} else if (acc_needed && (account.length < 3)) {
		alert("Your account number '" + account + "' is probably too short!\n" +
			"Please try again or contact our support!");
		document.getElementById(account_id).focus();
		return false;

	}
	
	
	// Account no. verification
	if (acc_needed) {
	
		var account_reply = prompt("Please confirm your account number at " +
			sitename + ".", "");
			
		if (account_reply) {
			
			if (account_reply == account) {
				
				msg = "Your identifiers for " + sitename + " has been saved as '" +
					alias + "' and '" + account + "'";
				
				alert (msg);
				return true;			
				
			} else {
				
				alert("The account number you entered did not match the previous " +
					"one,\nPlease try again!");
				addSiteIdentifier(alias_id, account_id, sitename, acc_needed);
			}
		}
		
	// Alias/username verification
	} else {
		
		var alias_reply = prompt("Please confirm your alias/username at " +
			sitename + ".", "");
		
		if (alias_reply) {
			
			if (alias_reply == alias) {
				
				msg = "Your identifier for " + sitename + " has been saved as '" +
					alias_reply + "'";
				
				alert (msg);
				return true;
				
			} else {
				
				alert("The alias/username you entered did not match the previous " +
					"one,\nPlease try again!");
				addSiteIdentifier(alias_id, account_id, sitename, acc_needed);
			}
		}
	}
	
	return false;
}

// Submits the form if all fields are appropriate filled out or
// returns a list of fields to be filled in otherwise.
function signupValidation() {
	
	var error = '';
	var message = "Failure!\nPlease look tru the following field(s) again.\n";
	var defaultColor = "#FFFFFF";
	var markColor = "#FFE6E6";
	
	firstname = document.getElementById('firstname');
	lastname = document.getElementById('lastname');
	username = document.getElementById('username');
	password = document.getElementById('password');
	re_password = document.getElementById('re_password');
	email = document.getElementById('email');
	re_email = document.getElementById('re_email');
	country = document.getElementById('country');
	captcha = document.getElementById('captcha');
	termsOfService = document.getElementById('terms').checked;

	firstname.style.backgroundColor = defaultColor;
	lastname.style.backgroundColor = defaultColor;
	username.style.backgroundColor = defaultColor;
	email.style.backgroundColor = defaultColor;
	re_email.style.backgroundColor = defaultColor;
	password.style.backgroundColor = defaultColor;
	re_password.style.backgroundColor = defaultColor;
	captcha.style.backgroundColor = defaultColor;

	// Name
	if ((firstname.value.length < 2) && (lastname.value.length < 2)) {

		error = error + "- Firstname & Lastname\n";
		firstname.style.backgroundColor = markColor;
		lastname.style.backgroundColor = markColor;

	} else {

		if (firstname.value.length < 2) {

			error = error + "- Firstname\n";
			firstname.style.backgroundColor = markColor;

		} else if (lastname.value.length < 2) {

			error = error + "- Lastname\n";
			lastname.style.backgroundColor = markColor;
		}
	}

	// Username
	if (username.value.length < 2 || username.value.length > 15) {

		error = error + "- Username\n  Must contain between 2-15 characters.\n";
		username.style.backgroundColor = markColor;
	}

	// password
	if (password.value.length < 5 || password.value.length > 15) {

		error = error + "- Password\n  Must be between 5 - 15 characters.\n";
		password.style.backgroundColor = markColor;

	} else if (password.value !== re_password.value){
		error = error + "- Password\n  The Passwords differ.\n";
		password.style.backgroundColor = markColor;
		re_password.style.backgroundColor = markColor;
	}


	// Email
	if (email.value.length < 5) {

		error = error + "- Email\n";
		email.style.backgroundColor = markColor;

	} else if (email.value.indexOf("@") == -1) {

		error = error + "- Email\nYour E-mail must contain an @\n";
		email.style.backgroundColor = markColor;

	} else if (email.value.indexOf(".") == -1) {

		error = error + "- Email\nYour E-mail Domain is inappropriate.\n";
		email.style.backgroundColor = markColor;

	} else if ((email.value !== re_email.value) || (re_email.value.indexOf("@") == -1)
			|| (re_email.value.indexOf(".") == -1)) {

		error = error + "- Email\nThe E-mails differs.\n";
		email.style.backgroundColor = markColor;
		re_email.style.backgroundColor = markColor;
	}

	// Country
	if (!country.value || country.value == '0') {

		error = error + "- Country\n";

	} 
	/* REMOVE MALTA BLOCK
	else if (country.value == '128') {

		error = error + "- Country\nYou are not allowed to create an account\n" +
			"at rakemeback if you are living in Malta.\n";
	}*/

	// Validation code
	if (captcha.value.length < 3) {

		error = error + "- Validation code\n";
		captcha.style.backgroundColor = markColor;
	}

	// Agreement
	if (!termsOfService) {

		error = error + "- Terms of service\nYou must accept the terms of service.";
	}

	// Error check & submit
	if (error) {
		
		alert("Please make sure that the following\nfields are correct " +
			"filled out:\n\n" + error);
		
	} else {
		
		//if (checkLogin("username", username.value)) {
		//if (checkLogin("email", email.value)) {
				
		document.getElementById('signupForm').submit();
	}
}

// Rakeback calculator
function rakebackCalc(form, resultId, resultHeading) {

	var hands = form.hands.value;
	var limit = form.limit.value;
	var percent = form.percent.value;
	var result = document.getElementById(resultId);

    var limit = (limit / 100);
	var amount = (hands * limit * percent * 30.4167);

	alert("hands * limit * percent * 30.4167\n" + 
			hands + " * " + limit + " * " + percent + " * 30.4167\n\n" +
			"Equals: " + amount + "\n");

	var rakeback = Math.round((amount * 1000) / 1000)

	result.style.display = 'block';
	result.innerHTML = "\n<h4>" + resultHeading + "</h4>\n" +
			"<br />\n<span>&#36;" + rakeback + "</span>";
}

function stickNewsDrop(id) {

	var textid = "newsid" + id + "text";
	var ingid = "newsid" + id + "ing";

	if (document.getElementById(textid).style.display=='none') {
		document.getElementById(textid).style.display='block';
		document.getElementById(ingid).style.display='none';
	} else {
		document.getElementById(textid).style.display='none';
		document.getElementById(ingid).style.display='block';
	}
}

function showForgotPassword() {
	if(document.getElementById('forgotPSW').style.display=='none')
		document.getElementById('forgotPSW').style.display='block';
	else
		 document.getElementById('forgotPSW').style.display='none';
}

function retrievePsw() {
	//document.getElementById('forgotusername').value=='' && 
	if(document.getElementById('forgotemail').value=='') {

		alert("You must fill the E-mail address");

	} else {

		makeRequest("retrievePSW.php?email=" + document.getElementById('forgotemail').value, processRetrievePSW, false, '');
		//alert("ajax");
	}

	/*alert(document.getElementById('forgotusername').value);
	alert(document.getElementById('forgotemail').value);
	document.getElementById('forgorMsg').firstChild.data='si';
	document.getElementById('forgorMsg').style.display='block';
	*/
}

function processRetrievePSW() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			if (http_request.responseText == 0) {

				alert("Invalid E-mail address.");

			} else if (http_request.responseText == 1) {

				document.getElementById('forgotPSW').style.display='none';
				alert("An e-mail has been sent to the specified address.");

			} else if (http_request.responseText == 2) {

				document.getElementById('forgotPSW').style.display='none';
				alert("Inactivated account!\nContact our support to reactive your account.");

			} else if (http_request.responseText == 3 || http_request.responseText == 9) {

				//document.getElementById('forgotPSW').style.display='none';
				alert("Could not find the E-mail you entered.\nPlease try again or contact our support.");

			} else {

				alert('An unexpected error occurred!\nPlease contact our support.');
			}
		}
	}
}
 
 function checkLogin(key, value) {
 	
 	var response = 0;
 	
 	if (key && value) {
 		
 		AjaxRequest.get(
 		{
 		 'url':'ajax/check_login.php?k=' + key + '&v=' + value
 		 ,'onSuccess': function(req) { responseHandler(req); }
 		 ,'onError': function(req) { alert('Error!\nStatusText=' + req.statusText + '\nContents=' + req.responseText); }
 		}
 		);
 	}
 	
 	function responseHandler(req) {
 		
 		response = req.responseText;
 		//alert("responseHandler() returned: " + response);
 		
 		if (req.responseText == 2) {
 			alert("The " + key + " '" + value + "' is already registrerd!");
 		}
 	}
 }
 
 
function makeRequest(url, func, post, parameters) {
	
	http_request = false;
	
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		//alert("Giving up :(\nCannot create an XmlHttp instance.");
		return false;
	}
	
	http_request.onreadystatechange = func;
	
	if(post){
		http_request.open('POST', url, true);
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http_request.setRequestHeader("Content-length", parameters.length);
		http_request.setRequestHeader("Connection", "close");
		http_request.send(parameters);
	} else {
		http_request.open('GET', url, true);
		http_request.send(null);	
	}
}

//////////////////////// For subdomains that pull in RMB site frames. This will rewrite all the links 
function rewriteLinks(currentDomain,replaceDomain){

	var links = document.getElementsByTagName('a');
	for(var i = 0; i < links.length; ++i){
       
	       if(links[i].href!='')
	       {
		       if(links[i].href.indexOf('#')==links[i].href.length-1 || links[i].href.indexOf('#helper')!=-1 || links[i].className=='thissite' )
			       ;
		       
		       else
		       {
			       
			       var newHREF = links[i].href.replace(currentDomain,replaceDomain);
			       if(newHREF.indexOf('fs_header.php')!=-1)newHREF = newHREF.replace('fs_header.php','');
			       links[i].setAttribute('href',newHREF);
		       }
		       
		       if(links[i].rel)
		       {
			       var newRel = replaceDomain+""+links[i].rel;
			       links[i].rel = newRel;
		       }	
	       }
	}
       
       
	//fix form
	var inputs = document.getElementsByTagName('form');
	for(var i=0;i<inputs.length;i++)
	{
	       var newSRC= inputs[i].action.replace(currentDomain, replaceDomain);
	       inputs[i].action = newSRC;
		
	}
}
