var xhr = null; 
var xhr2 = null;
var xhr3 = null;

function getXhr(){
  var xhr = null;
  if(window.XMLHttpRequest) // Firefox et autres
    xhr = new XMLHttpRequest(); 
  else if(window.ActiveXObject){ // Internet Explorer 
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  else { // XMLHttpRequest non supporté par le navigateur 
    alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
    xhr = false; 
  } 
  return (xhr);
}

function getXhr2(){
  var xhr2 = null;
  if(window.XMLHttpRequest) // Firefox et autres
    xhr2 = new XMLHttpRequest(); 
  else if(window.ActiveXObject){ // Internet Explorer 
    try {
      xhr2 = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      xhr2 = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  else { // XMLHttpRequest non supporté par le navigateur 
    alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
    xhr2 = false; 
  } 
  return (xhr2);
}

function getXhr3(){
  var xhr3 = null;
  if(window.XMLHttpRequest) // Firefox et autres
    xhr3 = new XMLHttpRequest(); 
  else if(window.ActiveXObject){ // Internet Explorer 
    try {
      xhr3 = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      xhr3 = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  else { // XMLHttpRequest non supporté par le navigateur 
    alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
    xhr3 = false; 
  } 
  return (xhr3);
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) oldonload();
      func();
    }
  }
}

function buildLanguage(where,country,language) {
if (document.getElementById(where) != null) {
  var xhr = getXhr();
  xhr.onreadystatechange = function(){
    if(xhr.readyState == 4 && xhr.status == 200){
      disphtml = xhr.responseText;
      document.getElementById(where).innerHTML = disphtml;
    }
  }
  qstring = 'cty='+escape(country);
  if (language != '') {
    qstring = qstring+'&lg='+escape(language);
  }
  xhr.open("POST","partner_build_languages.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xhr.send(qstring);
}
}

function buildState(where,country,state) {
if (document.getElementById(where) != null) {
  var xhr2 = getXhr2();
  xhr2.onreadystatechange = function(){
    if(xhr2.readyState == 4 && xhr2.status == 200){
      disphtml = xhr2.responseText;
      document.getElementById(where).innerHTML = disphtml;
    }
  }
  qstring = 'cty='+escape(country);
  if (state != '') {
    qstring = qstring+'&state='+escape(state);
  }
  xhr2.open("POST","partner_build_states.php",true);
  xhr2.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xhr2.send(qstring);
}
}

function buildTitle(where,language,title) {
if (document.getElementById(where) != null) {
  var xhr3 = getXhr3();
  xhr3.onreadystatechange = function(){
    if(xhr3.readyState == 4 && xhr3.status == 200){
      disphtml = xhr3.responseText;
      document.getElementById(where).innerHTML = disphtml;
    }
  }
  qstring = 'lg='+escape(language);
  if (title != '') {
    qstring = qstring+'&tit='+escape(title);
  }
  xhr3.open("POST","partner_build_titles.php",true);
  xhr3.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xhr3.send(qstring);
}
}

function startDocsCart(key,contact,doc,where,icon){
  document.body.style.cursor = 'wait';
  window.setTimeout("doDocsCart('"+key+"','"+contact+"','"+doc+"','"+where+"','"+icon+"');", 100);
}

function doDocsCart(key,contact,doc,where,icon){
  var xhr = getXhr();
  xhr.onreadystatechange = function(){
    if(xhr.readyState == 4 && xhr.status == 200){
      disphtml = xhr.responseText;
      document.getElementById(where).innerHTML = disphtml;
      document.body.style.cursor = 'default';
      startCartList(key,contact,'cartscroll','none');
    }
  }
  qstring = 'key='+escape(key)+'&contact='+escape(contact);
  if (doc != '') {
    qstring = qstring+'&doc='+escape(doc);
  }
  xhr.open("POST","partner_docs_cart.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xhr.send(qstring);
}

function startDocsCartWithRefresh(key,contact,doc,where,icon){
  document.body.style.cursor = 'wait';
  window.setTimeout("doDocsCartWithRefresh('"+key+"','"+contact+"','"+doc+"','"+where+"','"+icon+"');", 100);
}

function doDocsCartWithRefresh(key,contact,doc,where,icon){
  var xhr = getXhr();
  xhr.onreadystatechange = function(){
    if(xhr.readyState == 4 && xhr.status == 200){
      disphtml = xhr.responseText;
      doDeliveryDDD(key,contact);
    }
  }
  qstring = 'key='+escape(key)+'&contact='+escape(contact);
  if (doc != '') {
    qstring = qstring+'&doc='+escape(doc);
  }
  xhr.open("POST","partner_docs_cart.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xhr.send(qstring);
}

function doDeliveryDDD(key,contact){
  var xhr = getXhr();
  xhr.onreadystatechange = function(){
    if(xhr.readyState == 4 && xhr.status == 200){
      disphtml = xhr.responseText; // jobid
      doDDD(disphtml);
    }
  }
  qstring = 'key='+escape(key)+'&contact='+escape(contact);
  qstring += '&delivery=DDD';
  xhr.open("POST","partner_document_delivery.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xhr.send(qstring);
}

function doDDD(jobid){
  var xhr = getXhr();
  xhr.onreadystatechange = function(){
    if(xhr.readyState == 4 && xhr.status == 200){
      disphtml = xhr.responseText; // error ou lien vers doc
      if (disphtml == 'error') {
        alert('Error: unable to transfer direct request to docpackager');
      } else {
        window.location.replace(disphtml);
      }
      document.body.style.cursor = 'default';
    }
  }
  qstring = 'mode=DDD&jobid='+escape(jobid);
  xhr.open("POST","doc_request.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xhr.send(qstring);
}

function doDocsProject(key,contact,where){
  var span = document.getElementById(where);
  if (span) {
    var waitimg = buildImgTag('a7_icon_loading.gif','20','20','Loading...');
    span.innerHTML = waitimg;
    var xhr = getXhr();
    xhr.onreadystatechange = function(){
      if(xhr.readyState == 4 && xhr.status == 200){
        disphtml = xhr.responseText;
        span.innerHTML = disphtml;
        if (document.body.style.cursor == 'wait') document.body.style.cursor = 'default';
        window.setTimeout("doProfilePreview('"+key+"','"+contact+"','previewbox');", 1);
      }
    }
    qstring = 'key='+escape(key)+'&contact='+escape(contact);
    xhr.open("POST","partner_docs_project.php",true);
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.send(qstring);
  }
}

function startCartList(key,contact,where,refresh){
  document.body.style.cursor = 'wait';
  window.setTimeout("doCartList('"+key+"','"+contact+"','"+where+"','"+refresh+"');", 100);
}

function doCartList(key,contact,where,refresh){
  var xhr2 = getXhr2();
  xhr2.onreadystatechange = function(){
    if(xhr2.readyState == 4 && xhr2.status == 200){
      disphtml = xhr2.responseText;
      document.getElementById(where).innerHTML = disphtml;
      document.body.style.cursor = 'default';
      if (refresh=='refresh') {
        if (PageName == 'document_library') {
          startDocsList(key,contact,'refresh','','','','scroll');
        } else if (PageName == 'document_delivery') {
          startCartList(key,contact,'scroll');
        } else if (PageName == 'project') {
          doDocsProject(key,contact,'scroll');
        }
      }
    }
  }
  qstring = 'key='+escape(key)+'&contact='+escape(contact);
  xhr2.open("POST","partner_cart_list.php",true);
  xhr2.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xhr2.send(qstring);
}

function startProfileList(key,contact,level,answer,where){
  var span = document.getElementById(where);
  if (span) {
    document.body.style.cursor = 'wait';
    window.setTimeout("doProfileList('"+key+"','"+contact+"','"+level+"','"+answer+"','"+where+"');", 100);
  }
}

function doProfileList(key,contact,level,answer,where){
  var span = document.getElementById(where);
  if (span) {
    var xhr = getXhr();
    xhr.onreadystatechange = function(){
      if(xhr.readyState == 4 && xhr.status == 200){
        disphtml = xhr.responseText;
        document.getElementById(where).innerHTML = disphtml;
        if (document.body.style.cursor == 'wait') document.body.style.cursor = 'default';
        window.setTimeout("doDocsProject('"+key+"','"+contact+"','scroll');", 1);
      }
    }
    qstring = 'key='+escape(key)+'&contact='+escape(contact);
    qstring = qstring+'&level='+escape(level)+'&answer='+escape(answer);
    xhr.open("POST","partner_profile_list.php",true);
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.send(qstring);
  }
}

function doProfilePreview(key,contact,where){
  span = document.getElementById(where);
  if (span) {
    var xhr = getXhr();
    document.body.style.cursor = 'wait';
    xhr.onreadystatechange = function(){
      if(xhr.readyState == 4 && xhr.status == 200){
        disphtml = xhr.responseText;
        document.getElementById(where).innerHTML = disphtml;
        document.body.style.cursor = 'default';
      }
    }
    qstring = 'key='+escape(key)+'&contact='+escape(contact);
    xhr.open("POST","partner_profile_preview.php",true);
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.send(qstring);
  }
}

function startDocsList(key,contact,docparm,prodparm,langparm,extparm,where){
  document.body.style.cursor = 'wait';
  window.setTimeout("doDocsList('"+key+"','"+contact+"','"+docparm+"','"+prodparm+"','"+langparm+"','"+extparm+"','"+where+"');", 100);
}

function doDocsList(key,contact,docparm,prodparm,langparm,extparm,where){
  var xhr = getXhr();
  xhr.onreadystatechange = function(){
    if(xhr.readyState == 4 && xhr.status == 200){
      disphtml = xhr.responseText;
      document.getElementById(where).innerHTML = disphtml;
      document.body.style.cursor = 'default';
    }
  }
  qstring = 'key='+escape(key)+'&contact='+escape(contact);
  if (docparm != 'all') {
    qstring = qstring+'&docparm='+escape(docparm);
  }
  if (prodparm != 'all') {
    qstring = qstring+'&prodparm='+escape(prodparm);
  }
  if (langparm != 'all') {
    qstring = qstring+'&langparm='+escape(langparm);
  }
  if (extparm != 'all') {
    qstring = qstring+'&extparm='+escape(extparm);
  }
  xhr.open("POST","partner_docs_list.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xhr.send(qstring);
}

function startAuditList(key,contact,status,where){
  document.body.style.cursor = 'wait';
  window.setTimeout("doAuditList('"+key+"','"+contact+"','"+status+"','"+where+"');", 100);
}

function doAuditList(key,contact,status,where){
  var xhr = getXhr();
  xhr.onreadystatechange = function(){
    if(xhr.readyState == 4 && xhr.status == 200){
      disphtml = xhr.responseText;
      document.getElementById(where).innerHTML = disphtml;
      document.body.style.cursor = 'default';
    }
  }
  qstring = 'key='+escape(key)+'&contact='+escape(contact);
  if (status != 'all') {
    qstring = qstring+'&astatus='+escape(status);
  }
  xhr.open("POST","partner_audits_list.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xhr.send(qstring);
}

function startGuarRoofList(key,contact,status,fname,fvalue,where){
  var span = document.getElementById(where);
  if (span) {
    var waitimg = buildImgTag('a7_icon_loading.gif','20','20','Loading...');
    var roofcnt = document.getElementById('roofcnt');  // get roofcnt span
    if (roofcnt) roofcnt.innerHTML = waitimg; // set to loading as well
    span.innerHTML = waitimg;
    window.setTimeout("doGuarRoofList('"+key+"','"+contact+"','"+status+"','"+fname+"','"+fvalue+"','"+where+"');", 100);
  }
}

function doGuarRoofList(key,contact,status,fname,fvalue,where){
  var span = document.getElementById(where);
  if (span) {
    var imgall = new Array('allaccepted','allnone','allrequested','allother','allaudit','allmissing');
    if (status == '') {
      if (fname != '') {
        if ((fname == 'guartype') && (fvalue == '#EMPTY#')) {
          status = 'allnone';
        } else {
          status = GuarStatus;
        }
      }
    } else {
      GuarStatus = status;
      fname = '';
      fvalue = '';
    }
    var xhr = getXhr();
    xhr.onreadystatechange = function(){
      if(xhr.readyState == 4 && xhr.status == 200){
        disphtml = xhr.responseText;
        document.getElementById(where).innerHTML = disphtml;
        xhr = null;
        doGuarRoofCnt(key,contact,'roofcnt');
      }
    }
    for (var i=0; i<imgall.length; i++) {
      if (icon=document.getElementById('img'+imgall[i])) {
        iconsel = (imgall[i]==status) ? '_selected' : '';
        icon.src = 'images/a7_icon_guar_'+imgall[i]+iconsel+'.gif';
      }
    } 
    qstring = 'key='+escape(key)+'&contact='+escape(contact);
    if (status != 'all') {
      qstring = qstring+'&gstatus='+escape(status);
    }
    qstring = qstring+'&fname='+escape(fname)+'&fvalue='+escape(fvalue);
    xhr.open("POST","guaranty_roofs_list.php",true);
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.send(qstring);
  }
}

function doGuarRoofCnt(key,contact,where){
  var span = document.getElementById(where);
  if (span) {
    var waitimg = buildImgTag('a7_icon_loading.gif','20','20','Loading...');
    span.innerHTML = waitimg;
    var xhr = getXhr();
    xhr.onreadystatechange = function(){
      if(xhr.readyState == 4 && xhr.status == 200){
        disphtml = xhr.responseText;
        document.getElementById(where).innerHTML = disphtml;
        xhr = null;
      }
    }
    qstring = 'key='+escape(key)+'&contact='+escape(contact);
    qstring += '&action=displayroofcnt';
    xhr.open("POST","guaranty_action.php",true);
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.send(qstring);
  }
}

function startRoofList(key,contact,status,fname,fvalue,where){
  var span = document.getElementById(where);
  if (span) {
    var waitimg = buildImgTag('a7_icon_loading.gif','20','20','Loading...');
    span.innerHTML = waitimg;
    window.setTimeout("doRoofList('"+key+"','"+contact+"','"+status+"','"+fname+"','"+fvalue+"','"+where+"');", 100);
  }
}

function doRoofList(key,contact,status,fname,fvalue,where){
  var span = document.getElementById(where);
  if (span) {
    var imgall = new Array('allaccepted','allnone','allrequested','allother');
    if (status == '') {
      if (fname != '') status = GuarStatus;
    } else {
      GuarStatus = status;
      fname = '';
      fvalue = '';
    }
    var xhr = getXhr();
    xhr.onreadystatechange = function(){
      if(xhr.readyState == 4 && xhr.status == 200){
        disphtml = xhr.responseText;
        document.getElementById(where).innerHTML = disphtml;
        doRoofFilters(key,contact,'guarfilters');
      }
    }
    for (var i=0; i<imgall.length; i++) {
      if (icon=document.getElementById('img'+imgall[i])) {
        iconsel = (imgall[i]==status) ? '_selected' : '';
        icon.src = 'images/a7_icon_guar_'+imgall[i]+iconsel+'.gif';
      }
    } 
    qstring = 'key='+escape(key)+'&contact='+escape(contact);
    if (status != 'all') {
      qstring = qstring+'&gstatus='+escape(status);
    }
    qstring = qstring+'&fname='+escape(fname)+'&fvalue='+escape(fvalue);
    xhr.open("POST","partner_roofs_list.php",true);
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.send(qstring);
  }
}

function doRoofFilters(key,contact,where){
  var span = document.getElementById(where);
  if (span) {
    var waitimg = buildImgTag('a7_icon_loading.gif','20','20','Loading...');
    span.innerHTML = waitimg;
    var xhr = getXhr();
    xhr.onreadystatechange = function(){
      if(xhr.readyState == 4 && xhr.status == 200){
        disphtml = xhr.responseText;
        document.getElementById(where).innerHTML = disphtml;
      }
    }
    qstring = 'key='+escape(key)+'&contact='+escape(contact);
    xhr.open("POST","partner_guaranty_build_filters.php",true);
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.send(qstring);
  }
}

function startGeneratePDF(key,contact,roof,guaranty){
  alert(generatepdfalert);
  document.body.style.cursor = 'wait';
  window.setTimeout("doGeneratePDF('"+key+"','"+contact+"','"+roof+"','"+guaranty+"');", 100);
}

function doGeneratePDF(key,contact,roof,guaranty){
  var xhr = getXhr();
  xhr.onreadystatechange = function(){
    if(xhr.readyState == 4 && xhr.status == 200){
      restext = xhr.responseText;
      resvals = restext.split("||");
      if (typeof(resvals[0] != "undefined")) {
        if (resvals[0] == 'url') {
          if (typeof(resvals[1] != "undefined")) {
            WDT = 1024;
            HGT = 768;
            TOP = (screen.height-HGT)/2;
            LEFT = (screen.width-WDT)/2;
            window.open(resvals[1],'request','directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,width='+WDT+',height='+HGT+',left='+LEFT+',top='+TOP);
          } else {
            alert('Unable to open unknown pdf file');
          }
        } else {
          alert('Error: '+restext);
        }
      }
      document.body.style.cursor = 'default';
    }
  }
  qstring = 'key='+escape(key)+'&contact='+escape(contact);
  if (roof != 'none') {
    qstring = qstring+'&roof='+escape(roof);
    if (guaranty != 'none') {
      qstring = qstring+'&guaranty='+escape(guaranty);
    }
  }
  xhr.open("POST","partner_guaranty_request.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xhr.send(qstring);
}

function startCertificatePDF(key,contact,roof,guaranty){
  document.body.style.cursor = 'wait';
  window.setTimeout("doCertificatePDF('"+key+"','"+contact+"','"+roof+"','"+guaranty+"');", 100);
}

function doCertificatePDF(key,contact,roof,guaranty){
  var xhr = getXhr();
  xhr.onreadystatechange = function(){
    if(xhr.readyState == 4 && xhr.status == 200){
      document.body.style.cursor = 'default';
      disphtml = xhr.responseText; // jobid
      doDDD(disphtml);
      //restext = xhr.responseText;
      //alert(restext);
    }
  }
  qstring = 'key='+escape(key)+'&contact='+escape(contact);
  if (roof != 'none') {
    qstring = qstring+'&roof='+escape(roof);
    if (guaranty != 'none') {
      qstring = qstring+'&guaranty='+escape(guaranty);
    }
  }
  qstring += '&delivery=DDD';
  xhr.open("POST","partner_guaranty_certificate.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xhr.send(qstring);
}

function showPreview(doc,target) {
  targetobj = document.getElementById(target);
  if (targetobj) {
    var waitimg = buildImgTag('a7_icon_loading.gif','20','20','Loading...');
    targetobj.innerHTML = waitimg;
    if (!doc) return;
    var xhr = getXhr();
    xhr.onreadystatechange = function() {
      if(xhr.readyState == 4) {
        if (xhr.status == 200) {
          disphtml = xhr.responseText;
          xhr = null;
        } else {
	  disphtml = "Unable to load document preview...";
	}
      } else {
	disphtml = "Loading document preview...";
      }
      targetobj.innerHTML=disphtml;
    }
    qstring = 'doc='+escape(doc);
    xhr.open("POST","partner_document_preview.php",true);
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.send(qstring);
  }
}

function ddrivetip(thetext, thewidth, thecolor){
  enableprv=true;
  if (ns6||ie){
    var xhr = getXhr();
    xhr.onreadystatechange = function() {
      if(xhr.readyState == 4) {
        if (xhr.status == 200) {
          disphtml = xhr.responseText;
        } else {
	  disphtml = "Unable to load document preview...";
	}
      } else {
	disphtml = "Loading document preview...";
      }
      if (enableprv) {
        if (typeof thewidth == "undefined") thewidth=300;
        prvobj.style.width=thewidth+"px";
        if (typeof thecolor!="undefined" && thecolor!="") prvobj.style.backgroundColor=thecolor;
        prvobj.style.top="180px";
        dwidth = (ie) ? document.body.clientWidth : document.width;
        prvmargin = (dwidth > 996) ? (dwidth-996)/2 : 0;
        prvleft = parseInt((dwidth - thewidth) - prvmargin - 8);
        prvobj.style.left= prvleft+"px"; //}
        prvobj.innerHTML=disphtml;
        prvobj.style.display="block";
      } else {
        prvobj.style.display="none";
        prvobj.innerHTML='';
      }
    }
    qstring = 'doc='+escape(thetext);
    xhr.open("POST","partner_document_preview.php",true);
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.send(qstring);
  }
  return false;
}

function hideddrivetip(){
enableprv=false;
if (ns6||ie){
prvobj.style.display="none";
prvobj.style.backgroundColor='';
prvobj.style.width='';
prvobj.innerHTML='';
}
}

function toggleRows(id,number,icon) {
 var type='hide';
 for (i=1;i<=number;i++) {
  row=document.getElementById(id+i);
  if (row) {
   if (row.style.display == 'none') {
    type='show';
    try {
     row.style.display='table-row';
    } catch(e) {
     row.style.display = 'block';
    }
   } else {
     row.style.display = 'none';
   }
  }
 }
 if (type == 'show') {
  document.getElementById(icon).src='images/a7_icon_bundle_collapse.gif';
 } else {
  document.getElementById(icon).src='images/a7_icon_bundle_16.gif';
 }
}

function buildImgTag(img,w,h,alt) {
  return '<img src="images/'+img+'" width="'+w+'" height="'+h+'" alt="'+alt+'" border="0" />';
}

