/*
  $Id: general.js,v 1.3 2003/02/10 22:30:55 hpdl Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/

function showFlash(index) {
	document.getElementById('banner1').style.display = 'none';
	document.getElementById('banner2').style.display = 'none';
	document.getElementById('banner3').style.display = 'none';
	document.getElementById('banner4').style.display = 'none';
	document.getElementById('banner'+index).style.display = 'block';
}
function launch(URL, newName, newFeatures, orgName) {
  var remote = open(URL, newName, newFeatures);
  if (remote.opener == null)
    remote.opener = window;
  remote.name = orgName;
  //remote.opener.name = orgName;
  return remote;
}

function openWindow(adresse,fwidth,fheight,fname) {
 pwidth = ( fwidth ? fwidth : 400 );
 pheight = ( fheight ? fheight : 400 );
 pname = ( fname ? fname : 'popup');
 Popup = launch(adress, pname, 'dependent=0,left=50,height=' + pheight + ',width=' + pwidth + ',top=50,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=auto,status=yes,toolbar=0','help');
}

function demarche(page) { window.open(page,'demarche','width=400,height=400,toolbar=false,scrollbars=false');	 }
function calculette(page) { window.open(page,'calculette','width=600,height=495,toolbar=false,scrollbars=false');	}

function popuprnp(){
	var win2 = window.open("http://www.receiveandpay.com/wallet/customer/popup3x.php?logo=3680" ,'popup','height=956,width=616,status=yes,scrollbars=yes,menubar=no,resizable=yes');
}
function SetFocus(TargetFormName) {
  var target = 0;
  if (TargetFormName != "") {
    for (i=0; i<document.forms.length; i++) {
      if (document.forms[i].name == TargetFormName) {
        target = i;
        break;
      }
    }
  }

  var TargetForm = document.forms[target];
    
  for (i=0; i<TargetForm.length; i++) {
    if ( (TargetForm.elements[i].type != "image") && (TargetForm.elements[i].type != "hidden") && (TargetForm.elements[i].type != "reset") && (TargetForm.elements[i].type != "submit") ) {
      TargetForm.elements[i].focus();

      if ( (TargetForm.elements[i].type == "text") || (TargetForm.elements[i].type == "password") ) {
        TargetForm.elements[i].select();
      }

      break;
    }
  }
}

function RemoveFormatString(TargetElement, FormatString) {
  if (TargetElement.value == FormatString) {
    TargetElement.value = "";
  }

  TargetElement.select();
}

function CheckDateRange(from, to) {
  if (Date.parse(from.value) <= Date.parse(to.value)) {
    return true;
  } else {
    return false;
  }
}

function IsValidDate(DateToCheck, FormatString) {
  var strDateToCheck;
  var strDateToCheckArray;
  var strFormatArray;
  var strFormatString;
  var strDay;
  var strMonth;
  var strYear;
  var intday;
  var intMonth;
  var intYear;
  var intDateSeparatorIdx = -1;
  var intFormatSeparatorIdx = -1;
  var strSeparatorArray = new Array("-"," ","/",".");
  var strMonthArray = new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
  var intDaysArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  strDateToCheck = DateToCheck.toLowerCase();
  strFormatString = FormatString.toLowerCase();
  
  if (strDateToCheck.length != strFormatString.length) {
    return false;
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strFormatString.indexOf(strSeparatorArray[i]) != -1) {
      intFormatSeparatorIdx = i;
      break;
    }
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strDateToCheck.indexOf(strSeparatorArray[i]) != -1) {
      intDateSeparatorIdx = i;
      break;
    }
  }

  if (intDateSeparatorIdx != intFormatSeparatorIdx) {
    return false;
  }

  if (intDateSeparatorIdx != -1) {
    strFormatArray = strFormatString.split(strSeparatorArray[intFormatSeparatorIdx]);
    if (strFormatArray.length != 3) {
      return false;
    }

    strDateToCheckArray = strDateToCheck.split(strSeparatorArray[intDateSeparatorIdx]);
    if (strDateToCheckArray.length != 3) {
      return false;
    }

    for (i=0; i<strFormatArray.length; i++) {
      if (strFormatArray[i] == 'mm' || strFormatArray[i] == 'mmm') {
        strMonth = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'dd') {
        strDay = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'yyyy') {
        strYear = strDateToCheckArray[i];
      }
    }
  } else {
    if (FormatString.length > 7) {
      if (strFormatString.indexOf('mmm') == -1) {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mm'), 2);
      } else {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mmm'), 3);
      }

      strDay = strDateToCheck.substring(strFormatString.indexOf('dd'), 2);
      strYear = strDateToCheck.substring(strFormatString.indexOf('yyyy'), 2);
    } else {
      return false;
    }
  }

  if (strYear.length != 4) {
    return false;
  }

  intday = parseInt(strDay, 10);
  if (isNaN(intday)) {
    return false;
  }
  if (intday < 1) {
    return false;
  }

  intMonth = parseInt(strMonth, 10);
  if (isNaN(intMonth)) {
    for (i=0; i<strMonthArray.length; i++) {
      if (strMonth == strMonthArray[i]) {
        intMonth = i+1;
        break;
      }
    }
    if (isNaN(intMonth)) {
      return false;
    }
  }
  if (intMonth > 12 || intMonth < 1) {
    return false;
  }

  intYear = parseInt(strYear, 10);
  if (isNaN(intYear)) {
    return false;
  }
  if (IsLeapYear(intYear) == true) {
    intDaysArray[1] = 29;
  }

  if (intday > intDaysArray[intMonth - 1]) {
    return false;
  }
  
  return true;
}

function IsLeapYear(intYear) {
  if (intYear % 100 == 0) {
    if (intYear % 400 == 0) {
      return true;
    }
  } else {
    if ((intYear % 4) == 0) {
      return true;
    }
  }

  return false;
}

function countdown_clock(index, duration, format)
{
//I chose a div as the container for the timer, but
//it can be an input tag inside a form, or anything
//who's displayed content can be changed through
//client-side scripting.
	html_code = '<div id="countdown_' + index + '"></div>';
	document.write(html_code);
	countdown(index, duration, format);
}

function countdown(index, temp_restant, format)
{
	if(temp_restant < 0) {
		window.location.href = window.location.href;
		temp_restant = 0;
		return;
	}
	switch(format)
	{
		case 0:
		    //The simplest way to display the time left.
		    document.getElementById('countdown').innerHTML = temp_restant + ' s';
		    break;
		case 1:
		    //More datailed.
		    Time_Left = temp_restant;
		    days = Math.floor(Time_Left / (60 * 60 * 24));
		    Time_Left %= (60 * 60 * 24);
		    hours = Math.floor(Time_Left / (60 * 60));
		    Time_Left %= (60 * 60);
		    minutes = Math.floor(Time_Left / 60);
		    seconds = Time_Left;
		    seconds %= 60;

		    dps = 's'; hps = 's'; mps = 's'; sps = 's';
		    //ps is short for plural suffix.
		    if(days == 1) dps ='';
		    if(hours == 1) hps ='';
		    if(minutes == 1) mps ='';
		    if(seconds == 1) sps ='';

		    //document.all.countdown.innerHTML = days + ' jours' + dps + ' ';
		    eval('document.getElementById(\'countdown_' + index + '\').innerHTML = \'VENTE FLASH : \';');
		    eval('document.getElementById(\'countdown_' + index + '\').innerHTML += (days > 0?days + \' jour'+ dps +'<br />\':\'\');');
		    eval('document.getElementById(\'countdown_' + index + '\').innerHTML += (hours < 10?\'0\' + hours:hours) + \'h \';');
		    eval('document.getElementById(\'countdown_' + index + '\').innerHTML += (minutes < 10?\'0\' + minutes:minutes) + \'m \'');
		    eval('document.getElementById(\'countdown_' + index + '\').innerHTML += (seconds < 10?\'0\' + seconds:seconds) + \'s\'');
		    //eval('document.all.countdown_' + index + '.innerHTML') += (seconds < 10?'0' + seconds:seconds);
		    break;
		default: 
		    document.getElementById('countdown').innerHTML = temp_restant + ' s';
	}
     
//Recursive call, keeps the clock ticking.
	setTimeout('countdown(' + index + ',' + (temp_restant - 1) + ',' + format + ');', 1000);
}


function popupWindowx(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=450,height=280,screenX=150,screenY=150,top=150,left=150')
}

	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;
	}

	// Node cleaner
	function go(c){
		if(!c.data.replace(/\s/g,''))
			c.parentNode.removeChild(c);
	}

	function clean(d) {
		var bal=d.getElementsByTagName('*');

		for(i=0;i<bal.length;i++){
			a=bal[i].previousSibling;
			if(a && a.nodeType==3)
				go(a);
			b=bal[i].nextSibling;
			if(b && b.nodeType==3)
				go(b);
		}
		return d;
	} 

function refresh1Euro(amount, option) {
  var url = displayUrl1Euro(amount, option);
  document.getElementById('uneuroFrame').innerHTML = '<iframe height="50" scrolling="no" style="width: 230px; border-width: 0px; margin-left: -10px;" frameborder="0" src="' + url + '">' + url + '</iframe>';
  return false;
}
function displayUrl1Euro(amount, option) {
  return 'https://www.1euro.com/1euro/calculettePartenaire.do?idPartenaire=2671777&montant=' + amount + '&option=' + option;
}

function refresh1Presto(amount, option) {
  /*var url = displayUrl1Euro(amount, option);
  document.getElementById('prestoFrame').innerHTML = "<iframe width='290' height='40' scrolling='no' style='border-width: 0px' frameborder=0 src='" + url + "'></iframe>";
  return true;*/
	var xhr = getXhr();
	// On défini ce qu'on va faire quand on aura la réponse
	xhr.onreadystatechange = function() {
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200) {
			response = clean(xhr.responseXML.documentElement);
			var items = response.getElementsByTagName("produit");
			var html = ''; /* Présentation HTML de la liste des DVD */
			var titre = 'Règlement en ';
			/* Nombre de DVD */
			count = items.length;
			for(i = 0; i < count; i++) { /* POUR CHAQUE item */
				titre += '<a href="javascript: void(0);" onclick="javascript: refresh1Presto(\'' + amount + '\', ' + i + '); return false;">' + items[i].getElementsByTagName("duree")[0].firstChild.nodeValue + ' fois</a> ';
				if ( i == option ) {
					html += '<br /><span style="font-family: Verdana, Arial, sans-serif; font-size: 10px; color: #004080;">Payez ce produit <b>' + items[i].getElementsByTagName("mensualite")[0].firstChild.nodeValue + '€ x ' + items[i].getElementsByTagName("duree")[0].firstChild.nodeValue + '</b>';
					html += '<br />(Coût du crédit ' + items[i].getElementsByTagName("cout")[0].firstChild.nodeValue + '€ TEG annuel fixe ' + items[i].getElementsByTagName("teg")[0].firstChild.nodeValue + '%)</font>';
				}
			}
			//alert(titre + html);
			document.getElementById("prestoFrame").innerHTML = '<span class="smallText">' + titre + html + '</span>';
		}
	}
	xhr.open("GET","presto/simulation" + amount + ".xml",true);
	xhr.send(null);

  //document.getElementById('prestoFrame').innerHTML = "<iframe width='290' height='40' scrolling='no' style='border-width: 0px' frameborder=0 src='" + url + "'></iframe>";
  return true;
}
/*function displayUrl1Euro(amount, option) {
  return 'https://www.1euro.com/1euro/calculettePartenaire.do?idPartenaire=2671777&montant=' + amount + '&option=' + option;
}*/

function change_tab(onglet) {	
	document.getElementById('content_1').style.display = 'none';
	if ( document.getElementById('content_2') ) document.getElementById('content_2').style.display = 'none';
	if ( document.getElementById('content_3') ) document.getElementById('content_3').style.display = 'none';		
	document.getElementById('content_' + onglet).style.display = 'block';		

	document.getElementById('tab_1').className = '';
	if ( document.getElementById('tab_2') ) document.getElementById('tab_2').className = '';
	if ( document.getElementById('tab_3') ) document.getElementById('tab_3').className = '';		
	document.getElementById('tab_' + onglet).className = 'active';		
}

function showerror(field_name, message) {
  if (document.getElementById('zone_' + field_name)) {
  	$('#zone_' + field_name).toggleClass('error', true);
  }
  if (document.getElementById('error_msg_' + field_name)) document.getElementById('error_msg_' + field_name).innerHTML = message;
}
function hide_error(field_name) {
  //
  if (document.getElementById('zone_' + field_name)) {
  	$('#zone_' + field_name).toggleClass('error', false);
  }
}
