var brow = '';
if (document.getElementById) { brow = 'ffox';}
else if (document.all) { brow = 'ie'; }
else if (document.layers) { brow = 'ns'; }

var clicks = 0;
var myDate;
var clickStart;
var cpm;

function el(string) {
	var x = '';
	if (brow == 'ffox') { x = document.getElementById(string);}
        else if (brow == 'ie') { x = document.all(string); }
	return x;
}

function getObjInnerText(obj){
  if (document.all) { // IE;
    return obj.innerText;
  }
  else{
    if (obj.textContent)
    {
      return obj.textContent;
    }
  }
}

function refreshImg() {
	var src = el('captcha_image').src; 
	el('captcha_image').src = src + "?" + Math.random();
}

function fill(qty, val) { el(val).setAttribute('value',qty); }

function bump_recruiter() {
	clicks += 1;
	if (clickStart)
	{
		var now = new Date();
		var duration = now.getTime() - clickStart;
		cpm = clicks / (duration / 60000);
	}
	else
	{
		myDate = new Date();
		clickStart = myDate.getTime();
		cpm = "Over 9000";
	}
	dunnit.push(captchas.shift());
	if (captchas[0]) {
		el("captcha_image").setAttribute('src', 'http://www.lordsofaman.es/img.php?hash='+String(captchas[0]));
		el("captcha").setAttribute('value', String(captchas[0]));
	} else {
		el("captcha_image").setAttribute('src', '');
		el("captcha").setAttribute('value', '');
	}
	if (el("captcha_image2")) {
		var sechs = 0;
		for (sechs = 1; sechs < 30; sechs++) {
			if (captchas[sechs]) {
				el("captcha_image" + String(sechs+1)).setAttribute('src', 'http://www.lordsofaman.es/img.php?hash='+ (captchas[sechs]));
				el("captcha" + String(sechs+1)).innerHTML = captchas[sechs];
			} else {
				el("captcha_image" + String(sechs+1)).setAttribute('src', '');
				el("captcha" + String(sechs+1)).innerHTML = '';
			}			
		}
		document.title = captchas.length + ' images ready / Lords Of Aman / Alliance Recruiter';

		if (el("cpm"))
		{
			el("cpm").innerHTML = (cpm.toFixed(1)) + ' CPM';
                        el("cpm_up").value = (cpm.toFixed(1));
                        el("clicks").value = (clicks.toFixed(1));
		}
	}
}
function isNumber(e) {
	var keynum, keychar, numcheck;
	if(window.event) { keynum = e.keyCode; }
	else if(brow == "ffox") { keynum = e.which; }
	keychar = String.fromCharCode(keynum);
	numcheck = /[1-9]/;
	if (numcheck.test(keychar)) { return keychar; }
	else {
		if (keynum >= 97 && keynum <= 105) {
			return String(keynum - 96);
		} else if (keychar == "U") {
			return "4";
		} else if (keychar == "I") {
			return "5";
		} else if (keychar == "O") {
			return "6";
		} else if (keychar == "J") {
			return "1";
		} else if (keychar == "K") {
			return "2";
		} else if (keychar == "L") {
			return "3";
		} else {
			//alert(keynum);
			return false;
		}
	}
}

function pausecomp(millis) { // www.sean.co.uk
	var date = new Date();
	var curDate = null;
	do { curDate = new Date(); }
	while(curDate-date < millis);
}

function ajax_submit(obj, method) {
	var getstr = get(obj);
	var action = obj.action.replace(/\.php/, "x.php");
	sendCall(action + getstr, "alertContents");
	return false;
}

function ajax_refresh(page) {
	var action = page;
	if (inCall == false) {
		sendCall(action, "updatePage");
	}
	return false;
}

function get(obj) {
	var getstr = "?";
	for (i=0; i<obj.elements.length; i++) {
		if (obj.elements[i].type == "text") {
			getstr += obj.elements[i].name + "=" + obj.elements[i].value + "&";
		}
		if (obj.elements[i].type == "hidden") {
			getstr += obj.elements[i].name + "=" + obj.elements[i].value + "&";
		}
		if (obj.elements[i].type == "checkbox") {
			if (obj.elements[i].checked) {
				getstr += obj.elements[i].name + "=" + obj.elements[i].value + "&";
			} else {
				getstr += obj.elements[i].name + "=&";
			}
		}
		if (obj.elements[i].type == "radio") {
			if (obj.elements[i].checked) {
				getstr += obj.elements[i].name + "=" + obj.elements[i].value + "&";
			}
		}
		if (obj.elements[i].type == "select-one") {
			getstr += obj.elements[i].name + "=" + obj.elements[i].value + "&";
		}
	}
	return getstr;
}

var http_request = false;
var captchas = new Array();
var dunnit = new Array();

if (window.XMLHttpRequest) { // Mozilla, Safari,...
	http_request = new XMLHttpRequest();
	if (http_request.overrideMimeType) {
		http_request.overrideMimeType('text/xml');
	}
} else if (window.ActiveXObject) { // IE
	try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	}
}

var inCall = false;
var callToArray = new Array();
var returnToArray = new Array();
function sendCall(whereTo, returnTo){
	callToArray.push(whereTo);
	returnToArray.push(returnTo);
}
function callQueue(){
	if(!inCall && callToArray.length > 0){
		if(callToArray.length > 0){
			whereTo = callToArray.shift();
			returnTo = returnToArray.shift();
			doCall(whereTo, returnTo);
		}
	}
}
function doCall(whereTo, returnTo){
	inCall = true;
	http_request.open('GET', whereTo, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
	http_request.setRequestHeader('Content-Length', '0');
	eval("http_request.onreadystatechange = "+returnTo);
	http_request.send('');
}
var queueWatcher = setInterval(callQueue, 100);
function in_array(p_val, yarr) {
	for(var i = 0, l = yarr.length; i < l; i++) {
		if(yarr[i] == p_val) {
			return true;
		}
	}
	return false;
}
function updatePage() {
	if (http_request.readyState == 4) {
		inCall = false;
		if (http_request.status == 200) {
			var xml=http_request.responseXML;
			var xmlDoc=xml.documentElement;
			var i;
			var noobs;
			if (xmlDoc) {
				noobs=xmlDoc.getElementsByTagName("notify");
			} else {
				if (xml.parseError.errorCode != 0) {
					var myErr = xml.parseError;
					alert("XML Document parse error " + myErr.reason);
				}
			}
			
			for (i = 0; i < noobs.length; i++) {
				if (noobs[i].attributes[0].firstChild) {
					// works in IE, FF, safari
					if (el(noobs[i].attributes[0].firstChild.nodeValue)) {
						el(noobs[i].attributes[0].firstChild.nodeValue).innerHTML = noobs[i].textContent || noobs[i].firstChild.nodeValue;
					}
				}
				else if (noobs[i].attributes[0]) {
					// works in Opera 9.5
					if (el(noobs[i].attributes[0].nodeValue)) {
						if (noobs[i].firstChild) {
							el(noobs[i].attributes[0].nodeValue).innerHTML = noobs[i].firstChild.nodeValue;
							if (el(noobs[i].attributes[0].nodeValue + 'again')) {
								el(noobs[i].attributes[0].nodeValue + 'again').innerHTML = noobs[i].firstChild.nodeValue;
							}
						} else {
							el(noobs[i].attributes[0].nodeValue).innerHTML = "";
							if (el(noobs[i].attributes[0].nodeValue + 'again')) {
								el(noobs[i].attributes[0].nodeValue + 'again').innerHTML = "";
							}
						}
					}
				}
			}
		}
		callQueue();
	}
}

function alertContents() {
	if (http_request.readyState == 4) {
		inCall = false;
		if (http_request.status == 200) {
			var xml=http_request.responseXML;
			var xmlDoc=xml.documentElement;
			var i;
			var noobs;
			if (xmlDoc) {
				noobs=xmlDoc.getElementsByTagName("notify");
			} else {
				if (xml.parseError.errorCode != 0) {
					var myErr = xml.parseError;
					alert("XML Document parse error " + myErr.reason);
				}
			}
			
			for (i = 0; i < noobs.length; i++) {
				if (noobs[i].attributes[0].firstChild) {
					// works in IE, FF, safari
					if (el(noobs[i].attributes[0].firstChild.nodeValue)) {
						if (noobs[i].firstChild) {
							el(noobs[i].attributes[0].firstChild.nodeValue).innerHTML = noobs[i].firstChild.nodeValue;
							if (el(noobs[i].attributes[0].firstChild.nodeValue + 'again')) {
								 el(noobs[i].attributes[0].firstChild.nodeValue).innerHTML = noobs[i].textContent || noobs[i].firstChild.nodeValue;
							}
						} else {
							el(noobs[i].attributes[0].firstChild.nodeValue).innerHTML = "";
							if (el(noobs[i].attributes[0].firstChild.nodeValue + 'again')) {
								el(noobs[i].attributes[0].firstChild.nodeValue + 'again').innerHTML = "";
							}
						}
					}
				}
				else if (noobs[i].attributes[0]) {
					// works in Opera 9.5
					if (el(noobs[i].attributes[0].nodeValue)) {
						if (noobs[i].firstChild) {
							el(noobs[i].attributes[0].nodeValue).innerHTML = noobs[i].firstChild.nodeValue;
							if (el(noobs[i].attributes[0].nodeValue + 'again')) {
								el(noobs[i].attributes[0].nodeValue + 'again').innerHTML = noobs[i].firstChild.nodeValue;
							}
						} else {
							el(noobs[i].attributes[0].nodeValue).innerHTML = "";
							if (el(noobs[i].attributes[0].nodeValue + 'again')) {
								el(noobs[i].attributes[0].nodeValue + 'again').innerHTML = "";
							}
						}
					}
				}
			}
			var voobs=xmlDoc.getElementsByTagName("value");
			for (i = 0; i < voobs.length; i++) {
				if (voobs[i].attributes[0].firstChild) {
					// works in IE, FF, safari
					if (el(voobs[i].attributes[0].firstChild.nodeValue)) {
						if (voobs[i].firstChild) {
							el(voobs[i].attributes[0].firstChild.nodeValue).setAttribute('value', voobs[i].firstChild.nodeValue);
						} else {
							el(voobs[i].attributes[0].firstChild.nodeValue).setAttribute('value', "");
						}
					}
				}
				else if (voobs[i].attributes[0]) {
					// works in Opera 9.5
					if (el(voobs[i].attributes[0].nodeValue)) {
						if (voobs[i].firstChild) {
							el(voobs[i].attributes[0].nodeValue).setAttribute('value', voobs[i].firstChild.nodeValue);
						} else {
							el(voobs[i].attributes[0].nodeValue).setAttribute('value', "");
						}
					}
				}						
			}
			var choobs=xmlDoc.getElementsByTagName("captcha");
			for (i = 0; i < choobs.length; i++) {
				if (el("captcha_image2")) { // we're in multi-window recruiter, push it onto the array
					if (in_array(choobs[i].firstChild.nodeValue, captchas) == false && in_array(choobs[i].firstChild.nodeValue, dunnit) == false) {
						captchas[captchas.length] = choobs[i].firstChild.nodeValue;
					}
				} else { // we're in single-window recruiter
					captchas[0] = choobs[i].firstChild.nodeValue;
				}
			}
			var choobs2=xmlDoc.getElementsByTagName("captchanext");
			for (i = 0; i < choobs2.length; i++) {
				if (choobs2[i].attributes[0].firstChild) {
					// works in IE, FF, safari
					if (el("captcha" + choobs2[i].attributes[0].firstChild.nodeValue)) {
						if (in_array(choobs2[i].firstChild.nodeValue, captchas) == false && in_array(choobs2[i].firstChild.nodeValue, dunnit) == false)
						{
							captchas[captchas.length] = choobs2[i].firstChild.nodeValue;
						}
					}
				}
				else if (choobs2[i].attributes[0]) {
					// works in Opera 9.5
					if (el("captcha" + choobs2[i].attributes[0].nodeValue)) {
						if (in_array(choobs2[i].firstChild.nodeValue, captchas) == false && in_array(choobs2[i].firstChild.nodeValue, dunnit) == false)
						{
							captchas[captchas.length] = choobs2[i].firstChild.nodeValue;
						}
					}
				}
			}

			el("number").setAttribute("value", "");
			el("altsubmit").setAttribute("disabled", "");

			var doobs=xmlDoc.getElementsByTagName("cooldown");
			for (i = 0; i < doobs.length; i++) {
				if (doobs[i].firstChild.nodeValue) {
					location.href = 'prot.php';
				}
			}

			if (captchas[0]) {
				el("captcha_image").setAttribute('src', 'http://www.lordsofaman.es/img.php?hash='+String(captchas[0]));
				el("captcha").setAttribute('value', String(captchas[0]));
			} else {
				el("captcha_image").setAttribute('src', 'http://www.lordsofaman.es/img.php?');
				el("captcha").innerHTML = '';
			}
			if (el("captcha_image2")) {
				var sechs = 0;
				for (sechs = 1; sechs < 15; sechs++) {
					if (captchas[sechs]) {
						el("captcha_image" + String(sechs+1)).setAttribute('src', 'http://www.lordsofaman.es/img.php?hash='+ (captchas[sechs]));
						el("captcha" + String(sechs+1)).innerHTML = captchas[sechs];
					}
					else
					{
						el("captcha_image" + String(sechs+1)).setAttribute('src', '');
						el("captcha" + String(sechs+1)).innerHTML = '';
					}			
				}
				document.title = captchas.length + ' images ready / Lords Of Aman / Alliance Recruiter';
			}
		}
		callQueue();
	}
}

function enter_pressed(e){
        var keycode;
        if (window.event) keycode = window.event.keyCode;
        else if (e) keycode = e.which;
        else return false;
        return (keycode == 13);
}


function openchatwin() {
            var popurl="chat/";
            winpops=window.open(popurl,"","width=750,height=550");
}

function changeSection (id,action) {

      $('#loading').show();
      $('#content').hide();
   
	   jQuery.address.value(action);
		//$('#content').load('/loading.html');

		$('td').each(function(index) {
			$(this).removeClass('menu_active');
		});
		jQuery('#content').load(action,function() {
				id='#'+id;
				$(id).addClass('menu_active');
				$('#loading').hide();
                                $('#content').width($('#container').width()-$('#left').width());
                                $('#content').fadeIn('slow');

			}
		);
	
}

function changeSectionModal (id,action, h, w) {
		//$('#content').load('/loading.html');

		$('td').each(function(index) {
			$(this).removeClass('menu_active');
		});
		$('#modalcontent').load(action,function() {
				id='#'+id;
				$(id).addClass('menu_active');
			}
		);
		if (h || w) {
		$("#modalcontent").dialog({
			height: h,
			width: w,			
			});
		}else {
			$( "#modalcontent" ).dialog();			
		}

	
}

function formSubmit (id) {

   // Interceptamos el evento submit
    $('#'+id).submit(function() {
    	  $('#loading').show();
        $('#content').hide();
        $.ajax({
            type: 'POST',
            url: $(this).attr('action'),
            data: $(this).serialize(),
            success: function(data) {
               $('#content').html(data);
               $('#loading').hide();
        			$('#content').fadeIn('slow');
            }
        })        
        return false;
    }); 

 }

function updateLocation (latitude,longitude) {
      $.ajax({
            type: 'POST',
            url: '/ajax/updatelocation.php',
			   data: 'latitude='+latitude+'&longitude='+longitude
      })        
      return false;
 

 }  


