function strpos( haystack, needle, offset){
	offset = offset || 0;
	var i = haystack.indexOf( needle, offset );
	return i >= 0 ? i : false;
}
function trim(s) {
  s = s.replace( /^\s+/g, '');
  return s.replace( /\s+$/g, '');
}
function cart_data(str,amount, test) 
{
	
	hrhame = "basket.php";
	trnd = "rnd";
	if( typeof(volvopenta)!='undefined' ) {
		hrhame = volvopenta;
		trnd = "basket";		
	}
	
	if( test == -999 ) {
		price = str;
		price = parseFloat(number_format(price,2,'.',''));
		amo += parseInt(amount);
		test = 1;
	} else if (test != 0) {
		price2 = str; 
		price2 = parseFloat(number_format(price2,2,'.',''));		
		price += price2;
		price = parseFloat(number_format(price,2,'.',''));
		amo += parseInt(amount);
		test += parseInt(test);
	} else {
		price = str;
		price = parseFloat(number_format(price,2,'.',''));
		amo = parseInt(amount);
		test += parseInt(test);
	}
	z = document.getElementById('divBasket');
	hreff = hrhame + '?' + trnd + '=' + Math.floor( Math.random( ) * (10000+1) );
	z.innerHTML = '<font class=\"basket\">В корзине товаров: '+amo+' шт.<br>Общей стоимостью: '+price+'&nbsp;'+edizm+'</font><br><img src=\"img/dot.gif\" width=1 height=15 alt=\"\" border=0><br><div align=left><a href=\"'+hreff+'\" class=basket>Оформить заказ</a></div>';
}

function init_cart(amount, price, test)
{ 
	if (price!='0')
	{
		cart_data(amount,price, test);
	} else {
		z = document.getElementById('divBasket');
		z.innerHTML = '<b>Ваша корзина пуста.<b>';
	}
}

function send_cart(str)
{
    var id = (new Date().getTime());
    var href="request.php?rnd="+id+'&data='+str; //alert(href);
	with (document) {
             var span = null;
             span = body.appendChild(createElement("SPAN"));
             span.style.display = 'none';
             span.innerHTML = 'Text for stupid IE.<s'+'cript></' + 'script>';
             setTimeout(function() {
                 var s = span.getElementsByTagName("script")[0];
                 s.language = "JavaScript";
                 if (s.setAttribute) s.setAttribute('src', href); else s.src = href;
             }, 10);
             this._id = id;
             this._span = span;
         }
}

// 
function minus(source){
	element0 = 'basket['+source+']';	
	a1 = document.form1[element0].value;
	if (Number(a1)){ $a = Number(a1); }
	else{ $a = 1; }	
	if($a>1){ document.form1[element0].value=$a-1; total = total - 1; price_summ (source); cart_data(-goods_list[source],-1); }
}

function plus(source){
	element0 = 'basket['+source+']';
	a1 = document.form1[element0].value;
	if (Number(a1)){ a = Number(a1); }
	else{ a = 0; }		
	document.form1[element0].value=a+1;
    total = total + 1;
	price_summ (source);
	
	cart_data(goods_list[source],1);
}

function dell(source,max){
	element0 = 'basket['+source+']';
	a = 0; 
	document.form1[element0].value=a;
	price_summ (source);
}

function price_summ(goods_id)
{
	if (goods_id)
	{
		cell_name = 'total_'+goods_id; 
		basket_name = 'basket['+goods_id+']';
		bsk=document.getElementsByName(basket_name);
		bsk=bsk[0];
		summ = bsk.value*goods_list[goods_id];
		summ = (!summ ? 0 : summ );
		summ = Math.round(summ*100);
		summ = summ/100;
		cell=document.getElementById(cell_name);
		cell.innerHTML =summ + '&nbsp;'+edizm; // cart_data(-goods_list[goods_id],'',-888);
	}
	var js_total = 0;
	for (i in goods_list) { basket_name = 'basket['+i+']'; js_total = js_total + goods_list[i]*document.form1[basket_name].value; }
	total_all_cost = Math.round(js_total*100);
	total_all_cost = total_all_cost/100;
	total_all_cost = (!total_all_cost ? 0 : total_all_cost );
	document.getElementById("total_all").innerHTML = total_all_cost + '&nbsp;'+edizm;	//alert(document.form1[basket_name].value);//cart_data(total_all_cost,document.form1[basket_name].value,0);
}

function ValidateForm() 
	{
////////////////////////////////////////////////////////////////////////		kto
		if (!document.forms['orderform'].name.value)
			{
				a = document.getElementsByTagName('div')[1];
				a.innerHTML = '<FONT COLOR=#FF0000><B>'+lico.innerText+'</B></FONT>';
				document.forms['orderform'].elements['name'].focus();
				return false;
			}
		else
			{
				a = document.getElementsByTagName('div')[1];
				a.innerHTML = '<B>'+lico.innerText+'</B>';
				document.forms['orderform'].elements['to'].focus();
			}
		
////////////////////////////////////////////////////////////////////////		kuda
		if (!document.forms['orderform'].to.value)
			{
				a = document.getElementsByTagName('div')[2];
				a.innerHTML = '<FONT COLOR=#FF0000><B>'+to.innerText+'</B></FONT>';
				document.forms['orderform'].elements['to'].focus();
				return false;
			}
		else
			{
				a = document.getElementsByTagName('div')[2];
				a.innerHTML = '<B>'+to.innerText+'</B>';
				document.forms['orderform'].elements['fone'].focus();
			}

////////////////////////////////////////////////////////////////////////   fone
		if (!document.forms['orderform'].fone.value)
			{
				a = document.getElementsByTagName('div')[3];
				a.innerHTML = '<FONT COLOR=#FF0000><B>'+tel.innerText+'</B></FONT>';
				document.forms['orderform'].elements['fone'].focus();
				return false;
			}
		else
			{
				a = document.getElementsByTagName('div')[3];
				a.innerHTML = '<B>'+tel.innerText+'</B>';
				document.forms['orderform'].elements['email'].focus();
			}
////////////////////////////////////////////////////////////////////////		mail
		var mail = document.forms['orderform'].email.value.toString();
		if (!EMail_Validator(mail))
			{
				z = document.getElementsByTagName('div')[4];
				z.innerHTML = '<B><FONT COLOR=#FF0000>E-mail:</FONT></B>';
				document.forms['orderform'].email.focus();
				return false; 
			}
		return true;
	}
function EMail_Validator(email_addr) {
 var re, re2;
 re = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(\.$)/; 
	re2 = /^.+\@\[?(\w|[-.])+\.[a-zA-Z]{2,3}|[0-9]{1,3}\]?$/;
 if (email_addr.search(re) == -1 ) {
	 if (email_addr.search(re2) != -1) return(true);
 }
}

function proverka(id) {
	pr = document.getElementById(id);
	text = pr.value;
	len = parseInt(text.length);
	ch = text.substr(len-1,1); 
	if( len > 1 )
		re = /^[0-9]$/;
	else
		re = /^[1-9]$/;
	
	
	if( len == 1 && ch.search(re) == -1 )
		text = 1;
	else if( ch.search(re) == -1 )
		text = text.substr(0,len-1);
	
	pr.value = text;
}

function number_format( number, decimals, dec_point, thousands_sep ) {    // Format a number with grouped thousands
    // 
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://crestidg.com)
 
    var i, j, kw, kd, km;
 
 	if( number == parseInt(number) ) return number;
 	
    // input sanitation & defaults
    if( isNaN(decimals = Math.abs(decimals)) ){
        decimals = 2;
    }
    if( dec_point == undefined ){
        dec_point = ",";
    }
    if( thousands_sep == undefined ){
        thousands_sep = ".";
    }
 
    i = parseInt(number = (+number || 0).toFixed(decimals)) + "";
 
    if( (j = i.length) > 3 ){
        j = j % 3;
    } else{
        j = 0;
    }
 
    km = (j ? i.substr(0, j) + thousands_sep : "");
    kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
    //kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : "");
    kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");
 
 
    return km + kw + kd;
}
