var minus=new Image();
minus.src="../images/minus.gif";
var plus=new Image();
plus.src="../images/signPlus.png";

var add=new Image(15,15);
add.src="../images/icon_add.gif";
var edit=new Image(15,15);
edit.src="../images/icon_edit.gif";
var del=new Image(15,15);
del.src="../images/icon_del.gif";
var preview=new Image(15,15);
preview.src="../images/icon_preview.gif";
var save=new Image(15,15);
save.src="../images/icon_save.gif";

function opacity(img,op)
{
	img.style.filter='alpha(style=0,opacity='+op+')';
}

function opacity_out(img)
{
	img.className='trans';
}

function opacity_over(img)
{
	img.className='trans_over';
}

function ShowLayer(d, ic)
{
	
	var myDiv = document.getElementById(d);
	var myIcon = document.getElementById(ic);
	if(myDiv)
	{
		if(myDiv.style.display=='none')
		{
			myDiv.style.display='block';
			myIcon.src=minus.src;
		}
		else
		{
			myDiv.style.display='none';
			myIcon.src=plus.src;
		}
	}
}


function openwindow(URL, WinName, width, height, external){


	if(height=='' || !height)
	height=screen.height/2
	height_factor = ((screen.height/100)*90)
	if(height>=height_factor)
		height=height_factor;

	if(width=='' || !width)
		width=screen.width/2
	width_factor = ((screen.width/100)*98)
	if(width>=width_factor)
		width=width_factor;

	var top=((screen.height-height)/2)-(screen.height/20);
	if(top<1) top=1;

	var left=(screen.width - width) / 2 ;
	if(left<1) left=1;

	win=window.open(URL, WinName, 'toolbar=no,location=no,directories=no,status=0,menubar=no,scrollbars=1,resizable=1,copyhistory=no,width='+width+',height='+height+',top='+top+',left='+left+'');
	if(!external)
	{
		win.resizeTo(width, height);
		win.moveTo(left, top);
	}
	win.focus();
	
	return win;

}


function MouseOver(id, len)
{
	if ( row = document.getElementById(id) )
	{
		count = len ? len : row.cells.length;
		for( curr_cell = 0; curr_cell < count; curr_cell++ ) 
		{
			row.cells[curr_cell].className='Active';
		}
	}
}

function MouseOut(id, len)
{
	if ( row = document.getElementById(id) )
	{
		count = len ? len : row.cells.length;
		for( curr_cell = 0; curr_cell < count; curr_cell++ ) 
		{
			row.cells[curr_cell].className='InActive';
		}
	}
}



function Calendar(){
	var height=250;
	var width=250;
	var top=(screen.height - height) / 3 ;
	var left=(screen.width - width) / 2 ;
	win=window.open('/shared/Calender.php', 'Calendar','toolbar=no,location=0,directories=no,status=0,menubar=no,scrollbars=no,resizable=0,copyhistory=no,width='+width+',height='+height+',top='+top+',left='+left+'');
	win.focus();
}

/*function CheckOnlyNumbersKP()
{
	if ( event.keyCode < 46 || event.keyCode > 57 || event.keyCode == 47 ) 
	{
		event.returnValue = false;
	}
}*/

function CheckOnlyNumbers(field)
{
	for( i=0; i<46; i++ )
	{
		str = String.fromCharCode(i);
		if(field.value.indexOf(str)>=0)
		{
			field.value='';
			return;
			
		}
	}
	
	str = String.fromCharCode(47);
	if (field.value.indexOf(str)>=0)
	{
		field.value='';
		return;
	}
		
	for( i=58; i<=255; i++ )
	{
		str = String.fromCharCode(i);
		if (field.value.indexOf(str)>=0)
		{
			field.value='';
			return;
		}
	}		
}

function CheckEmail(src) {
     var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
     var regex = new RegExp(emailReg);
     return regex.test(src);
}

function ShohImage(field, img, src)
{
	var bool=false, true_src, strImg = field.value.substr(field.value.length - 4, 4);
	if (src.length>=4)
	{
		var newsrc=src.substr(src.length - 4, 4);

		if (newsrc.toLowerCase()==".jpg" || newsrc.toLowerCase()==".gif" || newsrc.toLowerCase()==".png")
			true_src=true;
		else
			true_src=false;		
	}
	
	if(strImg.toLowerCase()==".jpg" || strImg.toLowerCase()==".gif" || strImg.toLowerCase()==".png")
	{
		bool=true
		img.src=field.value;
		img.style.visibility='visible';
	}
	else
	{
		img.src=src;
		img.style.visibility = true_src ? 'visible' : 'hidden';
	}
}

function HidePic(cb, img, input, src)
{
	if(cb.checked==true)
	{
//		input.value='';
		input.disabled=true;
		img.style.visibility='hidden';
	}
	else
	{
		input.disabled=false;
		ShohImage(input, img, src)
	//	img.style.visibility='visible';
	}
}


function MakePrice(o, f)
{
	CheckOnlyNumbers(o);

	var p=0;

	if(!isNaN(parseFloat(f.PropValue.value)))
		p+=parseFloat(f.PropValue.value);

	if(!isNaN(parseFloat(f.SelfFortune.value)))
		p+=parseFloat(f.SelfFortune.value);

	if(!isNaN(parseFloat(f.Mortgage.value)))
		p+=parseFloat(f.Mortgage.value);

	if(!isNaN(parseFloat(f.Budget.value)))
		p+=parseFloat(f.Budget.value);
	if(p)
		f.Price.value=AddComa(String(p));
	else
		f.Price.value='0';
}


function AddComa(str)
{
	var ii=0, s="", ss="";
	for(i=(str.length-1);i>=0;i--)
	{
		ii++;
		s+=String(str.substr(i,1));
		if(ii%3==0)
			s+=",";
	}
	for(i=(s.length-1);i>=0;i--)
		ss+=s.substr(i,1);

	if(ss.substr(0,1)==",")
		ss = ss.substr(1,ss.length);
	return ss;
}

function reloadopener()
{
	var o = window.opener;
	if(o && !o.closed)
	{
		o.focus();
		o.location.reload();
	}
	window.close();
}	

function focusopener()
{
	var o = window.opener;
	if(o && !o.closed)
	{
		o.focus();
	}
	window.close();
}

function Global_Reset_Form(ThisForm)
{
	ThisForm.reset();
	form_length = ThisForm.elements.length;
	for( el = 0; el < form_length ; el++ )
	{
		FormInput = ThisForm.elements[el];
		switch( FormInput.type )
		{
			case "select-one":
				FormInput.options[0].selected = true;
				break;

			case "select-multiple":
				select_length = FormInput.length;
				for( opt = 0; opt < select_length; opt++ )
				{
					FormInput.options[opt].selected = false
				}
				break;

			case "radio":
			case "checkbox":
				FormInput.checked = false;
				break;

			case "text":
			case "textarea":
				FormInput.value = "";
				break;
		}
	}	
}

function ResetForm(ThisForm)
{
	Global_Reset_Form(ThisForm);
}

function Combo(f, src, dest)
{
	var len=src.length, bool=false, opt=0;
	if(len)
	{
		while (!bool && opt<dest.options.length)
		{
			if(src.toLowerCase()==dest.options[opt].text.substr(0,len).toLowerCase())
			{
				dest.options[opt].selected=true;
				bool=true;
				break;
			}
			opt++;
		}
	}

	if(!bool)
	{
		src="";
	}

	return;

}

function opacity(img,op)
{
	img.style.filter='alpha(style=0,opacity='+op+')';
}

function ChangeOrder(num, lorder)
{
	document.searchform.order.value=num;
	if(num!=lorder)
	{
	document.searchform.lorder.value='';
	document.searchform.ordcount.value='';
	}
	
	if(window.OnSubmitAdd)
	{
		OnSubmitAdd();
	}
	
	return document.searchform.submit();
}


function disAll(f) {
	//alert("inside");
	for( j = 0; j < f.length; j++ )
	{
		f[j].disabled=true;
	}
}
function AddZero(Number)
{
	Number = String(Number);
	if(Number.length < 2)
	{
		Number = "0" + Number;
	}
		
	return Number;
}

function VerifyDate(f, y, m, d)
{
	var NewDay = Math.abs(f[d].value);
	var NewMonth = Math.abs(f[m].value)-1;
	var NewYear = Math.abs(f[y].value);
	
	f[d].options.length=0;
	
	for( i = 1; i <= 31; i++ )
	{
		CheckDate = new Date(NewYear, NewMonth, i)
		if ( CheckDate.getDate() != i )
		{
			break;
		}
		f[d].options.length++;
		f[d].options[i-1].value = AddZero(i)
		f[d].options[i-1].text = AddZero(i)
		if( i == NewDay )
		{	
			f[d].options[i-1].selected = true;
			NewDay = i;
		}
	}
	if( NewDay > (i - 1) )
	{
		NewDay = (i - 1);
		f[d].value = AddZero(NewDay);
	}
}

function CompareDates(f, y1, m1, d1, y2, m2, d2, src)
{
	var mydate1 = new Date(f[y1].value, f[m1].value, f[d1].value, 0, 0, 0);
	var mydate2 = new Date(f[y2].value, f[m2].value, f[d2].value, 0, 0, 0);
	
	if( mydate1.valueOf() > mydate2.valueOf() )
	{
		switch(src)
		{
			case 1:
			VerifyDate(f, y1, m1, d1);
			VerifyDate(f, y1, m1, d2);
			
			f[y2].value = f[y1].value;
			f[m2].value = f[m1].value;
			f[d2].value = f[d1].value;
			return;

			case 2:
			VerifyDate(f, y2, m2, d2);
			VerifyDate(f, y2, m2, d1);
			
			f[y1].value = f[y2].value;
			f[m1].value = f[m2].value;
			f[d1].value = f[d2].value;	
			return;
		}		
	}
	else if( src == 1 )
	{
		VerifyDate(f, y1, m1, d1);
		return;
	}
	else
	{
		VerifyDate(f, y2, m2, d2);			
		return;
	}
}

function ChangeDateFieldsStatus( f, Select )
{
	for ( i = 0; i < f.elements.length; i++ )
	{
		e = f.elements[i];
		if( e.type == "select-one" )
		{
			switch ( e.name )
			{
				case "y1":
				case "m1":
				case "d1":
				case "y2":
				case "m2":
				case "d2":
				e.disabled = Select ? true : false;
				break;				
			}			
		}
	}	
	f.DateType.disabled =  Select ? false : true;
}


function CurDate(formHandler, DateField)
{
	var d = new Date();
	var day = AddZero(d.getDate());
	var month = AddZero(d.getMonth()+1);
	var year = d.getYear();
	
	DateField = formHandler[DateField];
	DateField.value = String(year + "-" + month + "-" + day);
	
	return String(day + "/" + month + "/" + year);
}

function CheckFloat(Number)
{
	return (parseFloat(Number) == Number);
}

function CheckInt(Number)
{
	return (parseInt(Number) == Number);
}

function openwindow2(URL, WinName, width, height, resizable, scrollbars)
{
	if(height=='' || !height) {height=screen.height/2;}
	
	height_factor = ((screen.height/100)*90)
	
	if(height>=height_factor) {height=height_factor;}

	if(width=='' || !width) {width=screen.width/2;}
	
	width_factor = ((screen.width/100)*98)
	
	if(width>=width_factor) {width=width_factor;}

	var top=((screen.height-height)/2)-(screen.height/20);
	if(top<1) top=1;

	var left=(screen.width - width) / 2 ;
	if(left<1) left=1;
	
	if(resizable=='' || !resizable) { resizable=0;}
	if(scrollbars=='' || !scrollbars) { scrollbars=0;}
	
	win=window.open(URL, WinName, 'toolbar=no,location=no,directories=no,status=0,menubar=no,scrollbars='+scrollbars+',resizable='+resizable+',copyhistory=no,width='+width+',height='+height+',top='+top+',left='+left+'');
	
	win.moveTo(left, top);
	win.focus();
	
	return win;
}

function banner_click(param)
{
	// praram=dealID_location_areaID_real_typeID
	openwindow('banner_redir.php?param='+param, 'SiteShow', 795, 580)
}

function trim(string)
{
return string.replace(/(^\s+)|(\s+$)/g, "");
}



//Site's options

currentCatName = null;
currentImg = 1;

function ajaxDatas(filename, action, id_dom){
			var xhr_object = null; 
				 
			if(window.XMLHttpRequest) // Firefox 
			   xhr_object = new XMLHttpRequest(); 
			else if(window.ActiveXObject) // Internet Explorer 
			   xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
			else { // XMLHttpRequest non supporté par le navigateur 
			   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
			   return; 
			} 
				 
			xhr_object.open("GET", filename, true); 
				 
			xhr_object.onreadystatechange = function() { 
				if(id_dom != '' && action != 3){
					switch(action){
							case 1: document.getElementById(id_dom).innerHTML = '<img src="Loader.gif" alt="Chargement" title="Chargement" width=15 height=15 border=0 />'; break;
							default: document.getElementById(id_dom).innerHTML = '<img src="Loader.gif" alt="Chargement" title="Chargement" width=15 height=15 border=0 />'; break;
					}
				}
			   if(xhr_object.readyState == 4) {
						switch(action){
							case 1: 	if(document.getElementById(id_dom)){
											document.getElementById(id_dom).innerHTML = xhr_object.responseText; 
										}
										break;
							case 2:		document.getElementById(id_dom).innerHTML = xhr_object.responseText; 
										var filename = "ajax.php?t=3";
										ajaxDatas(filename, 1, 'categories');
										break;
							case 3: 	id_dom.innerHTML = xhr_object.responseText; break;
							case 11:	eval(xhr_object.responseText); break;
						}
					}
			}
				 
			xhr_object.send(null);
		return false;
}

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

function openTrad(arg){
	if(arg == 1){
		document.getElementById('cat_trad').style.display = 'block';
		document.getElementById('tab_trad').style.display = 'none';
	}
	if(arg == 2){
		document.getElementById('cat_trad').style.display = 'none';
		document.getElementById('tab_trad').style.display = 'block';
	}
}

function addCategory(){
	if(!document.getElementById('categoryAdder')){
		var catContainer = document.createElement('div');
		catContainer.setAttribute('id', 'categoryAdder');
		catContainer.style.width = '300px';
		catContainer.style.height = '115px';
		catContainer.style.left = (largScreen/2)-150+'px';
		catContainer.style.top = '100px';
		catContainer.style.position = 'fixed';
		catContainer.style.backgroundColor = 'white';
		catContainer.style.border = '1px solid black';
		
		document.body.appendChild(catContainer);
		var filename = "ajax.php?t=1";
		ajaxDatas(filename, 1, 'categoryAdder');
	}
}

function closeAddCategory(img_closer){
	img_closer.parentNode.parentNode.parentNode.parentNode.removeChild(img_closer.parentNode.parentNode.parentNode);
}

function addCatSubmit(){
	if(document.getElementById('new_category').value != ''){
		var new_cat = document.getElementById('new_category').value;
		var filename = "ajax.php?t=2&cat="+new_cat;
		ajaxDatas(filename, 2, 'cat_response');
		return false;
	}
	else{
		alert('Nom invalide');
		return false;
	}
}

function cat_modify(cat_id, img_modify){
	var current_name = img_modify.parentNode.parentNode.getElementsByTagName('td')[1].textContent;
	currentCatName = current_name;
	img_modify.parentNode.parentNode.getElementsByTagName('td')[0].innerHTML = '<input type="text" name="new_cat_name" id="new_cat_name" value="'+current_name+'" />';
	img_modify.onclick = function(){
		save_new_cat(cat_id, this);
	}
}

function save_new_cat(cat_id, img_modify){
	var new_name = img_modify.parentNode.parentNode.getElementsByTagName('td')[1].getElementsByTagName('input')[0].value;
	if(new_name != ''){
		img_modify.parentNode.parentNode.getElementsByTagName('td')[1].innerHTML = new_name;
		img_modify.onclick = function(){
			cat_modify(cat_id, this);
		}
		
		var filename = "ajax.php?t=4&cat="+cat_id+"&name="+escape(new_name);
		ajaxDatas(filename, 1, '');
	}
	else{
		img_modify.parentNode.parentNode.getElementsByTagName('td')[1].innerHTML = newCatName;
		img_modify.onclick = function(){
			cat_modify(cat_id, this);
		}
	}
	currentCatName = null;
}

function cat_active(cat_id, img_active){
	var current_bg = img_active.parentNode.parentNode.getAttribute('bgcolor');
	if(current_bg == 'pink'){
		img_active.parentNode.parentNode.bgColor = '#77C15A';
		var actif = 1;
	}
	else{
		img_active.parentNode.parentNode.bgColor = 'pink';
		var actif = 0;
	}

	var filename = "ajax.php?t=5&cat="+cat_id+"&active="+actif;
	ajaxDatas(filename, 1, '');
}

function reorder(selection){
	if(document.getElementById('catables') || document.getElementById('tabtables')){
		var all_lines = selection.parentNode.parentNode.parentNode.getElementsByTagName('tr');
		var current_table = selection.parentNode.parentNode.parentNode;
		var current_line = selection.parentNode.parentNode;
		var togo = selection.options[selection.selectedIndex].textContent;
		var newTable = new Array();
		var new_current_order = 1;
		var new_togo_order = 1;
		var new_current_id = 0;
		var new_togo_id = 0;
		
		for(var i=0; i<all_lines.length; i++){
			if(i == togo){
				newTable[i] = current_line;
			}
			else if(all_lines[i] == current_line){
				newTable[i] = all_lines[togo];
			}
			else{
				newTable[i] = all_lines[i];
			}
		}

		current_table.innerHTML = '';
		for(var i=0; i<newTable.length; i++){
			current_table.appendChild(newTable[i]);
		}
		
		for(var i=1; i<newTable.length; i++){
			newTable[i].getElementsByTagName('select')[0].selectedIndex = i-1;
		}
	}
}

function cat_delete(cat_id, img_delete){
	if(confirm('Etes-vous sur(e) de vouloir supprimer cette cat&eacute;gorie ?')){
		img_delete.parentNode.parentNode.parentNode.removeChild(img_delete.parentNode.parentNode);
		
		var filename = "ajax.php?t=7&cat="+cat_id;
		ajaxDatas(filename, 1, '');
	}
}

function addTab(){
	if(!document.getElementById('tabAdder')){
		var tabContainer = document.createElement('div');
		tabContainer.setAttribute('id', 'tabAdder');
		tabContainer.style.width = '400px';
		tabContainer.style.height = '200px';
		tabContainer.style.left = (largScreen/2)-200+'px';
		tabContainer.style.top = '100px';
		tabContainer.style.position = 'fixed';
		tabContainer.style.backgroundColor = 'white';
		tabContainer.style.border = '1px solid black';
		
		document.body.appendChild(tabContainer);
		var filename = "ajax.php?t=8";
		ajaxDatas(filename, 1, 'tabAdder');
	}
}

function showPics(img_path){

}

function hidePics(){

}

function tab_active(tab_id, img_active){
	var current_bg = img_active.parentNode.parentNode.getAttribute('bgcolor');
	if(current_bg == 'pink'){
		img_active.parentNode.parentNode.bgColor = '#77C15A';
		var actif = 1;
	}
	else{
		img_active.parentNode.parentNode.bgColor = 'pink';
		var actif = 0;
	}

	var filename = "ajax.php?t=9&tab="+tab_id+"&active="+actif;
	ajaxDatas(filename, 1, '');
}

function tab_modify(tab_id, img_modify){
	var cols = img_modify.parentNode.parentNode.getElementsByTagName('td');
	var cat = cols[1].textContent;
	var tab = cols[2].textContent;
	
	cols[2].innerHTML = '<input type="text" name="new_tab_name" id="new_tab_name" value="'+tab+'" />';
	
	img_modify.onclick = function(){
		save_tab_modification(tab_id, this);
	}
	
	var filename = "ajax.php?t=10";
	ajaxDatas(filename, 3, cols[1]);
}

function link_modifier(tab_id, img_modify){
	var container = document.createElement('div');
	container.style.height = '150px';
	container.style.width = '300px';
	container.style.top = '100px';
	container.style.position = 'fixed';
	container.style.backgroundColor = 'white';
	container.style.padding = '5px';
	container.style.border = '1px solid black';
	container.style.left = (largScreen/2)-150+'px';
	container.setAttribute('id', 'link_modification');
	
	document.body.appendChild(container);
	var filename = "ajax.php?t=13&id="+tab_id;
	ajaxDatas(filename, 1, 'link_modification');
}

function save_tab_modification(tab_id, img_modify){
	var cols = img_modify.parentNode.parentNode.getElementsByTagName('td');
	var new_cat = cols[1].getElementsByTagName('select')[0].options[cols[1].getElementsByTagName('select')[0].selectedIndex].value;
	var new_cat_name = cols[1].getElementsByTagName('select')[0].options[cols[1].getElementsByTagName('select')[0].selectedIndex].textContent;
	var new_name = cols[2].getElementsByTagName('input')[0].value;
	
	cols[1].innerHTML = '';
	cols[2].innerHTML = '';
	
	cols[1].textContent = new_cat_name;
	cols[2].textContent = new_name;
	
	img_modify.onclick = function(){
		tab_modify(tab_id, this);
	}
	
	var filename = "ajax.php?t=11&tab="+tab_id+"&cat="+new_cat+"&name="+escape(new_name);
	ajaxDatas(filename, 1, '');
}

function tab_delete(tab_id, img_delete){
	if(confirm('Etes-vous sur(e) de vouloir supprimer cet onglet ?')){
		img_delete.parentNode.parentNode.parentNode.removeChild(img_delete.parentNode.parentNode);
	
		var filename = "ajax.php?t=12&id="+tab_id;
		ajaxDatas(filename, 1, '');
	}
}

function home_slider_init(){
if(document.getElementById('slider_container')){
	if(currentImg > 6){
		currentImg = 1;
	}
	else if(!Slider_imgs[currentImg]){
		currentImg = 1;
	}
	else{
		if(Slider_imgs[currentImg] != ''){
			document.getElementById('slider_container').src = Slider_imgs[currentImg];
		}
		currentImg++;
	}
	setTimeout('home_slider_init();', 5000);
}
}

window.onload = function(){
	if(navigator.appName == 'Microsoft Internet Explorer'){
		largScreen = document.documentElement.clientWidth;
		hautScreen = document.documentElement.clientHeight;
	}
	else{
		largScreen = window.innerWidth;
		hautScreen = window.innerHeight;
	}
}
