// fonctions generales

function Request_Get(sParam) {		// get parameter from another window. Using a get method. I don't know how I can do that in javascript so I've recreated a Querystring method.
	var sString = "";
	var iPos1;
	var iPos2;

	sString = document.location.search;		// I get the string from the document element. There is inside for instance : ?m=12&toto=45
	iPos1 = sString.indexOf(sParam);			// Position of the parameter I'm looking for.

	if (iPos1 == -1) {		// doesn't exist
		return "";
	} else {
		iPos1 += sParam.length + 1;
		iPos2 = sString.indexOf('&',iPos1);
		if (iPos2 == -1) {		// there are no more parameters in the string. So the end position for substring is the end of the string
			iPos2 = sString.indexOf('%26',iPos1);
			if (iPos2 == -1) {
				iPos2 = sString.length;
			}
		}
		return sString.substring(iPos1,iPos2);		// return the values of sParam parameter
	}
}

function TransformSpecialCharact(sString1) {
  //var Array_SpecialCharact =   new Array("\\",    "'",    "\"",    ">",   "<",   "à",       "é",       "è",       "ù",       "û",      "ê",      "ô",      "ç",       "ï",     "î",      "Ê",      "ë",     "ã",       "Ã",       "Ë",     "Ô",      "ö",     "Ö",     "Î",      "Ï",     "ü",     "Ü",     "Û",      "ñ",       "Ñ",       "[",    "]");
  //var Array_TransformCharact = new Array("&#92;", "&#39;","&quot;","&gt;","&lt;","&agrave;","&eacute;","&egrave;","&ugrave;","&ucirc;","&ecirc;","&ocirc;","&ccedil;","&iuml;","&icirc;","&Ecirc;","&euml;","&atilde;","&Atilde;","&Euml;","&Ocirc;","&ouml;","&Ouml;","&Icirc;","&Iuml;","&uuml;","&Uuml;","&Ucirc;","&ntilde;","&Ntilde;","&#91;","&#93;");
  var Array_SpecialCharact =   new Array("\\",    "'",    "\"",    ">",   "<");
  var Array_TransformCharact = new Array("&#92;", "&#39;","&quot;","&gt;","&lt;");
  var iLen1 = sString1.length;
  var iLen2 = Array_SpecialCharact.length;
  var Charact = "";
  var sNewString = "";

  for (var i=0;i<iLen1;i++) {
    Charact = sString1.substring(i,i+1);
    for (var j=0;j<iLen2;j++) {
      if (Charact == Array_SpecialCharact[j]) {
        sNewString += Array_TransformCharact[j];
        j=iLen2+1;
      }
    }

   	if (j<iLen2+1) {
   		sNewString += Charact;
    }
  }
  return sNewString;
}

function openWindow(sUrl,sName,iWidth,iHeight) {
	W_Popup = window.open(sUrl,sName,"toolbar=0,directories=0,menubar=0,resizable=yes,scrollbars=1,width="+iWidth+",height="+iHeight+",screenX=0,screenY=0,top=0,left=0");

	//W_Setting = window.open(sUrl,sName,"toolbar=0,directories=0,menubar=0,resizable=no,scrollbars=0,width="+ iWidth +",height="+iHeight+",screenX=0,screenY=0,top="+ (screen.availHeight - iHeight)/2 +",left="+(screen.availWidth - iWidth)/2); // center the window

	if (!W_Popup.opener) {
		W_Popup.opener = self;
	}
}

function CheckEmail(sEmail) {
	// fonction qui permet de verifier si la chaine sEmail est bel et bien un email
	// retourne true s'il s'agit bien d'un email et false autrement

	var iPos1=0;
	var iPos2=0;

	// 1) il existe un et un seul @ avec au moins un caractere avant
	if ((iPos1 = sEmail.indexOf('@')) == -1) {
		return false;
	}

	if (sEmail.indexOf('@',iPos1+1) != -1) {
		return false;
	}

	if (iPos1 == 0) {
		return false;
	}

	// 2) il existe un . situé au moins 1 caractere apres @
	if ((iPos2 = sEmail.indexOf('.',iPos1)) == -1) {
		return false;
	} else if (iPos2-iPos1 == 1) {
		return false;
	}

	//3) il existe au moins un caractere entre les points apres @
	//recherche du point suivant iPos3

	while ((iPos3 = sEmail.indexOf('.',iPos2+1)) != -1) {
		if (iPos3 - iPos2 < 2) {
			return false;
		}
		iPos2 = iPos3;
	}

	//4) il existe au moins deux caracteres apres le dernier .
	if (sEmail.length-iPos2<3) {
		return false;
	}

	//5) les seuls caracteres autorises sont abcdefghijklmnopqrstuvwxyz0123456789.@_- []
	sEmail = sEmail.toLowerCase();
	var iMax = sEmail.length;
	var sCaracteresAutorises = "abcdefghijklmnopqrstuvwxyz0123456789.@_-[] "; // ne pas effacer l'espace !

	for (var i=0;i<iMax;i++) {
		if (sCaracteresAutorises.indexOf(sEmail.charAt(i)) == -1) {
			i=iMax+1;
		}
	}

	if (i>iMax) {
		return false;
	}

	return true;
}

function DateActuelle() {
	var months=new Array(12);
	var dTime=new Date();
	var iYear = 0;

	months[0]="janvier";
	months[1]="f&eacute;vrier";
	months[2]="mars";
	months[3]="avril";
	months[4]="mai";
	months[5]="juin";
	months[6]="juillet";
	months[7]="ao&ucirc;t";
	months[8]="septembre";
	months[9]="octobre";
	months[10]="novembre";
	months[11]="d&eacute;cembre";

	iYear = dTime.getYear();
	if (iYear < 2000) {
		iYear += 1900; // avec Netscape le resultat de getYear ne comprend que l'année, pas le siecle !
	}

	return dTime.getDate() + " " + months[dTime.getMonth()] + " " + iYear;
}

function AfficherDateActuelle() {
	document.write(DateActuelle());
}

function Trim(String1) { // delete spaces before and after the string
  var bEmpty = "true";
  var iLen = String1.length;

  for (var i=0;i<iLen;i++) {
    if (String1.substring(i,i+1) != " ") {
      String1 = String1.substring(i,iLen);
      iLen = String1.length;
      i=iLen;
      bEmpty = "false";
    }
  }

  for (var i=0;i<iLen;i++) {
    if (String1.substring(iLen-i-1,iLen-i) != " ") {
      String1 = String1.substring(0,iLen-i);
      i=iLen;
      bEmpty = "false";
    }
  }

  if (bEmpty=="true") { String1 = ""; }
  return String1;
}

function CInt(String1) {
	// with javascript there is a function named parseInt in order to cast a string to an Integer
	// However this method doesn't work with those strings : (that's a bug)
	// "08" and "09" !!
	// examples : - parseInt("05") -> 5 (it works)
	//	      - parseInt("07") -> 7 (it works)
	//	      - parseInt("08") -> 0 (!!)
	//	      - parseInt("09") -> 0 (!!)
	//	      - parseInt("10") -> 10 (it works)
	// So I've recreated a parseInt method :

	var iCursor = 0;
	var sCharact = "";
	var iCharact = 0;
	var iResult = 0;
	String1 = "" + String1; // avoid some bugs when String1 is a number. So String1 is a real string now
	var iMax = String1.length;

	for (iCursor=0;iCursor<iMax;iCursor++) {
		sCharact = String1.substring(iCursor,iCursor+1);

		switch(sCharact) {
			case "0": iCharact = 0; break;
			case "1": iCharact = 1; break;
			case "2": iCharact = 2; break;
			case "3": iCharact = 3; break;
			case "4": iCharact = 4; break;
			case "5": iCharact = 5; break;
			case "6": iCharact = 6; break;
			case "7": iCharact = 7; break;
			case "8": iCharact = 8; break;
			case "9": iCharact = 9; break;
			default: iCharact = -1; iCursor = iMax;
		}

		if (iCharact != -1) {
			iResult = iResult*10 + iCharact;
		}
	}
	return iResult;
}

function Replace(p_sString,p_sStringReplaced,p_sStringSubstitute) {
  var sTempString = p_sString.toLowerCase();
  var iLen = p_sStringReplaced.length;
  var iPos = 0;

  p_sStringReplaced = p_sStringReplaced.toLowerCase();

  while ((iPos = sTempString.indexOf(p_sStringReplaced,iPos)) != -1) {
    p_sString = p_sString.substring(0,iPos) + p_sStringSubstitute + p_sString.substring(iPos + iLen,p_sString.length); // replace the string
    sTempString = p_sString.toLowerCase();
  }

  return p_sString;
}

function rnd() {
	rnd.seed = (rnd.seed*9301+49297) % 233280;
	return rnd.seed/(233280.0);
}

function Rand(number) {
	rnd.today=new Date();
	rnd.seed=rnd.today.getTime();
  return Math.ceil(rnd()*number);
}

//Gratuit --> NON UTILISE
function openCoord3(id) {
	openWindow('',"Coordonnees",700,350);
	W_Popup.document.write("<HTML><HEAD><TITLE>Appartement.org - Coordonnées de l'annonce</title></HEAD><BODY onload=\"document.form1.submit();\">Veuillez patienter, le chargement de la page est en cours ...<FORM method=POST name=\"form1\" action=\"/cgi-bin/coordannonce.cgi\"><INPUT type=hidden name=\"id\" value=\"" + id + "\"></FORM></BODY></HTML>");
	W_Popup.focus();
	W_Popup.document.close();
	return;	
}

//Payant --> NON UTILISE
function openCoord2(id) {
	openWindow('http://www.appartement.org/Allopass.html?DATAS='+id,"Coordonnees",700,440);
	return;	
}

//Payant 2 --> UTILISE
function openCoord(id,prix) {
	openWindow('',"Coordonnees",700,500);
	W_Popup.document.write("<HTML><HEAD><TITLE>Appartement.org - Coordonnées de l'annonce</title></HEAD><BODY onload=\"document.form1.submit();\">Veuillez patienter, le chargement de la page est en cours ...<FORM method=POST name=\"form1\" action=\"/cgi-bin/allopasscoord.cgi\"><INPUT type=hidden name=\"id\" value=\"" + id + "\"><INPUT type=hidden name=\"prix\" value=\"" + prix + "\"></FORM></BODY></HTML>");
	W_Popup.focus();
	W_Popup.document.close();
	return;	
}

function Xiti(NomPage) {
	
	hsh = new Date();
	hsd = document;
	hsr = hsd.referrer.replace(/[<>]/g, '');
	hsi = '<img width="39" height="25" border=0 ';
	hsi += 'src="http://logv25.xiti.com/hit.xiti?s=161654';
	hsi += '&p='+NomPage;
	hsi += '&hl=' + hsh.getHours() + 'x' + hsh.getMinutes() + 'x' + hsh.getSeconds();
	if(parseFloat(navigator.appVersion)>=4)
	{Xiti_s=screen;hsi += '&r=' + Xiti_s.width + 'x' + Xiti_s.height + 'x' + Xiti_s.pixelDepth + 'x' + Xiti_s.colorDepth;}
	hsd.writeln(hsi + '&ref=' + hsr.replace(/&/g, '$') + '" title="Mesurez votre audience"><\!--');
	return;	
}

