function changeInputType(
  oldElm, // a reference to the input element
  hiddenElm, // a reference to the input hidden element
  iType, // value of the type property: 'text' or 'password'
  iValue, // the default value, set to 'password' in the demo
  blankValue, // true if the value should be empty, false otherwise
  noFocus) {  // set to true if the element should not be given focus
  if(!oldElm || !oldElm.parentNode || (iType.length<4) || 
    !document.getElementById || !document.createElement) return;
  var newElm = document.createElement('input');
  newElm.type = iType;
  if(oldElm.name) newElm.name = oldElm.name;
  if(oldElm.id) newElm.id = oldElm.id;
  if(oldElm.className) newElm.className = oldElm.className;
  if(oldElm.size) newElm.size = oldElm.size;
  if(oldElm.tabIndex) newElm.tabIndex = oldElm.tabIndex;
  if(oldElm.accessKey) newElm.accessKey = oldElm.accessKey;
  if(oldElm.onkeypress) newElm.onkeypress = oldElm.onkeypress;
  newElm.onchange = function(){return function(){
    hiddenElm.value=this.value;
  }}();
 // hasFocus is to prevent a loop where onfocus is triggered over and over again
  newElm.hasFocus=false;
  oldElm.parentNode.replaceChild(newElm,oldElm);
  if(!blankValue) newElm.value = iValue;
  if(!noFocus || typeof(noFocus)=='undefined') {
    window.tempElm = newElm;
    setTimeout("tempElm.hasFocus=true;tempElm.focus();",1);
  }
  return newElm;
}

function ClearPassword(elem,elemtmp,defaultvalue) {
  if (elem.value==defaultvalue) {elem.value='';changeInputType(elem,elemtmp,'password','',false,false);}   
};

function ClearUsername(elem,defaultvalue) {
  if (elem.value==defaultvalue) {elem.value='';}
};

function handleKeyPress(e,form,elem,elemtmp,defaultvalue){
  var key=e.keyCode || e.which;
  if (key==13) {
    elemtmp.value=elem.value;
    form.submit();
  }
}

function showhide(id,idpanel) {
  if (document.getElementById(id)) {
    if (document.getElementById(id).style.display=='block') {	
	  resizePanel(idpanel,-(document.getElementById(id).offsetHeight)-8); 
      document.getElementById(id).style.display='none';
    } else {
      document.getElementById(id).style.display='block';
	  resizePanel(idpanel,document.getElementById(id).offsetHeight);
    }
  }	
  if (document.getElementById(id+'-2')) {
    if (document.getElementById(id+'-2').style.display=='block') {
      document.getElementById(id+'-2').style.display='none';
    } else {
      document.getElementById(id+'-2').style.display='block';
    }
  }	  
  return false;
};

function show(id,idpanel) {
  if (document.getElementById(id)) {
    if (document.getElementById(id).style.display=='none') {	
      document.getElementById(id).style.display='block';
	  resizePanel(idpanel,document.getElementById(id).offsetHeight);
	}    
  }	
  if (document.getElementById(id+'-2')) {
    if (document.getElementById(id+'-2').style.display=='none') {	
      document.getElementById(id+'-2').style.display='block';
	  resizePanel(idpanel,document.getElementById(id+'-2').offsetHeight);
	}    
  }	  
  return true;
};

function hide(id,idpanel) {
  if (document.getElementById(id)) {
    if (document.getElementById(id).style.display=='block') {	 
	  resizePanel(idpanel,-document.getElementById(id).offsetHeight-8);
	  document.getElementById(id).style.display='none'; 
	}
  }	
  if (document.getElementById(id+'-2')) {
    if (document.getElementById(id+'-2').style.display=='block') {	 
	  resizePanel(idpanel,-document.getElementById(id+'-2').offsetHeight-8);
	  document.getElementById(id+'-2').style.display='none'; 
	}
  }	  
  return true;
};

function uncheck(id) {
  document.getElementById(id).checked=false;
  return false;
};

function validfrm(frm,id,action,mess) {
  if (action=='access') {
    if (frm.mypassword.value!=frm.myconfirmpassword.value) { 
      alert(mess);	  
	  return resetfrm(frm,id);
    } else {      
	  return submitfrm(frm,id);
	}
  } else {    
	return submitfrm(frm,id);
  } 
};

function submitfrm(frm,id) {
  if (frm.onsubmit()) {
    frm.submit();
    hide(id);
  }	
  return false;
};


function resetfrm(frm,id) {
  frm.reset();
  hide(id);
  return false;
};

function onchangestatut(cb,val,id) {
  if (cb.value==val) { 
    if (document.getElementById(id+'_l')) show(id+'_l');  
	if (document.getElementById(id+'_v')) show(id+'_v'); 
  } else {
    if (document.getElementById(id+'_l')) hide(id+'_l');  
	if (document.getElementById(id+'_v')) hide(id+'_v'); 
  } 
  return true; 
}

function clearProfileInputFile(fileID){
  var elemID = "ed_pic" + fileID;     
  var remElem = document.getElementById(elemID);
  var newElem = document.createElement("input");   
  newElem.setAttribute("type","file");
  newElem.setAttribute("id","ed_pic" + fileID);
  newElem.setAttribute("name","picture" + fileID);
  newElem.onchange = Function("show('btn-profile');");
  document.getElementById("td_pic"+fileID).replaceChild(newElem, remElem);             
}

function fillList(id,ar,selectedvalue) {
  var sel = document.getElementById(id);
  while (sel.options.length > 0) {
    sel.options[0] = null;
  }  
  for (i=0; i<ar.length; i++) {
    var theOption = new Option;
    theOption.value = ar[i].id;
	theOption.text = ar[i].text;
    sel.options[i] = theOption;
	if (selectedvalue && theOption.value==selectedvalue) {sel.options[i].selected=true;}
  };  
  return true;
}
	

function getSelTxtList(cb) {
  if ((cb.type=='select-one')) {
     var w = cb.selectedIndex;
    if (w!=0){
      return cb.options[w].text;}
    else 
      return ''; 
  } else return cb.value;	
} 

function getSelValueList(cb) {
  if ((cb.type=='select-one')) {
     var w = cb.selectedIndex;
    if (w!=0) 
      return cb.options[w].value;
    else 
      return ''; 
  } else return cb.value;	
}  
	
function removeSpaces(string) {
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}

function Trim(str)
{  while(str.charAt(0) == (" ") )
  {  str = str.substring(1);
  }
  while(str.charAt(str.length-1) == " " )
  {  str = str.substring(0,str.length-1);
  }
  return str;
}

function formatAdr(street,city,zip,state,country) {
  var myAdr = '';
  mystr=Trim(country);  
  if (removeSpaces(mystr) != '')
    myAdr=mystr;
  mystr=Trim(state);  
  if (removeSpaces(mystr) != '')
    myAdr=myAdr+','+mystr;
  mystr=Trim(zip);  
  if (removeSpaces(mystr) != '')
    myAdr=myAdr+','+mystr;
  mystr=Trim(city);    
  if (removeSpaces(mystr) != '')
    myAdr=myAdr+','+mystr;
  mystr=Trim(street);  
  if (removeSpaces(mystr) != '')
    myAdr=myAdr+','+mystr;
	
  return myAdr;
}

function isdefined(variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

var map = null;
var geocoder = null;

      

function mapInit(address,errMess,nosubmit) {
  if ((GBrowserIsCompatible()) && (removeSpaces(address)!='')) {
    if (document.getElementById('map_canvas') && (!isdefined('CollapsiblePanelMap') || (isdefined('CollapsiblePanelMap') && (CollapsiblePanelMap.contentIsOpen)))) {
      var map = new GMap2(document.getElementById("map_canvas"));   
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());   
      map.setCenter(new GLatLng(46.516231, 6.637355), 1); 
	}   
    geocoder = new GClientGeocoder();
    if (geocoder) {
      geocoder.getLatLng(
        address,                      
        function(point) {
          if (point) { 
		    if (document.getElementById('fldLatitude')) document.getElementById('fldLatitude').value=point.lat();
            if (document.getElementById('fldLongitude')) document.getElementById('fldLongitude').value=point.lng();
			if (map) { 
		      map.setCenter(point, 15);			
 	          var marker = new GMarker(point);
              map.addOverlay(marker);
              //marker.openInfoWindowHtml(address);	
			}
			if ((document.frmaddress) && !(nosubmit)) {
			  submitfrm(document.frmaddress,'btn-address');	
			  }
          } else if (errMess) alert(errMess);
        }
      );
    }  
  }	
  return false;
}

function HttpRequest(id,lng,lat) {
 var xmlHttp=null; // Defines that xmlHttp is a new variable.
 // Try to get the right object for different browser
 try {
    // Firefox, Opera 8.0+, Safari, IE7+
    xmlHttp = new XMLHttpRequest(); // xmlHttp is now a XMLHttpRequest.
 } catch (e) {
    // Internet Explorer
    try {
       xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
       xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
 }
 xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4)
 try { // In some instances, status cannot be retrieved and will produce 
  // an error (e.g. Port is not responsive)
 if (xmlHttp.status == 200) {
  // Set the main HTML of the body to the info provided by the 
  // Ajax Request
  var myResponse=xmlHttp.responseText
  
 }
 } catch (e) {
  alert("Error on Ajax return call : " + e.description);
}
}
xmlHttp.open("get","/my/admin/upload/sortcalcPosition.cfm?uid=010203005&id="+id+"&long="+lng+"&lat="+lat,true); // .open(RequestType, Source);
xmlHttp.send(null); // Since there is no supplied form, null takes its place 
// as a new form.
return false;
}

var cptcalcpos=0;
var cptcalcposok=0;
function SetCalcPosition(id,address) {
  if ((GBrowserIsCompatible()) && (removeSpaces(address)!='')) {
    geocoder = new GClientGeocoder();
    if (geocoder) {
      geocoder.getLatLng(
        address,                      
        function(point) {
          if (point) { 
		      cptcalcpos++;cptcalcposok++;
			  document.getElementById('cpt1').value=cptcalcpos;
			  if(document.getElementById('cpt1').value=document.getElementById('cpt3').value)
			  {document.getElementById('waitplease').style.display='none';
			  document.getElementById('waitpleasegif').style.display='none';
			  document.getElementById('uploadsuccess').style.display='block';
			  document.getElementById('notfindmap').style.display='none';
			  }
			  document.getElementById('cpt2').value=cptcalcposok;
			  if (document.getElementById('cpt2').value!=document.getElementById('cpt3').value)
			  {document.getElementById('notfindmap').style.display='block';}
			  HttpRequest(id,point.lng(),point.lat());
          } else {
			  cptcalcpos++;document.getElementById('cpt1').value=cptcalcpos;
			  if(document.getElementById('cpt1').value=document.getElementById('cpt3').value)
			  {document.getElementById('waitplease').style.display='none';
			  document.getElementById('waitpleasegif').style.display='none';
			  document.getElementById('uploadsuccess').style.display='block';
			  document.getElementById('notfindmap').style.display='none';
			  }
			  if (document.getElementById('cpt2').value!=document.getElementById('cpt3').value)
			  {document.getElementById('notfindmap').style.display='block';}
			  };
		}  
      );
    }  
  }	
  return false;
}


function setcursor(el,st,end) {
  if (el.setSelectionRange) {
    el.focus();
    el.setSelectionRange(st,end);
  } else {
    if (el.createTextRange) {
      range=el.createTextRange();
      range.collapse(true);
      range.moveEnd('character',end);
      range.moveStart('character',st);
      range.select();
    }
  }
} 

function openPanel(panel,focuselemid) {
  if ((panel) && !(panel.isOpen())) {
    panel.open();
	panel.focus();	
  }
  if (focuselemid && document.getElementById(focuselemid)) setcursor(document.getElementById(focuselemid),0,0);
}	

function closePanel(panel) {
  if ((panel) && (panel.isOpen())) {
    panel.close();
  }
}	

function onChangeDate(id){
  var yList=document.getElementById('y_'+id);
  var mList=document.getElementById('m_'+id);
  var dList=document.getElementById('d_'+id);
  if ( (yList.options[yList.selectedIndex].value=='') && (mList.options[mList.selectedIndex].value=='') && (dList.options[dList.selectedIndex].value=='')) {
    document.getElementById(id).value='';
  } else {
    document.getElementById(id).value=document.getElementById('y_'+id).value+'-'+document.getElementById('m_'+id).value+'-'+document.getElementById('d_'+id).value;
  }	
  return true;
}

function onSetDate(id){
  var yList=document.getElementById('y_'+id);
  var mList=document.getElementById('m_'+id);
  var dList=document.getElementById('d_'+id);
  myvalue=document.getElementById(id).value;
  if (myvalue!='') {     
    yList.value=myvalue.split('-')[0];
    mList.value=myvalue.split('-')[1];
    day=myvalue.split('-')[2];
    dList.value=day.split(' ')[0];	
  } else {
    yList.selectedIndex=0;
    mList.selectedIndex=0;	
    dList.selectedIndex=0;
  }	 		 
}

function addRec(id,nbmax,idpanel,txt,value,theoffset){
  var myresult=false;
  if (!theoffset) theoffset=2;
  if (!nbmax) nbmax=5; 
  if ((value!=null) && (value=='')) {return false;}
  for (i=1; i<=nbmax; i++) {
    if ( (document.getElementById(id+'_'+i)) && (document.getElementById(id+'_'+i).style.display=='none')) {
	  document.getElementById(id+'_'+i).style.display='block'; 
	  if (value) 
	    document.getElementById('fld_'+id+'_'+i).value=value;		 
	  else
	    document.getElementById('fld_'+id+'_'+i).value=1;	
	  if ((document.getElementById('txt_'+id+'_'+i)) && (txt)) {
	    document.getElementById('txt_'+id+'_'+i).innerHTML=txt;   
	  }
	  resizePanel(idpanel,document.getElementById(id+'_1').offsetHeight+theoffset);
	  myresult=true; 
	  break;
	}
  };  
  return myresult;
}

function delRec(id,i,idpanel,theoffset){
  if (!theoffset) theoffset=2;
  if ( (document.getElementById(id+'_'+i)) && (document.getElementById(id+'_'+i).style.display=='block')) { 
    resizePanel(idpanel,-document.getElementById(id+'_'+i).offsetHeight-theoffset);
    document.getElementById(id+'_'+i).style.display='none'; 
	document.getElementById('fld_'+id+'_'+i).value=0;
  };  
  return false;
}

function CheckForDisabled(cb) {
  if (cb.options[cb.selectedIndex].disabled) {
    cb.selectedIndex = 0;
	return false;
  } else {
    return true;  
  }  
}

function ReturnTrue() {
    return true;  
}

function CheckAll(frm,activeClass,inactiveClass){
  for (var i=0;i<frm.elements.length;i++) {    
    var e=frm.elements[i];
    if ((e.name != 'allcheckbox') && (e.type=='checkbox')) {
        e.checked=frm.allcheckbox.checked;
        setClass(e,activeClass,inactiveClass)
    }
  }
}

// Color le TR apres avoir coché un checkbox
function setClass(elem,activeClass,inactiveClass) {
  if (elem.checked ) myclassName=activeClass; else myclassName="";
  while (elem.tagName!="TR") { 
    elem=elem.parentNode;
  }
  if (!(inactiveClass) || (elem.className!=inactiveClass)) elem.className=myclassName;
  
}

function LoadPBImg(inputId,imgElem,imgPath,heightId) {
  if (document.getElementById(heightId) && imgElem) 
    document.getElementById(heightId).value=imgElem.height;  
  if (document.getElementById(inputId) && imgElem && (document.getElementById(inputId).value!='')) {
  imgElem.src=imgPath;  
  }
}

function resizePanel(idpanel,theoffset) {
  if ((idpanel) && document.getElementById(idpanel) && theoffset) {
    if ((document.getElementById(idpanel).offsetHeight+theoffset)>=0)
	  document.getElementById(idpanel).style.height=document.getElementById(idpanel).offsetHeight+theoffset+'px';
  }    
}

function dblconfirm(mes1,mes2) {
if (confirm(mes1))
  if (confirm(mes2)) return true; else return false;
else return false;  
}


function print_preview(page) {
var h=800;
var w=600;
var top=(screen.height-h)/2;
var left=(screen.width-w)/2;
win = window.open(page,"","top="+top+",left="+left+",width="+w+",height="+h+",menubar=yes,location=no,directories=no,toolbar=no,status=no,scrollbars=yes,statusbar=no,resizable=yes");
return false;
} 
