var PATH = '';
var statusimg=new Array();
statusimg[-1]= PATH + "/images/error.png";
statusimg[0]= PATH + "/images/blank.png";
statusimg[1]= PATH + "/images/ok.png";

var last_info="";

var notValidMessage	= "Il ne sont pas remplis correctement!";
var notAllMessage	= "Moyens de remplir tous les champs!";
var mustAgreeTerms	= "Doivent accepter les conditions d'utilisation!";

function set_status(n,id)
{
// -1 : error
// 0: blank
// 1: ok

document.getElementById(id+"_status").src=statusimg[n];
/*switch (n)
{
	case -1: document.getElementById(id).style.background="red"; break;
	default: document.getElementById(id).style.background="#ffffff"; break;
}   */

}
function docVal(id)
{return document.getElementById(id).value;}



function over(obj)
{
	var length=obj.src.length;
	var index_ext=0;
	while (obj.src.indexOf('.',index_ext+1)!=-1)
	 {
		index_ext=obj.src.indexOf('.',index_ext+1);
	 }
	var extension=obj.src.substring( index_ext , length);
	obj.src=(obj.src.substring(0,index_ext)+"_over"+extension);
}
function out(obj)
{
	var length=obj.src.length;
	var index_ext=0;
	while (obj.src.indexOf('.',index_ext+1)!=-1)
	 {
	 index_ext=obj.src.indexOf('.',index_ext+1);
	 }
	var extension=obj.src.substring( index_ext , length);
	var idx_ov=0;
	obj.src=(obj.src.substring(0,index_ext-5)+extension);
}
 function toggle_content(event, obj, default_val) {
	if (event.type=="focus") {
		if (obj.value==default_val) {
			obj.value="";
		}
	} else
	if (event.type=="blur") {
		if (obj.value=="") {
			obj.value=default_val;
		}
	}
}

function switch_img(source, id) {
	document.getElementById(id).src = source;
}

function clearDefault(el) {
  if (el.defaultValue==el.value) el.value = "";
}

function keyUpHandler(e) {
	var evt = e || window.event;
	if (evt.keyCode == 13) {	
		if (isIE()) {
			document.getElementById('search_button').click();
		} else {
			var fireOnThis = document.getElementById('search_button');
			var evObj = document.createEvent('MouseEvents');
			evObj.initEvent( 'click', true, true );
			fireOnThis.dispatchEvent(evObj);
		}
	}
}

function searchCountrycode(obj) {
	$.get(PATH + "/search_countrycode.php", {number: obj.value , fr_format : true},
			function(data){
		// update prices to current country
		$('#result').html(data);
	},'html');
}
function selectCountry(obj) {
	$.getJSON(PATH + "/select_country.php", {id: obj.value , fr_format : true},
	  function(data){
	    // update prices to current country
		//$("#country_text1").html(data.country);
		//$("#country_text2").html(data.country);
		//$("#country_text3").html(data.country);
		$('#country_price1').html( (data.price1 == 0 )?'GRATIS':data.price1+"€/mn*");
		//$('#country_price2').html( (data.price2 == 0 )?'GRATIS':data.price2+"€/mn*");
		//$('#country_price3').html( (data.price3 == 0 )?'GRATIS':data.price3+"€/mn*");
	  });
}

function selectCountryBuy(obj) {
	$.getJSON(PATH + "/select_country.php", {id: obj.value},
	  function(data){
	    // update prices to current country
		$('#country_price1').attr('value', data.price1);
		//$('#country_price2').attr('value', data.price2);
		//$('#country_price3').attr('value', data.price3);
		updateMinutes();
	  });

}
function selectMontantValue(value){
	var total = 5;
	for (i=1;i<=total;i++) {
		$("#m"+i+"_status").removeClass("selected");
	}
	$("#m"+value+"_status").addClass("selected");
	var value = Number($("#m"+value).attr('value')).toFixed(2);
	$("#price").html(value);
	$("#price2").html(value);
	var value_vat = value * 0.15;
	value_vat = value_vat.toFixed(2);
	$("#price_vat").html(value_vat);
	var value_total = value * 1.15;
	value_total = value_total.toFixed(2);
	$("#price_total").html(value_total);
	$("#value").attr("value",value);
	updateMinutes();
}

function updateMinutes() {
	var min_bronze, min_silver, min_gold, value;
	value = Number($('#price').html());
	var price1, price2, price3;
	price1 = Number($('#country_price1').attr('value'));
	//price2 = Number($('#country_price2').attr('value'));
	//price3 = Number($('#country_price3').attr('value'));
	min_bronze = getMinutes(value,price1);
	//min_silver = getMinutes(value,price2);
	//min_gold = getMinutes(value,price3);
	
	$('#min_bronze').attr('value',min_bronze);
	//$('#min_silver').attr('value',min_silver);
	//$('#min_gold').attr('value',min_gold);
}

function getMinutes(amount, price) {
	if (price == 0) {
		return 99999;		
	}
	var min = amount / price;
	return Math.round(min);
}
function isIE()
{
	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

function submitCC(val) {
	$.getJSON(PATH + "/user/buy-credits-cc.php", {value: val , fr_format : true},
			function(data){
		// update prices to current country
		$('#cc_sign').attr('value',data.sign);
		$('#cc_data').attr('value',data.data);
		if (data.message && data.message.length>0){
			$("ul.montant li.error").html(data.message);
			tb_remove();
		}else{
			sendCC_click();
		}
	},'html');
}
function sendCC_click() {
	$(document).focus(function() {				
             location.href = 'buy-credits-thankyou.php';
           });
	document.cc_submit.submit();
};

function password(oldObject) {
	var newObject = document.createElement('input');
	newObject.type = 'password';
	if(oldObject.size) newObject.size = oldObject.size;
	newObject.value = '';
	if(oldObject.name) newObject.name = oldObject.name;
  if(oldObject.tabindex) newObject.tabindex = oldObject.tabindex;
	if(oldObject.id) newObject.id = oldObject.id;
	if(oldObject.className) newObject.className = oldObject.className;
	oldObject.parentNode.replaceChild(newObject,oldObject);
  newObject.focus();
  newObject.select();
}

