var testsuccess = 0;
var lnid = new Array();
function categorylist(url)
  {
  self.location = url;
  }

var getresults=function(results)
  {
  document.getElementById('shoppingcartcontents').innerHTML = results;
  document.getElementById('loadingindicator').style.visibility = 'hidden';
  if (document.getElementsByName('buy_3').length > 0)
	  ajax.post("index.php?ajax=true&user=true&getnumtoken=true",updateFreeTokens,'');
  }
  
var updateFreeTokens=function(results)
 {
 	document.getElementById('free_tokens').value = results;
 }
  
function submitform(frm)
  {
	if (frm.buy_3)
	{
		if (parseInt(frm.item_qty.value) > parseInt(frm.free_tokens.value))
		{
			window.alert("There are not enough tokens available.\nTeo by Xiring team has been informed of the situation.\nPlease try again later.");
	  		ajax.post("index.php?ajax=true&user=true&needtoken=true",'','');
			return;
		}
	}
    //alert(ajax.serialize(frm));
  	if(frm.buy_1) {
  		elements = document.getElementsByName('buy_0');
  		if (document.getElementById('slc_buy_0')) {
  			document.getElementById('slc_buy_0').style.display="none"
  		}
  	}
 	else elements = document.getElementsByName('buy_1'); 
  	
  	if(elements.length >=0) {
  		for(i=0;i<elements.length;i++) {
  			elements[i].style.display="none"	
  		}
  	}
  //alert(ajax.serialize(frm));
 	 if (frm.item_qty.value > 0)
  	{
  		ajax.post("index.php?ajax=true&user=true",getresults,ajax.serialize(frm));
  		document.getElementById('loadingimage').src = './wp-content/plugins/wp-shopping-cart/images/indicator.gif';
  		document.getElementById('loadingindicator').style.visibility = 'visible';
  	}
  	else {
  		ajax.post("index.php?ajax=true&user=true",getresults,ajax.serialize(frm));
  		window.location.href = frm.url_more.value;
  	}
  }

function emptycart()
  {
	if (document.getElementById('slc_buy_0')) {
  		document.getElementById('slc_buy_0').style.display="inline"
  		}
  	if (document.getElementsByName('buy_1')) {	
	 	elements = document.getElementsByName('buy_1'); 
	  	if(elements.length >=0) {
	  		for(i=0;i<elements.length;i++) {
	  			elements[i].style.display="inline"	
	  		}
	  	}
  	}
  	if (document.getElementsByName('buy_0')) {	
	 	elements = document.getElementsByName('buy_0'); 
	  	if(elements.length >=0) {
	  		for(i=0;i<elements.length;i++) {
	  			elements[i].style.display="inline"	
	  		}
	  	}
  	}   	  	
  ajax.post("index.php",getresults,"ajax=true&user=true&emptycart=true");
  if (document.getElementById('loadingimage')) {	
  	document.getElementById('loadingimage').src = './wp-content/plugins/wp-shopping-cart/images/indicator.gif';
 	 document.getElementById('loadingindicator').style.visibility = 'visible';
  }
  }

function show_additional_description(id,image_id)
  {
  currentstate = document.getElementById(id).style.display;
  //document.getElementById(id).style.display = 'inline';
  if(currentstate != 'inline')
    {
    document.getElementById(id).style.display = 'inline';
    document.getElementById(image_id).src = './wp-content/plugins/wp-shopping-cart/images/icon_window_collapse.gif';
    }
    else
      {
      document.getElementById(id).style.display = 'none';
      document.getElementById(image_id).src = './wp-content/plugins/wp-shopping-cart/images/icon_window_expand.gif';
      }
  return false;
  }

function prodgroupswitch(state)
  {
  if(state == 'brands')
    {
    document.getElementById('categorydisplay').style.display = 'none';
    document.getElementById('branddisplay').style.display = 'block';
    }
    else if(state == 'categories')
    {
      document.getElementById('branddisplay').style.display = 'none';
      document.getElementById('categorydisplay').style.display = 'block';
    }
  return false;
  }
  
var previous_rating;
function ie_rating_rollover(id,state)
  {
  target_element = document.getElementById(id);
  switch(state)
    {
    case 1:
    previous_rating = target_element.style.background;
    target_element.style.background = "url(./wp-content/plugins/wp-shopping-cart/images/blue-star.gif)";
    break;
    
    default:
    if(target_element.style.background != "url(./wp-content/plugins/wp-shopping-cart/images/gold-star.gif)")
      {
      target_element.style.background = previous_rating;
      }
    break;
    }
  }  
  
var apply_rating=function(results)
  {
  outarr = results.split(",");
  //alert(results);
  for(i=1;i<=outarr[1];i++)
    {
    id = outarr[0]+"and"+i+"_link";
    //document.getElementById(id).src = "./wp-content/plugins/wp-shopping-cart/images/gold-star.gif";
    document.getElementById(id).style.background = "url(./wp-content/plugins/wp-shopping-cart/images/gold-star.gif)";
    }
    
  for(i=5;i>outarr[1];i--)
    {
    id = outarr[0]+"and"+i+"_link";
    //document.getElementById(id).src = "./wp-content/plugins/wp-shopping-cart/images/grey-star.gif";
    document.getElementById(id).style.background = "#c4c4b8";
    }
  lnid[outarr[0]] = 1; 
    
  rating_id = 'rating_'+outarr[0]+'_text';
  //alert(rating_id);
  if(document.getElementById(rating_id).innerHTML != "Your Rating:")
    {
    document.getElementById(rating_id).innerHTML = "Your Rating:";
    }
    
  saved_id = 'saved_'+outarr[0]+'_text';
  document.getElementById(saved_id).style.display = "inline";
  update_vote_count(outarr[0]);
  }
  
function hide_save_indicator(id)
  {
  document.getElementById(id).style.display = "none";
  }
  
function rate_item(prodid,rating)
  {
  ajax.post("index.php",apply_rating,"ajax=true&rate_item=true&product_id="+prodid+"&rating="+rating);
  }
  
function update_vote_count(prodid)
  {
  var update_vote_count=function(results)
    {
    outarr = results.split(",");
    vote_count = outarr[0];
    prodid = outarr[1];
    vote_count_id = 'vote_total_'+prodid;
    document.getElementById(vote_count_id).innerHTML = vote_count;
    }
  ajax.post("index.php",update_vote_count,"ajax=true&get_rating_count=true&product_id="+prodid);
  }
  
 function modif_champ(val) {
	var frmelt = document.getElementById('frmevl');	
	for(i=0;i<frmelt.elements.length; i++) {
		if(val == 0) {
			frmelt.elements[i].removeAttribute("readonly");	
			if (frmelt.elements[i].id=='country_slct') {
				//alert(document.getElementById('country_hdn').value);
				frmelt.elements[i].removeAttribute("disabled");
			}
				
			//alert(frmelt.elements[i].readonly);
			if(frmelt.elements[i].name=="btn") {
				frmelt.elements[i].style.display="none";	
			}
			if(frmelt.elements[i].name=="btn2") {
				frmelt.elements[i].style.display="inline";	
			}
			if (frmelt.elements[i].id=="tva1") {
				frmelt.elements[i].setAttribute("readonly","readonly");
			}
		}
		else {
			frmelt.elements[i].setAttribute("readonly","readonly");
			//alert(frmelt.elements[i].readonly);
			if (frmelt.elements[i].id=='country_slct') {
				document.getElementById('country_hdn').value = frmelt.elements[i].options[frmelt.elements[i].selectedIndex].value;
				//alert(document.getElementById('country_hdn').value);
				frmelt.elements[i].setAttribute("disabled","disabled");
			}
			if(frmelt.elements[i].name=="btn") {
				frmelt.elements[i].style.display="inline";	
			}		
			if(frmelt.elements[i].name=="btn2") {
				frmelt.elements[i].style.display="none";	
			}	
		}
	}
 }
 function modif_val(obj) {
 	document.getElementById('country_hdn').value = obj.options[obj.selectedIndex].value;
 }

 
function matchRegularExpression(valeur, regularExpression)
{
	var resultat = valeur.match(regularExpression);
	if(resultat!=null && resultat.length==1) 
		return true;
	else 
		return false;
}

function doesntMatchRegularExpression(valeur, regularExpression)
{
	if(matchRegularExpression(valeur, regularExpression)) return false;
	else return true;
}
 
function isNotEmail(email)
 {
 	var regExpEmailAdress=/^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/g;
	return doesntMatchRegularExpression(email, regExpEmailAdress);
 }
 function isNotPhone(phone)
 {
 	var regExpTelephone=/^[ 0-9\+\.\-\(\)]*$/g;
	return doesntMatchRegularExpression(phone, regExpTelephone);
 }
 function isNum(n)
 {
 	var regExpNumeric=/^[0-9]+$/g;
 	return matchRegularExpression(n, regExpNumeric);
 }
 function isAlphaNum(n)
 {
 	var regExpAlphaNumeric =/^[0-9a-zA-Z]+$/g;
 	return matchRegularExpression(n, regExpAlphaNumeric);
 }
 function isAlpha(n)
 {
 	var regExpAlpha =/^[a-zA-Z]+$/g;
 	return matchRegularExpression(n, regExpAlphaNumeric);
 }
 function isTVA(tva, iso)
 {
 	tva = tva.replace(/\s/g,"");
 	var l = tva.length;
 	switch (iso)
 	{
 		case 'IE':
 			return (l==8) && isAlphaNum(tva);
 		case 'DK':
 		case 'FI':
		case 'HU':
 		case 'LU':
		case 'MT':
		case 'SI':
 			return (l==8) && isNum(tva);
 		case 'CZ':
 			return (l>=8) && (l<=10) && isNum(tva);
 		case 'CY':
			return (l==9) && isNum(tva.substr(0,8)) && isAlphaNum(tva.charAt(8));
 		case 'ES':
 			return (l==9) && isAlphaNum(tva);
 		case 'DE':
		case 'EE':
 		case 'EL':
 		case 'GR':
 		case 'PT':
 		case 'BE':
 			return (l==9) && isNum(tva);
 		case 'LT':
 			return ((l==9) || (l==12)) && isNum(tva);
 		case 'GB':
 			return ((l==9) && isNum(tva))
 					|| ((l==5) && isNum(tva.substr(2,3)));
 		case 'AT':
 			return (l==9) && (tva.charAt(0)=='U') && isNum(tva.substr(1,8));
 		case 'PL':
		case 'SK':
 			return (l==10) && isNum(tva);
 		case 'LV':
 		case 'IT':
			return (l==11) && isNum(tva);
 		case 'FR':
 			return (l==11) && isNum(tva.substr(2, 9)) && isAlphaNum(tva.substr(0, 2));
 		case 'NL':
 			return (l==12) && isNum(tva.substr(0,9)) && isNum(tva.substr(10,2)) && isAlpha(tva.charAt(9));
 		case 'SE':
 			return (l==12) && isNum(tva);
 		default:
 			return true;
 	}
 }
 
 function checkInfoPersoCreateAccount()
 {
	var isOk = true;
 	var msg="";
	
	if (isNotEmail(document.formulaire.user_email.value))
 	{
 		isOk = false;
 		msg += "- Bad eMail address.\n";
 	}
 
	if (msg.length > 0)
 		window.alert(msg);
 	return isOk;
 }
 
 
 function checkInfoPerso()
 {
 	var isOk = true;
 	var msg="";

 	if (isNotEmail(document.formulaire.email.value))
 	{
 		isOk = false;
 		msg += "- Bad eMail address.\n";
 	}
 	if ((document.formulaire.phone.value != "") && isNotPhone(document.formulaire.phone.value))
 	{
 		isOk = false;
 		msg += "- Bad phone number.\n";
 	}
 	if (document.formulaire.tva1.value != "" || document.formulaire.tva.value != "")
 	{
 		// cas particulier de la corse
 		if ((document.formulaire.country.options[document.formulaire.country.selectedIndex].value == 'F1')
 			&& (document.formulaire.tva1.value != "FR"))
	 	{
	 		isOk = false;
	 		msg += "- Bad VAT number, it should start with FR.\n"; 		
	 	}
	 	else if (document.formulaire.tva1.value != document.formulaire.country.options[document.formulaire.country.selectedIndex].value)
	 	{
	 		isOk = false;
	 		msg += "- Bad VAT number, it should start with "+document.formulaire.country.options[document.formulaire.country.selectedIndex].value+".\n"; 		
	 	}
	 	else
	 	{
	 		if (!isTVA(document.formulaire.tva.value, document.formulaire.tva1.value))
		 	{
		 		isOk = false;
		 		msg += "- Bad VAT number.\n";
		 	}
	 	}
 	}
 	if (document.formulaire.address.value != "" || document.formulaire.city.value != "" || document.formulaire.postal_code.value != "")
 	{
 		if (document.formulaire.address.value == "" || document.formulaire.city.value == "" || document.formulaire.postal_code.value == "")
 		{
 			isOk = false;
 			msg += "- Incomplete address. Line 1, city and zip code must me present.\n";
 		}
 	}
 	if (document.formulaire.password)
 	{
	 	if (document.formulaire.password.value != document.formulaire.password2.value)
	 	{
	 		isOk = false;
	 		msg += "- You entered two different passwords.\n"; 		
	 	}
 	}
 	if (msg.length > 0)
 		window.alert(msg);
 	return isOk;
 }
 
 function askClose()
 {
 	if (confirm("Do you really want to close this token?"))
 	{
 		document.endform.submit();
 	}
 }
 
 function verifNumber()
 {
 	if (document.adjustform.quantity.value > 150)
 	{
 		alert("You can buy 150 packs maximum.");
 		return false;
 	}
 	return true;
 }
 
function ajaxChangePass(frm) {
	var updateMessagePI = function(results) { document.getElementById('msg_password').innerHTML = results; document.form_pass.reset(); }
	ajax.post("index.php?ajax=true&user=true", updateMessagePI, ajax.serialize(frm));
 	return false;
}

function updateLevel(uid) {
	var changeRadio = function(results) {
		document.getElementById('rl_'+results).checked = 'checked';
		document.getElementById('rl_'+results).checked = true;
	}
	ajax.post("index.php?ajax=true&user=true&updateLevel=true&uid="+uid, changeRadio, '');
}
function ajaxChangeLevel(frm) {
	var updateMessage = function(results) {
		if (results == '') {
			window.alert("Your Trial Kit has been registered.")
			updateLevel(frm.user_id.value);
		} else {
			window.alert(results);
		}
	}
	ajax.post("index.php?ajax=true&user=true", updateMessage, ajax.serialize(frm));
 	return false;
}
function popupInfoTrial() {
	window.alert("The serial number is the 3-digit number at the back of the reader, after \"B/N\"." +
			"Once you have registered your Trial Kit, you can directly order the quantity of Pakcs 20 units you need.");
}

function popup(url,w,h) {
	var t = (screen.height - h) / 2;
	var l = (screen.width - w) / 2;
	window.open(url,'','height='+h+',width='+w+',top='+t+',left='+l+',location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');	
}