//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		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
		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 = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function showBox(lang){
	var arrayPageSize = getPageSize();
	
	if (document.getElementById('overlay').style.display == "none"){
			document.getElementById('overlay').style.display = "block";
	}
	document.getElementById('overlay').style.height = (arrayPageSize[1] + 'px');
	
	text = file('/ajax.php', 'action=showContact&lang='+lang);
	document.getElementById('box').innerHTML = text;
	
    center(document.getElementById('box'));
    return false;
}

function hideBox(){
	if (document.getElementById('box').style.display == "block"){
			document.getElementById('box').style.display = "none";
	}
	if (document.getElementById('overlay').style.display == "block"){
			document.getElementById('overlay').style.display = "none";
	}
    return false;
}

function center(element){

    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;
    }

    element.style.display  = 'block';


    var setX = ( my_width  - element.offsetWidth  ) / 2;
    var setY = ( my_height - element.offsetHeight ) / 2 + scrollY;

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

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


}

function addClick(id){
	text = file('/ajax.php', 'action=addClick&id='+id);
}

function deadLink(id, lang){
	text = file('/ajax.php', 'action=deadLink&id='+id+'&lang='+lang);
	document.getElementById('deadlink_'+id).innerHTML = text;
}


function gotopage(){
	//window.location.replace((addr_locale+type+'/'+document.pagination.page.value);
	window.location.href = 'page-'+document.pagination.page.value+'.html';
	//return false;	
}

function parpage(){
	text = file('/ajax.php', 'action=parpage&nb='+document.parpage.nbpage.value);
	window.location.href = 'http://macdirectory.fr/index.php';
	//return false;	
}

function sendMail(lang){
	text = file('/ajax.php', 'action=sendMail&adress='+document.contact.adress.value+'&subject='+document.contact.subject.value+'&contenu='+document.contact.contenu.value+'&lang='+lang);
}

function addSite(lang){
	text = file('/ajax.php', 'action=addConfirm&name='+document.entry.name.value+'&description='+document.entry.description.value+'&url='+document.entry.url.value+'&language='+document.entry.lang.options[document.entry.lang.selectedIndex].value+'&folder='+document.entry.folder.options[document.entry.folder.selectedIndex].value+'&creationyear='+document.entry.year.options[document.entry.year.selectedIndex].value+'&lang='+lang);
	if(text != 0){
		document.getElementById('liste').innerHTML = text;
	}
	else{
		text = file('/ajax.php', 'action=showProblem&lang='+lang);
		alert(text);
	}
}

function loadAddWebsite(lang){
	text = file('/ajax.php', 'action=addWebsite&lang='+lang);
	document.getElementById('liste').innerHTML = text;
	//showBox();
}

function showContact(lang){
	text = file('/ajax.php', 'action=showContact&lang='+lang);
	document.getElementById('liste').innerHTML = text;
}

function no_accent (my_string) {
	var new_string = "";
	var pattern_accent = new Array("é", "è", "ê", "ë", "ç", "à", "â", "ä", "î", "ï", "ù", "ô", "ó", "ö");
	var pattern_replace_accent = new Array("e", "e", "e", "e", "c", "a", "a", "a", "i", "i", "u", "o", "o", "o");
	if (my_string && my_string!= "") {
		new_string = preg_replace (pattern_accent, pattern_replace_accent, my_string);
		}
	return new_string;
}

function preg_replace (array_pattern, array_pattern_replace, my_string) {
	var new_string = String (my_string);
	for (i=0; i<array_pattern.length; i++) {
		var reg_exp= RegExp(array_pattern[i], "gi");
		var val_to_replace = array_pattern_replace[i];
		new_string = new_string.replace (reg_exp, val_to_replace);
	}
	return new_string;
}

function search() { 
   //document.forms['form'].action = "http://tonsite.fr/" + form.pays.value + "-" + form.ville.value + ".html"; 
   //document.forms['form'].submit(); 


	var search = document.recherche.keyword.value;
	search = search.replace(' ', '_');
	search = no_accent(search);
	document.recherche.action = "/search/" + search + "-" + document.recherche.lang.value + "-all/";
	document.recherche.submit();
   return false; 
}

function file(fichier, data)
{
	if(window.XMLHttpRequest) // FIREFOX
	xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
	xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
	return(false);
	xhr_object.open("POST", fichier, false);
	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
	xhr_object.send(data);
	if(xhr_object.readyState == 4) return(xhr_object.responseText);
	else return(false);
}

function file_get(fichier)
 {
 if(window.XMLHttpRequest) // FIREFOX
 xhr_object = new XMLHttpRequest();
 else if(window.ActiveXObject) // IE
 xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
 else
 return(false);
 xhr_object.open("GET", fichier, false);
 xhr_object.send(null);
 if(xhr_object.readyState == 4) return(xhr_object.responseText);
 else return(false);
 }
