var stdAmnt=0;
var proAmnt=0;
var testing=false;
//-------------------------------------------------
//-- Package buying
//-------------------------------------------------
/**
 * Fetch the DB values of pro and std packages.
 * This function receive a JSON object, and extract every key from it, instantiating local variables from it.
 * Used to setup javascript variables from the PHP config
 */
function _Each(_item, _idx, _cnt){
  alert(_idx+'::'+_item);
}
function getVal(){
  var url="/libs/xml/payment.php";
  var req="action=getValue&hash="+Math.random();
  var ajx = new xHttpRequest2();
  var ret = ajx.send('post', url, req, null, null, false, function(req, rsp){
        var json=rsp.responseText;//.evalJSON();
        json = eval('(' + json + ')');
        var objCount=0;
        for(_obj in json){
           val=eval(_obj+'=json.'+_obj);
        }
  });
}

/**
 * Update the total value of the command
 * @param string  elm   The reference to the input element which hold the value being parsed
 */
function updTot(elm){
  var cnt=elm.value;
  var tot=0;
  if(isNaN(cnt)){
    alert('Please enter only numbers.');
    elm.focus();
  }
  else{
    switch(elm.id){
      case 'inpTxtStd':
        tot=cnt*stdAmnt;
        tot=Math.round(tot*100)/100;
        $('stdTotal').innerHTML=tot+"$";
        break;
      case 'inpTxtPro':
        tot=cnt*proAmnt;
        tot=Math.round(tot*100)/100;
        $('proTotal').innerHTML=tot+"$";
        break;
    }
  }
}
/**
 * initiate a payment.
 * @param string    pp for paypal, mb for moneybooker
 */
function pay(method){
  //var stdCnt=$('inpTxtStd').value;
  var proCnt=$('inpTxtPro').value;
  //if( (isNaN(stdCnt)) || (isNaN(proCnt)) ){
  if(isNaN(proCnt)){
    alert('Please enter only numbers.')
  }
  else{
    switch(method){
      case 'pp':
        paypal();
        break;
      case 'mb':
        moneybooker();
        break;
    }
  }
}

/**
 * Create and submit the paypal form with the user entered values
 */
function paypal(){
  var url="/libs/xml/payment.php";
  //var stdCnt=$('inpTxtStd').value;
  var proCnt=$('inpTxtPro').value;
  //var req="action=paypal&hash="+Math.random()+"&stdCnt="+stdCnt+"&proCnt="+proCnt;
  var req="action=paypal&hash="+Math.random()+"&proCnt="+proCnt;
  var doSubmit=true;
  var ajx = new xHttpRequest();
  ajx.send('post', url, req, null, null, false, null, function(r, s, q){
        try{
          if($('frmPaypalChk').name){
            var trg=$('frmPaypalChk');
            trg.parentNode.removeChild(trg);
          }
        }
        catch(er){}
        var key=null;
        var frm=document.createElement('form');
        if(testing==true){
          frm.action="https://www.sandbox.paypal.com/cgi-bin/webscr";
          frm.appendChild(addInput('hidden','business','seller_1217880757_biz@webalis.com'));
          frm.appendChild(addInput('hidden','notify_url','http://dev.web-screenshots.com/libs/paypal/ipn_paypal.php'));
        }
        else{
          frm.action="https://www.paypal.com/cgi-bin/webscr";
          frm.appendChild(addInput('hidden','business','paypal@web-screenshots.com'));
          frm.appendChild(addInput('hidden','notify_url','http://www.web-screenshots.com/libs/paypal/ipn_paypal.php'));
        }
        frm.method="post";
        frm.id="frmPaypalChk";
        frm.name=frm.id;
        frm.appendChild(addInput('hidden','cmd','_cart'));
        frm.appendChild(addInput('hidden','upload',1));
        //frm.target="_blank";
        
        var json=r.responseText;
        json = eval('(' + json + ')');
        for(_obj in json){
           val=eval(_obj+'=json.'+_obj);
           if(_obj!='msg'){
             frm.appendChild(addInput('hidden',_obj,val));
           }
           else{
             alert(val);
             doSubmit=false;
           }
        }
        if(doSubmit==true){
          document.getElementsByTagName('body')[0].appendChild(frm);
          $('frmPaypalChk').submit();
        }
      });
  /*
  var ajx=new Ajax.Request(
    url,{
      method     : 'post',
      parameters : req,
      evalScripts: true,
      onComplete : function(q){
        try{
          if($('frmPaypalChk').name){
            var trg=$('frmPaypalChk');
            trg.parentNode.removeChild(trg);
          }
        }
        catch(r){}
        var key=null;
        var frm=document.createElement('form');
        if(testing==true){
          frm.action="https://www.sandbox.paypal.com/cgi-bin/webscr";
          frm.appendChild(addInput('hidden','business','seller@webalis.com'));
          frm.appendChild(addInput('hidden','notify_url','http://www.web-screenshots.com/libs/paypal/ipn_paypal.php'));
        }
        else{
          frm.action="https://www.paypal.com/cgi-bin/webscr";
          frm.appendChild(addInput('hidden','business','paypal@web-screenshots.com'));
          frm.appendChild(addInput('hidden','notify_url','http://www.web-screenshots.com/libs/paypal/ipn_paypal.php'));
        }
        frm.method="post";
        frm.id="frmPaypalChk";
        frm.name=frm.id;
        frm.appendChild(addInput('hidden','cmd','_cart'));
        frm.appendChild(addInput('hidden','upload',1));
        //frm.target="_blank";
        
        var json=q.responseText.evalJSON();
        var jsonKeys=Object.keys(json);
        for(var cptK=0;cptK<jsonKeys.length;cptK++){
          key=jsonKeys[cptK];
          val=eval('json.'+key);
          if(key!='alert'){
            frm.appendChild(addInput('hidden',key,val));
          }
          else{
            alert(val);
            doSubmit=false;
          }
        }
        if(doSubmit==true){
          document.getElementsByTagName('body')[0].appendChild(frm);
          $('frmPaypalChk').submit();
        }
      }
    }
  );
  */
}
/**
 * Create and submit the moneyBooker form
 */
function moneyBooker(){
  var url="/libs/xml/payment.php";
  var stdCnt=$('inpTxtStd').value;
  var proCnt=$('inpTxtPro').value;
  //var req="action=moneyBooker&hash="+Math.random()+"&stdCnt="+stdCnt+"&proCnt="+proCnt;
  var req="action=moneyBooker&hash="+Math.random()+"&proCnt="+proCnt;
}

/**
 * Create an element of type input, and return it
 * @param string  type
 * @param string  name
 * @param string  value
 * @return domElm
 */
function addInput(type,name,value){
  var inp=document.createElement('input');
  if(name==='ret'){inp.name='return'}
  else{inp.name=name;}
  inp.type=type;
  inp.value=value;
  return inp;
}

//-------------------------------------------------

//-------------------------------------------------
//-- init default values
//-------------------------------------------------
getVal();
