
var PSxmlhttp;
// var gDownloadLink;

function getXmlHttpRequest(){
  var xhr=null;
//   code for Mozilla, etc.
  if (window.XMLHttpRequest){
    xhr=new XMLHttpRequest();
  }
//   code for IE
  else if (window.ActiveXObject){
//     IE7 (and newer)
    try
    {
      if ( ActiveXObject("Msxml2.XMLHTTP") ){
        xhr=new ActiveXObject("Msxml2.XMLHTTP");
//     IE6 and older
      }else{
        xhr=new ActiveXObject("Microsoft.XMLHTTP");
      }
    } catch(e) {
      xhr=new ActiveXObject("Microsoft.XMLHTTP");
    }
    
  }
  return xhr;
}

function PSAjaxRequest(url){
  PSxmlhttp = getXmlHttpRequest();

  if (PSxmlhttp!=null){
    PSxmlhttp.onreadystatechange=state_Change;
    PSxmlhttp.open("GET", url, true);
    PSxmlhttp.send(null);
  }
  else{
    alert("Your browser does not support XMLHTTP.");
  }
}

function state_Change(){
  // if xmlhttp shows "loaded"
  if (PSxmlhttp.readyState==4){
    // if "OK"
    if (PSxmlhttp.status==200){
      responseText = PSxmlhttp.responseText;
      alert('responseText: '+responseText);
      
    }else{
      alert("Problem retrieving data\n"+PSxmlhttp.responseText);
    }
  }
}


function PSAjaxRequestDownload(url){
  PSxmlhttp = getXmlHttpRequest();

  if (PSxmlhttp!=null){
    PSxmlhttp.onreadystatechange=evalXmlResponse;
    PSxmlhttp.open("GET", url, true);
    PSxmlhttp.send(null);
  }
  else{
    alert("Your browser does not support XMLHTTP.");
  }
}


function validateEmail(sEmail){
  var bRegexValid = false;
  var res = false;
  var reg;
  if(typeof(RegExp) == 'function'){

    var test = new RegExp('abc');
    if(test.test('abc') == true){
      bRegexValid = true;
    }
  }
  
  if(bRegexValid == true){
    reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+
                     '(\\@)([a-zA-Z0-9\\-\\.]+)'+
                     '(\\.)([a-zA-Z]{2,4})$');
    res = reg.test(sEmail);
  }
  else{
    res = (sEmail.search('@') >= 1 &&
           sEmail.lastIndexOf('.') > sEmail.search('@') &&
           sEmail.lastIndexOf('.') >= sEmail.length-5)
  }
  return res;
} 


function user(sEmail, sCompany, sFirstname, sLastname, sStreet, sZipcode, sCity, sCountry, sState, sPhone, sFax){
  var bValidEmail = validateEmail(sEmail);
  if ( typeof sEmail!='undefined' || bValidEmail==true ){
    this.email = sEmail;
  }else{
    return false;
  }

  if (typeof sCompany!='undefined'){
    this.company = sCompany;
  }
  if (typeof sFirstname!='undefined'){
    this.firstname = sFirstname;
  }
  if (typeof sLastname!='undefined'){
    this.lastname = sLastname;
  }
  if (typeof sStreet!='undefined'){
    this.street = sStreet;
  }
  if (typeof sZipcode!='undefined'){
    this.zipcode = sZipcode;
  }
  if (typeof sCity!='undefined'){
    this.city = sCity;
  }
  if (typeof sCountry!='undefined'){
    this.country = sCountry;
  }
  if (typeof sState!='undefined'){
    this.state = sState;
  }
  if (typeof sPhone!='undefined'){
    this.phone = sPhone;
  }
  if (typeof sFax!='undefined'){
    this.fax = sFax;
  }
  return this;
}

function wkb(sFormat, sPath, aSettings, sMIDENT, sFirm){

  if (typeof sFormat!='undefined'){
    this.format = sFormat;
  }
  if (typeof sFirm!='undefined'){
    this.firm = sFirm;
  }else{
    this.firm = 'cadenas';
  }
  if (typeof sMIDENT!='undefined'){
    this.mident = sMIDENT;
  }
  if (typeof sPath!='undefined'){
    this.path = sPath;
  }
  if (typeof aSettings!='undefined'){
    this.settings = aSettings;
  }
  
  if (typeof this.mident!='undefined'){
    return this;
  }else{
    if (typeof this.path!='undefined' && typeof this.settings!='undefined'){

      this.mident = '{' +this.path+ '}';
      for (i=0; i<this.settings.length; i++){
        this.mident += ',{' +this.settings[i].key+ '=' +this.settings[i].value+ '}';
      }
      return this;
    }else{
      return false;
    }
  }
}

function parseXML(sxmlFile){
  var xmlDoc;
  try{ //Internet Explorer
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    }catch(e){
    try{ //Firefox, Mozilla, Opera, etc.
      xmlDoc=document.implementation.createDocument("","",null);
      }catch(e){
        alert('error: '+e.message);
        return;
      }
    }
  xmlDoc.async=false;
  xmlDoc.load(sxmlFile);
  return xmlDoc;
}

function evalXmlResponse(){
  // if xmlhttp shows "loaded"
  if (PSxmlhttp.readyState==4){
    // if "OK"
    if (PSxmlhttp.status==200){
      var xml = PSxmlhttp.responseText;
      alert(xml);
      return;
//       var order = xml.getElementsByTagName('ORDER');
//       var section = order.getElementsByTagName('SECTION');
//       var contents = section.getElementsByTagName('CONTENTS');
      var zipfile = xml.getElementsByTagName('ZIPFILE').nodeValue;
      alert('done: '&zipfile);
      gDownloadLink = zipfile;
    }else{
      
      alert("Problem retrieving data\n"+PSxmlhttp.responseText);
    }
  }
}

// ################################################################################################################
// ###############################################    orderPart    ################################################
// ################################################################################################################
function orderPart(user, wkb){

  if (typeof user.email!='undefined' && 
      typeof wkb.format!='undefined' &&
      typeof wkb.mident!='undefined')
    {
      var cgi2pview = '/cgi-bin/cgi2pview.cgi';
      var cgiaction = 'generate';
      
      var arglist='';
      arglist += '?email='+user.email;
      arglist += '&cgiaction='+cgiaction;
      arglist += '&format='+wkb.format;
      arglist += '&part='+wkb.mident;

      if (typeof wkb.firm!='undefined'){ arglist += '&firm='+wkb.firm; }
      if (typeof user.firstname!='undefined' && typeof user.lastname!='undefined'){ arglist += '&user='+user.firstname+' '+user.lastname; }
      if (typeof user.company!='undefined'){ arglist += '&company='+user.company; }
      if (typeof user.street!='undefined'){ arglist += '&street='+user.street; }
//       if (typeof user.zipcode!='undefined'){ arglist += '&zip='+user.zipcode; }
      if (typeof user.city!='undefined'){ arglist += '&city='+user.city; }
      if (typeof user.country!='undefined'){ arglist += '&country='+user.country; }
      if (typeof user.state!='undefined'){ arglist += '&state='+user.state; }
      if (typeof user.phone!='undefined'){ arglist += '&phone='+user.phone; }
      if (typeof user.fax!='undefined'){ arglist += '&fax='+user.fax; }
        
      var generationUrl = cgi2pview + arglist;
      PSAjaxRequest(generationUrl);
    }
}


// ##################################################################################################################
// ###############################################    downloadPart    ###############################################
// ##################################################################################################################
function downloadPart(user, wkb){

//   if (typeof user.email!='undefined' && 
//       typeof wkb.format!='undefined' &&
//       typeof wkb.mident!='undefined')
//     {
//       var cgi2pview = '/cgi-bin/cgi2pview.cgi';
//       var cgiaction = 'download';
//       
//       var arglist='';
//       arglist += '?email='+user.email;
//       arglist += '&cgiaction='+cgiaction;
//       arglist += '&format='+wkb.format;
//       arglist += '&part='+wkb.mident;
// 
//       if (typeof wkb.firm!='undefined'){ arglist += '&firm='+wkb.firm; }
//       if (typeof user.firstname!='undefined' && typeof user.lastname!='undefined'){ arglist += '&user='+user.firstname+' '+user.lastname; }
//       if (typeof user.company!='undefined'){ arglist += '&company='+user.company; }
//       if (typeof user.street!='undefined'){ arglist += '&street='+user.street; }
// //       if (typeof user.zipcode!='undefined'){ arglist += '&zip='+user.zipcode; }
//       if (typeof user.city!='undefined'){ arglist += '&city='+user.city; }
//       if (typeof user.country!='undefined'){ arglist += '&country='+user.country; }
//       if (typeof user.state!='undefined'){ arglist += '&state='+user.state; }
//       if (typeof user.phone!='undefined'){ arglist += '&phone='+user.phone; }
//       if (typeof user.fax!='undefined'){ arglist += '&fax='+user.fax; }
//         
//       var generationUrl = cgi2pview + arglist;
//       
//       PSAjaxRequestDownload(generationUrl);
//       
//       while (typeof gDownloadLink=='undefined'){
//         window.setTimeout(function(){}, 500);
//       }
//       return gDownloadLink;
//       
//       
//     }else{
//       alert('Error: invalid arguments!');
//       return '';
// //       alert(typeof user.email);
// //       alert(typeof wkb.format);
// //       alert(typeof wkb.mident);
//       }
}




// #################################################################################################################
// ###############################################    previewPart    ###############################################
// #################################################################################################################
function previewPart(user, wkb, sWindowName, sWindowSettings){
  if (typeof user.email!='undefined' && 
      typeof wkb.format!='undefined' &&
      typeof wkb.mident!='undefined')
    {
      wkb.format='PARTjava';
      var cgiaction = 'preview';
      var cgi2pview = '/cgi-bin/plogger.asp';
//       var cgi2pview = '/cgi-bin/cgi2pview.cgi';
//       var cgi2pview = 'http://www.partserver.de/cgi-bin/cgi2pview.cgi';
      
      var arglist='';
      arglist += '?email='+user.email;
      arglist += '&cgiaction='+cgiaction;
      arglist += '&format='+wkb.format;
      arglist += '&part='+wkb.mident;
      

      if (typeof wkb.firm!='undefined'){ arglist += '&firm='+wkb.firm; }
      if (typeof user.firstname!='undefined' && typeof user.lastname!='undefined'){ arglist += '&user='+user.firstname+' '+user.lastname; }
      if (typeof user.company!='undefined'){ arglist += '&company='+user.company; }
      if (typeof user.street!='undefined'){ arglist += '&street='+user.street; }
//       if (typeof user.zipcode!='undefined'){ arglist += '&zip='+user.zipcode; }
      if (typeof user.city!='undefined'){ arglist += '&city='+user.city; }
      if (typeof user.country!='undefined'){ arglist += '&country='+user.country; }
      if (typeof user.state!='undefined'){ arglist += '&state='+user.state; }
      if (typeof user.phone!='undefined'){ arglist += '&phone='+user.phone; }
      if (typeof user.fax!='undefined'){ arglist += '&fax='+user.fax; }
        
      if (typeof sWindowName=='undefined'){
        sWindowName = 'directdownloadWindow';
      }
      if (typeof sWindowSettings=='undefined'){
//         sWindowSettings = 'width=450,height=450,left=100,top=200';
        sWindowSettings = 'width=450,height=450';
      }
      var generationUrl = cgi2pview + arglist;
//       document.write(generationUrl);
      var newWindow = window.open(generationUrl, sWindowName, sWindowSettings);
      newWindow.focus();//       PSAjaxRequest(generationUrl);
    }
}


