// Déclatation des variables globales~
try {
HTMLElement.prototype.click = function() {
var evt = this.ownerDocument.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
this.dispatchEvent(evt);
}
} catch(ex) {}

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

var headEl = null;
isSafari3 = false;
var toggled = 0;

if(window.devicePixelRatio) isSafari3 = true; //Safari 3 filtering...

// Multi Ajax
function ajaxObject(url, callbackFunction) {                                   
   var that=this;                                                    
   var updating=false;     
   var layername='';
   var waitlayer='';
   this.callback=callbackFunction || function () { };    
   
   this.update = function(passData, postMethod) {                                
      if (updating==true) { return false; }     
      updating=true;                                                 
      var WaitLayerID = (this.waitlayer == null) ? document.getElementById(this.layername) : document.getElementById(this.waitlayer); //check wether the loading is the another layer or the layer itself
	  var Layer = this.layername; //set where to put the Layer
	  var Loadingmsg = this.loadingmsg
      var AJAX = null;                                               
      if (window.XMLHttpRequest) {                                   
         AJAX=new XMLHttpRequest();                                 
      } else {                                                       
         AJAX=new ActiveXObject("Microsoft.XMLHTTP");                
      }                                                             
      if (AJAX==null) {                                              
         alert("Your browser doesn't support AJAX.");                                     
         return false                                                
      } else {
         AJAX.onreadystatechange = function() {                      
 			WaitLayerID.innerHTML = Loadingmsg; // Load the waiting message
            if (AJAX.readyState==4 || AJAX.readyState=="complete") { 
	  		   WaitLayerID.innerHTML = ''; //set the loading to '' when the information is loaded
               updating=false;                                                      
               that.callback(AJAX.responseText,AJAX.status,Layer,AJAX.responseXML);                                      
               delete AJAX;
            }  
         }                                                           
         var timestamp = new Date();                                 
         var uri=urlCall+'?'+passData+'&timestamp='+(timestamp*1);  //+'?'
         AJAX.open(postMethod, uri, true);                                
		 if ( postMethod == "POST" && passData != "") AJAX.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		 else {if ( postMethod == "GET") passData = null;}
         AJAX.send(passData);                                            
         return true;                                                
      }                                                                              
   }
      
   // This area set up on constructor calls.
  this.layername=null; //initialize where you want to put the data
  this.waitlayer=null; //initialize where you want to put the wait information
  this.loadingmsg="<img src='images/icons/ajax-loader.gif' alt='loading' title='loading'/>"; //initialise the default loading message
  var urlCall = url;                                                
}                                                                    

function processData(responseText, responseStatus, Layer) {
  responseLayer = document.getElementById(Layer);
  if (responseStatus==200) {
	  if (isSafari3) {
		appendStyle(unescape(responseText));
		responseLayer.innerHTML=unescape(responseText); 
	  }else {
          responseLayer.innerHTML=unescape(responseText); 
	  }
  } 
}

function processMap(responseText, responseStatus, Layer) {
  responseLayer = document.getElementById(Layer);
  if (responseStatus==200) {
	  if (trim(responseText).charAt(0) == "1") {
          responseLayer.innerHTML="Welcome to " + responseText.substring(2, responseText.length); 
		  setTimeout('redirect()', 2000);
	  }
	  else {
          responseLayer.innerHTML=unescape(responseText); 
	  }
  } 
}

function redirect() {
	 //window.location.reload();
	 window.location = 'main.php';
}

function populate(url, itemid, destination) {
	var methode = "GET";
	var data = "itemid="+itemid;
	var myExec = new ajaxObject(url, processData);
	myExec.layername=destination;
	myExec.loadingmsg = "<img src='images/icons/ajax-loader.gif' alt='loading' title='loading' id='loading' style='margin-top:88px;'/>";
	myExec.update(data, methode);
}

function check(url, cityid, destination) {
	var methode = "GET";
	var data = "cityid="+cityid;
	var myExec = new ajaxObject(url, processMap);
	var ajaxalert = document.getElementById(destination);
	ajaxalert.style.display= "block";
	myExec.layername=destination;
	myExec.loadingmsg = "";
	myExec.update(data, methode);
}

function getNodeValue(obj,tag)
{
	return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}


function appendStyle(response) {
    // find all styles in the string
    var styleFragRegex = '<style[^>]*>([\u0001-\uFFFF]*?)</style>';
    var matchAll = new RegExp(styleFragRegex, 'img');
    var matchOne = new RegExp(styleFragRegex, 'im');
    var styles = (response.match(matchAll) || []).map(function(tagMatch) {  
                                                                            return (tagMatch.match(matchOne) || ['', ''])[1];
                                                                        });

    // add all found style blocks to the HEAD element.
    for (i = 0; i < styles.length; i++) {
        if (!headEl)
        {  
            headEl = document.getElementsByTagName('head')[0];
            if (!headEl)
            {  
                return;
            }
        }
        var newStyleEl = document.createElement('style');
        newStyleEl.type = 'text/css';
        //if (this.browser == 'ie')
        //{  
        //    newStyleEl.styleSheet.cssText = styles[i];
        //}
        //else
        //{  
            var cssDefinitionsEl = document.createTextNode(styles[i]);
            newStyleEl.appendChild(cssDefinitionsEl);
        //}
        headEl.appendChild(newStyleEl);
    }
}


function checkvalue(chkbox) {
if (document.getElementById("human"+chkbox+"cb").checked == 1) {
document.getElementById("human"+chkbox+"cb").checked = 0;
document.getElementById("image"+(chkbox+1)).style.border = "none";
}
else {
document.getElementById("human"+chkbox+"cb").checked = 1;
document.getElementById("image"+(chkbox+1)).style.border = "2px solid #ff2222";
}

}

function feecalculator(price) {
document.getElementById('Fee').innerHTML = '$ ' + number_format(Math.round(price.value*0.1));
}

function number_format(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function toggleMe(what)
{
safeinfo = document.getElementById('clanph');
infobar = document.getElementById('announcement');

switch(what) {
case "inv":
url2load = "_claninvit.php";
break;

case "rss":
url2load = "_safeinfos.php";
break;

case "war":
url2load = "_warinfos.php";
break;	
	
}

if(toggled == 0)
{
safeinfo.style.display = "block";
infobar.style.display = "none";

var updateinfobar = new ajaxObject(url2load, processData);
updateinfobar.layername="clanph";
updateinfobar.loadingmsg = "";
updateinfobar.update("", "GET");
toggled = 1;
}
else
{
safeinfo.style.display = "none";
infobar.style.display = "block";
toggled = 0;
}
}


function getPageDim() {
	        
	     var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			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
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				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 = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}


function getOffsetScroll() {
    return returnOffset(
      window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
      window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop);
  }

function returnOffset(l, t) {
  var result = [l, t];
  result.left = l;
  result.top = t;
  return result;
}