
// call this function             
function handleIdealPayment(iAmount, iPrice, iArtikelId, sAction) {    
    oIdeal = new cIdeal();
    oIdeal.init(iAmount, iPrice, iArtikelId, sAction);
}
function checkInput(iArtikelId) {    
    oCheck = new cIdeal();
    oCheck.checkInput(iArtikelId);
}

// voordat je naar ideal gaat, sla je de gegevens op
function saveInput(formulier) {    
    oSave = new cSave();
    oSave.saveInput(formulier);
}

// object cIdeal
function cIdeal() {   
    
    this.init = function(iAmount, iPrice, iArtikelId, sAction) {     //alert('init');  
    
        this.amount      = iAmount;         // amount of products
        this.price       = iPrice;          // price of product
        this.artikelid   = iArtikelId;      // artikel id
        this.action      = sAction;         // action => 0 (do nothing) or 1 (calculate)
        
        this.rooturl     = sRootUrl;        // rooturl
        
        this.hashkey     = '';              // hashkey
        this.validUntil  = '';              // validUntil
        this.total       = 0;               // total price in cents 
        this.titelBestel = '';              // titel when form is wrong submitted
        this.tekstBestel = '';              // tekst when form is wrong submitted
        
        if(this.action == 1) {                       
            this.calc();                    // start calculate       
            this.setQuantity();             // set value for itemQuantity1
            this.getHashKey();              // get hash key            
        }
        else {                                
            // do nothing
        }                
        
        document.getElementById('bestelSubmit').disabled = true;                     // disable submit button 
        document.getElementById('bestelSubmit').style.cursor = 'default';            // do not show pointer
    };               
        

    // replace document.getElementById function
    this.getId = function(id) {
        return document.getElementById(id);
    };            
        

    // compose hashKey
    this.getHashKey = function() { 
        
        // ajax request                   
        var oRequest = new cRequest();                      
        oRequest.sUrl = this.rooturl + "ideal/getData.php?usage=gethashkey&artikelid=" + this.artikelid + "&amount=" + this.total + "&quantity=" + this.amount; 
        oRequest.sType = 'GET';       
        var aData;
        aData = eval("("+  oRequest.getContent().responseText + ")");             // get hash key        
        
        this.hashkey = aData.hashkey;
        this.validUntil = aData.validUntil;               
        
        this.setHashKey();
    }
        
    // set hash key
    this.setHashKey = function() {
        
        this.getId('hash').value = this.hashkey;                   
        this.getId('validUntil').value = this.validUntil;                   
    }
        
        
    // calc total price
    this.calc = function() {                
                                 
        this.total = this.price * this.amount;         
        this.setAmount();                           // set value for amount        
    };    
    
    
    // set amount (= total price)
    this.setAmount = function() {    
        
        this.getId('amount').value = this.total;        
    }

    // set quantity (= aantal products)
    this.setQuantity = function() {    
        
        this.getId('itemQuantity1').value = this.amount;        
    }    
   
    
    
    
    
    
    
    
    // get feedback to user data    
    this.getFeedbackToUser = function() {             
                                              
        // ajax request
        var oRequest = new cRequest();                      
        oRequest.sUrl = sRootUrl + "ideal/getData.php?usage=feedbacktouser&pageid=33&artikelid=" + this.artikelid; 
        oRequest.sType = 'GET';       
        var aData; 
        aData = eval("("+  oRequest.getContent().responseText + ")");             // get data
                   
        this.titelBestel = aData.titel;       // set this.titelbestel
        this.tekstBestel = aData.tekst;       // set this.tekstBestel 
    }    
    
    // set error msg to user
    this.setFeedbackToUser = function() {
                                                
        this.getId('titelBestel').innerHTML = this.titelBestel;    
        this.getId('tekstBestel').innerHTML = this.tekstBestel;                                
    }        
        
    // check user input
    this.checkInput = function(iArtikelId) {
        
        this.artikelid = iArtikelId;
        
        // form checks (mark field red if not filled)
        if(this.getId('naam').value == "") {
            this.getId('naam').style.borderLeft = '5px solid #BE1A2B';           
        } 
        else {
            this.getId('naam').style.borderLeft = '5px solid #1F1F1F';               
        }
        
        if(this.getId('organisatie').value == "") {
            this.getId('organisatie').style.borderLeft = '5px solid #BE1A2B';           
        }
        else {
            this.getId('organisatie').style.borderLeft = '5px solid #1F1F1F';               
        }

        if(this.getId('straat').value == "") {
            this.getId('straat').style.borderLeft = '5px solid #BE1A2B';           
        }     
        else {
            this.getId('straat').style.borderLeft = '5px solid #1F1F1F';               
        }        
        
        if(this.getId('postcode').value == "") {
            this.getId('postcode').style.borderLeft = '5px solid #BE1A2B';           
        }
        else {
            this.getId('postcode').style.borderLeft = '5px solid #1F1F1F';               
        }        

        if(this.getId('plaats').value == "") {
            this.getId('plaats').style.borderLeft = '5px solid #BE1A2B';           
        }        
        else {
            this.getId('plaats').style.borderLeft = '5px solid #1F1F1F';               
        }    
        
        if(this.getId('land').value == "") {
            this.getId('land').style.borderLeft = '5px solid #BE1A2B';
        }        
        else {
            this.getId('land').style.borderLeft = '5px solid #1F1F1F';               
        }    

        if(this.getId('aantal_exemplaren').value == "" || !this.validateInteger()) {     
            this.getId('aantal_exemplaren').style.borderLeft = '5px solid #BE1A2B';
        }
        else {
            this.getId('aantal_exemplaren').style.borderLeft = '5px solid #1F1F1F';               
        }
                   
 
                
                                                
        if( this.getId('naam').value == "" ||
            this.getId('organisatie').value == "" ||
            this.getId('straat').value == "" ||
            this.getId('postcode').value == "" ||
            this.getId('plaats').value == "" ||
            this.getId('land').value == "" ||
            this.getId('aantal_exemplaren').value == "" || 
            !this.validateInteger()) {  
                                                
                
              // dit gedeelte is leuk maar eigenlijk overbodig omdat je al je validate on the fly doet.  
              //this.getFeedbackToUser();        // get feedback to user
              //this.setFeedbackToUser();        // set feedback to user
              
              this.getId('bestelSubmit').disabled = true;              
              this.getId('bestelSubmit').style.cursor = 'default';
        }
        else {
            this.getId('bestelSubmit').style.cursor = 'pointer';                                     
            this.getId('bestelSubmit').disabled = false;    
        } 
        
    } 
    
    
    // validate input for integer
    this.validateInteger = function() {   

        var val;
        val = getId('aantal_exemplaren').value; 
        for (var i = 0; i < val.length; i++) {                                            
            var ch = val.charAt(i)
            if (i == 0 && ch == "-") {
                continue
            }
            if (ch < "0" || ch > "9") { 
                return false;
            }
        }
        
        return true;    
    }
    
    
}



// object cSave
function cSave() {   

    /**
    * @desc save data to module doneren
    */
    this.saveInput = function() { 
            
        // bouw array met form values    
        var aForm = new Array();
        for(i=0; i<document.publicatieBestel.elements.length; i++) {
            aForm[i] = document.publicatieBestel.elements[i].value;        
        }    

        
        // define vars
        this.sBedrag = aForm[2];
        this.purchaseId = aForm[3];
        this.iPublicatieId = aForm[18];
        this.sNaam = aForm[19];
        this.sOrganisatie = aForm[20];
        this.sStraat = aForm[21];
        this.sPostcode = aForm[22];
        this.sPlaats = aForm[23];
        this.sLand = aForm[24];
        this.iAantal = aForm[25];
        this.hash = document.getElementById('hash').value;
        
        
       
        // ajax request naar saveBestelling.php
        var oReq = new cRequest();                                      
        var sGet = "&bedrag="+ this.sBedrag +"&hash=" + this.hash +"&purchaseid="+ this.purchaseId +"&pubid="+ this.iPublicatieId +"&aantal="+ this.iAantal +"&naam=" + this.sNaam +"&organisatie=" + this.sOrganisatie +"&straat=" + this.sStraat +"&postcode=" + this.sPostcode +"&plaats=" + this.sPlaats + "&land=" + this.sLand;

        oReq.sUrl = "http://www.wbs.nl/ideal/saveBestelling.php?type=ideal"+sGet;                
        oReq.sType = 'GET';                
        
        oReq.getContent().responseText;
  
        
        this.setFormAction();
    }     
    
    
    
    // set the action url
    this.setFormAction = function() {    
        // submit form (go to ideal)                                                           
        //getId('publicatieBestel').action = 'http://www.wbs.nl/publicaties';            
        //getId('publicatieBestel').action = 'https://idealtest.secure-ing.com/ideal/mpiPayInitIng.do';
        getId('publicatieBestel').action = 'https://ideal.secure-ing.com/ideal/mpiPayInitIng.do';
        document.publicatieBestel.submit();        
    
    }


}

