
function check_email(emailStr) 
{
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   return false
}

// If we've gotten this far, everything's valid!
return true;
}

function isValidURL(strUrl)
{
        var RegexUrl = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
        return RegexUrl.test(strUrl);
}

function IsNumeric(sText)
//vérifie si la chaine envoyée est de type numérique
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
	if(sText.length==0)
		return false;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

function CommonVoirAnnonceurImmobilier() 
{
	offsety=0;
	try
	{
		offsety = document.body.scrollTop;
		if( offsety==0 || offsety==undefined )
			offsety = window.pageYOffset;
		if( offsety==0 || offsety==undefined )
			offsety = document.documentElement.scrollTop;
	}
	catch(err)
	{
	offsety=0;
	}  
   document.getElementById("immobilier_annonceur").style.top=(offsety)+10;
   
}

function CommonCreateXMLHttpRequest() {
   try { return new XMLHttpRequest(); } catch(e) {}
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   return null;
 }


function get_tel(p,cont,kt)
{
	document.getElementById("ctl_phone").innerHTML='<img src="/images/sablier.gif" >';
	
	request = CommonCreateXMLHttpRequest(); 
	if(request==null)
		return ;
	
	request.open("POST", "/get_tel.php", true);
   	request.onreadystatechange = function() 
	{
      if(request.readyState == 4) {eval(request.responseText); }
  	}

   	var param = "p="+escape(p)+"&cont="+escape(cont)+"&kt="+escape(kt);
 	try {request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); } catch (e) {}
 	try {request.setRequestHeader("Content-length", param.length); } catch (e) {}
 	try {request.setRequestHeader("Connection", "close"); } catch (e) {}
	request.send(param);
}

function check_ValidDomainURL(url) {
	var regexp = /^http(s)?:\/\/[\-_a-zA-Z0-9]+\.[\-_a-zA-Z0-9]+\.[\-_a-zA-Z]{2,4}(\/)?$/;
	return regexp.test(url);
}
function check_ValidURL(url) {
	var regexp = /^http(s)?:\/\/[\.a-zA-Z0-9#!:.?+=_&%@!\-\/]+$/
	return regexp.test(url);
}
function check_ContainCode(txt) {
	var regexp = /\[[ /a-zA-Z0-9#!:.?+=_&%@!\-\/]+\]/
	if(regexp.test(txt)) return true;
	
	regexp = /<[ /a-zA-Z0-9#!:.?+=_&%@!\-\/]+>/
	if(regexp.test(txt)) return true;
	return false;
}

function go2url(url)
{
	window.location=url;
}
function openblankurl(url)
{
window.open (url,'_blank');
}

function openpopurl(url, winName)
{
window.open (url, winName, config='height=400, width=600, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no');
}
function openpopmeilleurtaux()
{
window.open ('https://credit-immobilier.meilleurtaux.com/form-immo/index.php?IDAPPORTEUR=REPIMMO', '_blank', config='height=700, width=700, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no');
}
function openpopMTmondossier()
{
window.open ('https://www.meilleurtaux.com/vos-propositions/login.do','mondossier','width=800,height=600,resizable=yes,toolbar=no,location=no,menubar=no,statusbar=yes,scrollbars=yes');
}
function openpopMTfraisnotaire()
{
window.open ('http://www.meilleurtaux.com/calculatrice/frais_notaire/index.php?ID_APPORTEUR=REPIMMO', '_blank', config='height=720, width=600, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no');
}

function openpopsiteurl(idpro)
{
window.open ('/site_agence.php?p_id_produit='+idpro,'siteagent','width=800,height=600,resizable=yes,toolbar=no,location=no,menubar=no,statusbar=yes,scrollbars=yes');
}
function openpopsiteagenceurl(ida)
{
window.open ('/site_agence.php?p_id_agence='+ida,'siteagent','width=800,height=600,resizable=yes,toolbar=no,location=no,menubar=no,statusbar=yes,scrollbars=yes');
}
function openpopcontactpro(idpro,idag)
{
var targ="immobilier";
if (navigator.userAgent.indexOf("Firefox")!=-1)
	targ="_blank";
window.open ('/contact_agent_pop.php?p_id_produit='+idpro+'&p_id_agent='+idag,targ,'width=700,height=610,resizable=yes,toolbar=no,location=no,menubar=no,statusbar=yes,scrollbars=yes');
}
function openpopcontactabus(idpro,idag)
{
var targ="immobilier";
if (navigator.userAgent.indexOf("Firefox")!=-1)
	targ="_blank";
window.open ('/contact_agent_pop.php?p_id_produit='+idpro+'&p_id_agent='+idag+'&p_abus=1',targ,'width=700,height=610,resizable=yes,toolbar=no,location=no,menubar=no,statusbar=yes,scrollbars=yes');
}
function openpopconnectfb()
{
window.open('/facebook/connect.php','fbConnect',confid='width=550, height=320, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no');
}

function wnd_myspace(purl,pname,pdescription,pprix)
{
    var targetUrl = 'http://www.myspace.com/index.cfm?fuseaction=postto&t=' + encodeURIComponent(pname) 
		+ '&c=' + encodeURIComponent(pdescription.substring(0,500) + " Prix : " + pprix ) 
		+ '&u=' + encodeURIComponent(purl);
    window.open(targetUrl, 'ptm', 'height=450,width=440,resizable=yes').focus();
}

function wnd_facebook(purl,pname,pdescription,pprix)
{
    var targetUrl = 'http://www.facebook.com/sharer.php?u='+ encodeURIComponent(purl);
    window.open(targetUrl, 'faceb', 'width=650, height=500, toolbar=no, menubar=no, scrollbars=auto, resizable=yes, location=no, directories=no, status=no').focus();
}

function wnd_twitter(purl,pname,pdescription,pprix)
{
	var targetUrl = 'http://twitter.com/share?url='+encodeURIComponent(purl)
	+'&text='+ encodeURIComponent(pname + " - " + pprix ) ;
    window.open(targetUrl, 'twitter', 'height=520,width=570,resizable=yes').focus();
}
function wnd_linkedin(purl,pname,pdescription,pprix)
{
    var targetUrl = 'http://www.linkedin.com/shareArticle?mini=true&url='
		+  encodeURIComponent(purl)
		+ '&title=' + encodeURIComponent(pname) 
		+ '&summary=' + encodeURIComponent(pdescription.substring(0,500)  + " Prix : " + pprix );
    window.open(targetUrl, 'linkedin', 'height=520,width=570,resizable=yes').focus();
}
function wnd_delicious(purl,pname,pdescription,pprix)
{
    var targetUrl = 'http://www.delicious.com/save?v=5&noui&jump=close&url=' + encodeURIComponent(purl) 
		+ '&title=' + encodeURIComponent(pname) ;
    window.open(targetUrl, 'delicious', 'height=550,width=650,resizable=yes').focus();
}
function wnd_messenger(purl,pname,pdescription,pprix,pphoto)
{
    var targetUrl = 'http://profile.live.com/badge?url=' + encodeURIComponent(purl) 
		+ '&title=' + encodeURIComponent(pname) 
		+ '&description=' + encodeURIComponent(pdescription.substring(0,480)  + " Prix : " + pprix )
		+ '&screenshot=' + encodeURIComponent(pphoto)  ;
		
    window.open(targetUrl, 'messenger', 'height=550,width=650,resizable=yes').focus();
}
function wnd_mailami(purl,pname,pdescription,pprix)
{
	var mailbody= "Bonjour,%0A%0Avoici une annonce trouv\351e sur http://www.repimmo.com %0A%0A"+(decodeURIComponent(pdescription))+"%0A%0APrix :"+encodeURIComponent(pprix)+"%0A%0AVoir l'annonce : "+encodeURIComponent(purl)+"%0A%0ACordialement";
   var targetUrl = 'mailto:?subject='+ decodeURIComponent(pname) +'&body=' + mailbody ;
	window.location=targetUrl;
}

function wnd_googlebookmark(purl,pname,pdescription,pprix)
{
    var targetUrl = 'http://www.google.com/bookmarks/mark?op=edit&bkmk=' + encodeURIComponent(purl) 
		+ '&title=' + encodeURIComponent(pname) ;
    window.open(targetUrl, 'goog', 'height=550,width=650,resizable=yes').focus();
}


function departement_change(cpselectobj,paysSelect,villeSelect,sablier)
{
	cpselectobj = cpselectobj || 0;
	paysSelect = paysSelect || 0;
	
	if(!paysSelect)
		pays=1;
	else	
		pays=paysSelect.value;
	cp=cpselectobj.value;

	if(pays==1 && isNaN(cp))
		return;	
		
	var newOpt = new Option("-------------------", "");
    villeSelect.options.length = 0; 
	villeSelect.options[0] = newOpt;	
	
	request = CommonCreateXMLHttpRequest(); 
	if(request==null)
		return ;
 	request.onreadystatechange = function()
 	{ 
		if(request.readyState  == 4)
    	{
			if(request.status  == 200) 
			{
			var jsondata=eval("("+request.responseText+")");
				for (var key in jsondata) 
				{
				valeur=jsondata[key];
				var newOpt = new Option(valeur, valeur);
				var selLength = villeSelect.length;
				villeSelect.options[selLength] = newOpt;
				}
				if(sablier) sablier.style.display = 'none';
			}
		}
	}; 

	var param = "cp="+cp+"&pays="+escape(pays);
	
	request.open('POST', "/gvdepartement"+".p"+"hp", true); 
 	try {request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); } catch (e) {}
 	try {request.setRequestHeader("Content-length", param.length); } catch (e) {}
 	try {request.setRequestHeader("Connection", "close"); } catch (e) {}
	
	if(sablier) sablier.style.display = 'inline-block';
 	request.send(param); 
 	return ;
}

function pays_change(paysselectobj,depSelect,villeSelect,sablier)
{
	pays=paysselectobj.value;
	
	var newOpt = new Option("-------------------", "");
    depSelect.options.length = 0; 
	depSelect.options[0] = newOpt;	

	var newOpt2 = new Option("-------------------", "");
    villeSelect.options.length = 0; 
	villeSelect.options[0] = newOpt2;	

	request = CommonCreateXMLHttpRequest(); 
	if(request==null)
		return ;
 	request.onreadystatechange = function()
 	{ 
		if(request.readyState  == 4)
    	{
			if(request.status  == 200) 
			{
			var jsondata=eval("("+request.responseText+")");
				for (var key in jsondata) 
				{
				valeur=jsondata[key];
				if(pays!=8)
					var newOpt = new Option(key+" - "+valeur,key);
				else	
					var newOpt = new Option(valeur,key);
				var selLength = depSelect.length;
				depSelect.options[selLength] = newOpt;
				}
			}
			if(sablier) sablier.style.display = 'none';
			
		}
	}; 

	var param = "pays="+escape(pays);
	request.open('POST', "/gdeppays"+".p"+"hp", true); 
 	try {request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); } catch (e) {}
 	try {request.setRequestHeader("Content-length", param.length); } catch (e) {}
 	try {request.setRequestHeader("Connection", "close"); } catch (e) {}

	if(sablier) sablier.style.display = 'inline-block';
	
 	request.send(param); 
 	return ;
}


function createSocialLnk(obj,pid)
{
		if(obj.html().length < 1)
		{
			if(last_social_obj) last_social_obj.html("");
			last_social_obj=obj;
			obj.append('<div class="socialize_facebook" title="Partager cette annonce sur Facebook" onclick="socialise_facebook('+pid+');"></div><div class="socialize_twitter" title="Twitter cette annonce" onclick="socialise_twitter('+pid+');"></div><div class="socialize_myspace" title="Partager cette annonce sur MySpace" onclick="socialise_myspace('+pid+');" ></div><div class="socialize_linkedin" title="Partager cette annonce sur LinkedIn" onclick="socialise_linkedin('+pid+');" ></div><div class="socialize_delicious" title="Partager cette annonce sur Delicious" onclick="socialise_delicious('+pid+');"></div><div class="socialize_googbm" title="Ajouter cette annonce \340 mes favoris google" onclick="socialize_googlebookmark('+pid+');" ></div><div class="socialize_favori" title="Ajouter cette annonce \340 mes favoris" onclick="socialise_favori('+pid+');" ></div><div class="socialize_mailami" title="Envoyer cette annonce par mail \340 un ami" onclick="socialize_mailami('+pid+');"></div>');
		}
}



function CenterObj(obj1,obj2)
{
	var h1 = obj1.height();
	var w1 = obj1.width();
	var h2 = obj2.height();
	var w2 = obj2.width();

	obj1.css({ 
            marginLeft: (w2 - w1) / 2, 
            marginTop: (h2 - h1) / 2
        });
}

function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}

function my_utf8_encode ( string ) {
    string = (string+'').replace(/\r\n/g, "\n").replace(/\r/g, "\n");

    var utftext = "";
    var start, end;
    var stringl = 0;

    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;

        if (c1 < 128) {
            end++;
        } else if((c1 > 127) && (c1 < 2048)) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc != null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }

    if (end > start) {
        utftext += string.substring(start, string.length);
    }

    return utftext;
}
