/* namespacing object */
var net=new Object();

net.READY_STATE_UNINITIALIZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;


/*--- content loader object for cross-browser requests ---*/
net.ContentLoader=function(url,onload,onerror,method,params,contentType){
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  this.loadXMLDoc(url,method,params,contentType);
}

net.ContentLoader.prototype.loadXMLDoc=function(url,method,params,contentType){
  if (!method){
    method="GET";
  }
  if (!contentType && method=="POST"){
    contentType='application/x-www-form-urlencoded';
  }
  if (window.XMLHttpRequest){
    this.req=new XMLHttpRequest();
  }

  else if(window.ActiveXObject) {
       	try {
        	this.req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		this.req= new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		this.req = null;
        	}
		}
    }

  if (this.req){
   try{
      var loader=this;
      this.req.onreadystatechange=function(){
        net.ContentLoader.onReadyState.call(loader);
      }
      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }
      this.req.send(params);
   }catch (e){
      this.onerror.call(this);
    }
  }
}


net.ContentLoader.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  if (ready==net.READY_STATE_COMPLETE){
    var httpStatus=req.status;
    if (httpStatus==200 || httpStatus==0){
      this.onload.call(this);
    }else{
      this.onerror.call(this);
    }
  }
}

net.ContentLoader.prototype.defaultError=function(){

  alert("error fetching data!"
    +"\n\nreadyState:"+this.req.readyState
    +"\nstatus: "+this.req.status
    +"\nheaders: "+this.req.getAllResponseHeaders());

}

String.prototype.parseJSON = function () {
    try {
        return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                this.replace(/"(\\.|[^"\\])*"/g, ''))) &&
            eval('(' + this + ')');
    } catch (e) {
        return false;
    }
}

	var retPromo;
	
	function validPromo(){
		var pr = document.getElementById('inputCodePromo').value;
		if(pr.length>1){
			document.getElementById('retourPromo').innerHTML = 'validation code en cours ...';
			netPromo(pr);
		}
		else {
			cr = document.getElementById('retourPromo');
			cr.innerHTML = 'Code non renseign&eacute;';
			setTimeout('cr.innerHTML = \'\'',2000);			
			}
	}
	
	
	function netPromo(pr){
	try{
			 //	{"retCode":"1","code":"iusKxRLuZQq","typeReduction":"0","montantPromo":"6"}
			 var req = 'http://www.toutapprendre.com/incV2/codePromo.asp?code=' + pr + '&editeur=' + editeurNum + '&pkCours=' + pkCours;
			 var host = window.location.host;
			 if(host.indexOf('www.')==-1){
			 req = 'http://toutapprendre.com/incV2/codePromo.asp?code=' + pr + '&editeur=' + editeurNum + '&pkCours=' + pkCours;
			 }
			 
			var loader1 = new net.ContentLoader(req,critRetour,null, "GET");	
						
			req = null; loader1 = null;
			return retPromo;
	}catch(e){alert('Erreur ' + e);e=null;}  		
	}

			function critRetour(){
			
		   		var json = (this.req.responseText).parseJSON();
		   		var msg = 'code retour : ' + json["retCode"] + '\ncode validé : ' + json["code"] + '\ntypeReduction : ' + json["typeReduction"] + '\nmontantPromo : ' + json["montantPromo"];
		   		typeReduction = json["typeReduction"];
		   		montant = json["montantPromo"];
		   		lecode =  json["code"];

		   		switch (json["retCode"]){
		   			case '1':
						calcul();
						if(!isNaN(montant) && montant <1){
			   				retPromo = 'Code valide votre promotion sera ajoutée à votre compte après achat.' ;		
		   				}
		   				else {
			   				retPromo = 'Code valide.' ;	
			   			}
			   			majCodeForm (lecode); 			   			
			   			break;
		   			case '2':	
			   			retPromo = 'Code non trouvé.' ;			  
		   				majCodeForm ('');
			   			break
		   			case '3':
		   				retPromo = 'Date de validit&eacute; du code d&eacute;pass&eacute;e.' ;	
		   				majCodeForm ('');
			   			break;
		   			case '4':
			   			retPromo = 'Code d&eacute;ja utilis&eacute;.' ;	
			   			majCodeForm ('');
			   			break;
		   			case '5':
		   				//erreur
		   				retPromo = 'Erreur' ;	
		   				majCodeForm ('');
			   			break;
		   			case '6':
		   				//erreur
		   				retPromo = 'Code non valable pour ce cours' ;	
		   				majCodeForm ('');
			   			break;			   			
					default : 
						// erreur	
						retPromo = 'Erreur' ;
						majCodeForm ('');		
				}
				
		   		affRetourCode(retPromo); // affiche infos retour
				
		   		}
	
	function majPrixPromo(typeReduction, montant){
	   		if(typeReduction.toString() == '0'){
	   				// montant
	   				reduction =' réduction de ' + montant + ' €\n';
	   				// alert('prix : '+ prix + '\nremise: ' + remise + '\nmontant: '+montant);
	  				prixPromo = (prix-remise) - montant;
	  				montantReduction = montant;	  				
	   		}
	   		else if (typeReduction.toString() == '1') {
	   			// % 
	   			reduction =' réduction de ' + montant + ' %\n';
	   			prixPromo = (prix-remise) - (tabPrix[0][1] * montant / 100);
	  			montantReduction = (prix-remise) - prixPromo;		  			
	   		}	
	   		else {
	   			reduction ='';
	   			prixPromo = '';
	   			montantReduction = 0;
	   		}
	   		// reducPromo = montant; 
	   		 reducPromo = Math.round(montantReduction*100) /100 ;
		changeStylePromo();
	}
	
	function affRetourCode(t){
	try{
		cr = document.getElementById('retourPromo');
		cr.innerHTML = t;	
		}catch(e){ alert('affRetourCode erreur : ' + e);e=null;}
	}
	
	
  	function majCodeForm(c){
  	// codePromo formulaire
	  	document.getElementById ('formCodePromo').value = c;  	
  	}
  	
  	function changeStylePromo(){
  		if(!isNaN(montant) && montant > 0){
	 	   	if(document.getElementById('total')){
			   	document.getElementById('total').style.textDecoration = 'line-through';
		   	}
		   	else {
			   	document.getElementById('prixPremier').style.textDecoration = 'line-through';
		   	}
	   	} 	
	   		
  	}


