var browser='';
var ua='';
var browsername='';
var browserversion='';
var ismsie=false;
var isfirefox=false;
var isopera=false;
var issafari=false;
var ischrome=false;

function getbrowser(){
	ua=navigator.userAgent.toLowerCase();
	sv=false ;
 	if(ua.indexOf('msie')!=-1){
		browsername='msie';
		ismsie=true;
		sv='; msie';
	}
 	else if(ua.indexOf('firefox')!=-1){
		browsername='firefox';
		isfirefox=true;
		sv='firefox/';
	}
	else if(ua.indexOf('opera/')!=-1){
		browsername='opera';
		isopera=true;
		sv='opera/';
	}
	else if(ua.indexOf('chrome/')!=-1){
		browsername='chrome';
		ischrome=true;
		sv='chrome/';
	}
	else if(ua.indexOf('safari/')!=-1){
		browsername='safari';
		issafari=true;
		sv='version/';
	};
	if(sv){
		a=ua.split(sv);
		if(a[1]){
			b=a[1].split('.');
			if (b[0]){
				browserversion=parseInt(b[0]);
			};
		};
	};
	browser=browsername+browserversion;
	/*document.write(browser);*/
};

getbrowser();


function getCookie(Name){
	var re=new RegExp(Name+"=[^;]+", "i");
	if (document.cookie.match(re)){
		return document.cookie.match(re)[0].split("=")[1]
	}
	return null;
};

function setCookie(name,value){
	document.cookie=name+"=" + value + ";path=/";
};


var email_subjects=new Array(
	'Question about phones',
	'Question about payments',
	'Question about shipping',
	'Question about order',
	'Payment notification (Western Union)',
	'Payment notification (Bank transfer)',
	'I want to return the phone',
	'Wholesale',
	'Other...'
);

function select_email_subject(a) {
	if(a==0){
		if(document.getElementById('subject').value.length==0) {
			html='<select id="subject" name="message[subject]" style="width:100%;" onchange="select_email_subject(1);">'
				+'<option value="">Please select one...</option>';
			for(i in email_subjects){
				html+='<option value="'+email_subjects[i]+'">'+email_subjects[i]+'</option>';
			};
			html+='</select>';
			document.getElementById('td_subject').innerHTML=html;
		};
	}
	else {
		if(document.getElementById('subject').value=='Other...') {
			document.getElementById('td_subject').innerHTML='<input type="text" name="message[subject]" id="subject" value="" style="width: 100%;" onkeyup="select_email_subject(0);">';
		};
	};
};


function fix_tables(){
	var fixHeight=0;
	var fixWidth=0;
	var currentHeight=parseInt(document.getElementById('add_to_cart_box').offsetHeight);
	var currentWidth=parseInt(document.getElementById('add_to_cart_box').offsetWidth);
	if(browsername=='msie'){
		fixWidth=2;
		fixHeight=2;
	};
	if(currentHeight<=222+fixHeight){
		fixHeight=222+fixHeight;
	}
	else if(currentHeight<=298+fixHeight){
		fixHeight=298+fixHeight;
	}
	else if(currentHeight<=374+fixHeight){
		fixHeight=374+fixHeight;
	}
	else if(currentHeight<=450+fixHeight){
		fixHeight=450+fixHeight;
	}
	else if(currentHeight<=526+fixHeight){
		fixHeight=526+fixHeight;
	}
	else if(currentHeight<=602+fixHeight){
		fixHeight=602+fixHeight;
	};
	if(fixWidth){
		document.getElementById('add_to_cart_box').style.width=currentWidth+fixWidth+'px';
	};
	if(fixHeight){
		document.getElementById('add_to_cart_box').style.height=fixHeight+'px';
	};
};


var _current_tab='';
function select_tab(tab){
	if(_current_tab){
		try{document.getElementById('tablink_' + _current_tab).className='tab';}catch(e){};
		document.getElementById('tabcontent_' + _current_tab).style.display='none';
	};
	try{document.getElementById('tablink_' + tab).className='tab_current';}catch(e){};
	try{document.getElementById('tabcontent_' + tab).style.display='table';}catch(e){document.getElementById('tabcontent_' + tab).style.display='block';};
	try{document.getElementById('current_tab').value=tab;}catch(e){};
	try{document.forms[0].elements[0].focus();}catch(e){};
	_current_tab = tab;
	return false;
};

var decimals=2;
var dec_point=".";
var thousands_sep=",";
var price_string_1="";
var price_string_2="";

function is_int(x) {
	if(x=="0"||x=="1"||x=="2"||x=="3"||x=="4"||x=="5"||x=="6"||x=="7"||x=="8"||x=="9"||
	x==0||x==1||x==2||x==3||x==4||x==5||x==6||x==7||x==8||x==9){
		return true;
	};
	return false;
};

function calc_product_price(price,tt_price){
	var qty=remove_number_format(document.getElementById('quantity').value);
	var totalPrice=price*qty;
	var totalttPrice=tt_price*qty;
	var savedAmount=totalPrice-totalttPrice;
	get_price_format(document.getElementById("total_price").innerHTML);
	document.getElementById("total_price").innerHTML=price_string_1+number_format(totalPrice,decimals,dec_point,thousands_sep)+price_string_2;
	document.getElementById("total_tt_price").innerHTML=price_string_1+number_format(totalttPrice,decimals,dec_point,thousands_sep)+price_string_2;
	document.getElementById("saved_amount").innerHTML=price_string_1+number_format(savedAmount,decimals,dec_point,thousands_sep)+price_string_2;
};

function get_price_format(nr){
	decimals=0;
	dec_point="";
	thousands_sep="";
	price_string_1="";
	price_string_2="";
	var clean_nr="";
	for(i=0;i<nr.length;i++){
		_c=nr.charAt(i);
		if(_c=="."||_c==","||_c=="'"||is_int(_c)){
			if(_c==","||_c=="'"){
				thousands_sep=_c;
			}
			else if(_c=="."){
				dec_point=_c;
				clean_nr+=_c;
			}
			else{
				clean_nr+=_c;
				if(dec_point!=""){
					decimals++;
				}
			}
		}
		else{
			if(clean_nr==""){
				price_string_1+=_c;
			}
			else{
				price_string_2+=_c;
			}
		}
	};
	return clean_nr;
};


function remove_number_format(nr){

	/*nr=replaceAll(nr,"'","");
	nr=replaceAll(nr,",","");
	nr=replaceAll(nr," ","");
	nr=parseInt(nr);
	*/
	var clean_nr="";
	for(i=0;i<nr.length;i++){
		_c=nr.charAt(i);
		if(_c=="."||is_int(_c)){
			clean_nr+=_c;
		}
	}
	return clean_nr;
};

function replaceAll(str,find,repl) {
	return str.replace(new RegExp(find,'g'),repl);
};

function calc_order_price(pid,price,qtyObj){
	qty = parseInt(qtyObj.value);
	if(!qty>=1){
		qty=1;
	};
	curr_price=remove_number_format(document.getElementById('price_total_' + pid ).innerHTML);
	curr_price_total=remove_number_format(document.getElementById('products_price_total').innerHTML);
	curr_qty_total=parseInt(remove_number_format(document.getElementById('total_quantity').innerHTML));
	new_price=qty*price;
	curr_qty=Math.floor(curr_price/price);
	new_price_total=(parseInt(curr_price_total))+(new_price-curr_price);
	new_qty_total=(curr_qty_total)+(qty-curr_qty);

	get_price_format(document.getElementById('price_total_' + pid ).innerHTML);
	document.getElementById('price_total_' + pid ).innerHTML=price_string_1+number_format(new_price,decimals,dec_point,thousands_sep)+price_string_2;

	get_price_format(document.getElementById('products_price_total').innerHTML);
	document.getElementById('products_price_total').innerHTML=price_string_1+number_format(new_price_total,decimals,dec_point,thousands_sep)+price_string_2;

	document.getElementById('total_quantity').innerHTML=new_qty_total;
};

function extra_checked(obj,price ) {
	get_price_format(document.getElementById("extras_price_total").innerHTML);
	extras_total=remove_number_format(document.getElementById("extras_price_total").innerHTML);
	if (obj.checked) {
		document.getElementById("extras_price_total").innerHTML = price_string_1+number_format(parseInt(extras_total)+price,decimals,dec_point,thousands_sep)+price_string_2;
	}
	else {
		document.getElementById("extras_price_total").innerHTML = price_string_1+number_format(parseInt(extras_total)-price,decimals,dec_point,thousands_sep)+price_string_2;
	};
		//document.getElementById("extras_price_total").innerHTML = price_string_1;

};

function isNumber(x) {
	return ( (typeof x === typeof 1) && (null !== x) && isFinite(x) );
};


function number_format(number,decimals,dec_point,thousands_sep) {
	var n=number, prec=decimals;
	var toFixedFix=function(n,prec) {
		var k=Math.pow(10,prec);
		return(Math.round(n*k)/k).toString();
	};
	n=!isFinite(+n)?0:+n;
	prec=!isFinite(+prec)?0:Math.abs(prec);
	var sep=(typeof thousands_sep==='undefined')?',':thousands_sep;
	var dec=(typeof dec_point==='undefined')?'.':dec_point;
	var s=(prec>0)?toFixedFix(n,prec):toFixedFix(Math.round(n),prec);
	var abs=toFixedFix(Math.abs(n),prec);
	var _,i;
	if(abs>=1000){
		_=abs.split(/\D/);
		i=_[0].length%3||3;
		_[0]= s.slice(0,i+(n<0))+
		_[0].slice(i).replace(/(\d{3})/g, sep+'$1');
		s=_.join(dec);
		}
	else {
		s=s.replace('.', dec);
	};
	var decPos=s.indexOf(dec);
	if(prec>=1&&decPos!==-1&&(s.length-decPos-1)<prec){
		s+=new Array(prec-(s.length-decPos-1)).join(0)+'0';
	}
	else if(prec>=1&&decPos===-1){
		s+=dec+new Array(prec).join(0)+'0';
	};
	return s;
};

