$.partners=new Object;
$.Oo=function(){
			if($("#blabla").attr("checked"))
			$("#datetime").slideDown('slow');
			else $("#datetime").slideUp('slow');
			}
$.bsurl=function(){
			if($("#bscheck").attr("checked"))
			$("#bsurl").slideDown('slow');
			else $("#bsurl").slideUp('slow');
			}
$.bstime=function(){
			if($("#bstime").attr("checked"))
			$("#bsstamp").slideDown('slow');
			else $("#bsstamp").slideUp('slow');
			}
function formInit(){
		var form=document.getElementById('create_delivery');
		if(form == null) return;
		var errorDiv = document.all ? document.all["errorDiv"] : document.getElementById("errorDiv");
		var numClickType='int';
		var smsLen = document.all ? document.all["smsLen"] : document.getElementById("smsLen");
		var smsLenA = document.all ? document.all["smsLenA"] : document.getElementById("smsLenA");
		var smsText = document.all ? document.all["smsText"] : document.getElementById("smsText");
		
		form.smsText.ChooseLen = function (){
			var maxLen=159;
			var mes = this.value.replace(/\r\n|\r|\n/g,'qq');
			var isRus=false;
			var i=0;
			while(ch = this.value.substr(i,1)){
				if (ch.charCodeAt(0)>128){//is rus
					isRus=true;
					break;
				}
				i++;
			}
			if (isRus){
				maxLen=(mes.length>69)?67:69;
			} else {
				maxLen=(mes.length>159)?154:159;
			}
			if (mes.length==0){
				smsLen.innerHTML = maxLen;
				smsLenA.innerHTML = 1;
			} else {
				smsLen.innerHTML = Math.ceil(mes.length/maxLen)*maxLen-mes.length;	
				smsLenA.innerHTML = Math.ceil(mes.length/maxLen);
			}		
		}
		
		smsText.ChooseLen();
		
		smsText.onblur=function(){
			Blur(this);
			smsText.ChooseLen();			
		}
		function Focus(obj){
			obj.style.overflow='auto';
		}
		function Blur(obj){
			obj.style.overflow='hidden';
		}
		smsText.onkeyup=form.smsText.ChooseLen;
		smsText.onkeydown=form.smsText.ChooseLen;
		smsLen.onfocus=function(){
			smsText.focus();
		}
	}
	
$.removeDelivery=function (){
                var id = $(this).attr('lang');
                var txt = 'Do you realy want to delete this delivery?<input type="hidden" id="deliveryid" name="deliveryid" value="'+ id +'" />';
                $.prompt(txt,{ 
                    buttons:{Delete:true, Cancel:false},
                    callback: function(v,m){
                        var did = m.find('#deliveryid').val();
                        if(v){                            
                            $.post('http://www.smsochki/ajax.php',{action:'deleteDelivery',deliveryid:did},function(data){
                                if(data == 'true'){
                                    $("#delivery"+did).remove();
                                    $("tbody tr").each(function(i) {
                                      $(this).find('td:first').text(1+i);
                                    });
                                    $("tr").removeClass();
                                    $("tr:odd").addClass('odd');
                                }
                            });
                        }
                        else{}
                    }
                });
                return false;
            }
$.oO=function(){
	if($("#short_num option:selected").attr('title')==1) $("#sourcenum").slideDown('slow');
	else $("#sourcenum").slideUp('slow');
}


$(document).ready(function(){
	$("#blabla").click($.Oo);
	$.Oo();
	$("#short_num").change($.oO);
	$.oO();
	$("#bscheck").click($.bsurl);
	$.bsurl();
    $("#bstime").click($.bstime);
	$.bstime();
	formInit();
	
		
    $("#but_slide").click(function () {
      $(".slider_text").slideToggle("slow");
    });

function summ()
{
	cb=$(".name_list option:selected").text();
	sms_count = parseInt($('#smsLenA').text());
	summ1 = $("#avsms").text();
	//pattern = /((.*?)\s+){1,}/i;
	arr = $.trim(summ1).split(" ");	
	reg = /.*?\(/i;
	res = cb.replace(reg, '');
	

	if(res != null)
	{
		regul = /(\d+)\s+/i;
		num = parseInt(res.match(regul)); 
		count_number =  parseInt(num); 
		total_sms = sms_count*count_number;
		ost = total_sms;
		var result = 0;
		
		$.each(arr, function(index, value) { 
  			symb = $.trim(value).split(":");
  			c_sms = symb[0];
  			price_sms = parseFloat("0."+symb[1]);
  			if(c_sms != 'def'){
	  			ost -= parseInt(c_sms);	
	  			if(ost > 0)	{result += (price_sms*parseInt(c_sms)); }
	  			else 
	  			{
	  				result = parseInt(total_sms)*price_sms;
	  				return false;
	  			}
	  		}
	  		else result +=parseInt(ost)*price_sms;
	  		  			
		});	
		formula = Math.round((result)*100)/100;
		$("#result").html('Итого: '+formula+' руб.');
	}
	else{ $("#result").empty();}
}

$(".name_list").change( function() {
	summ();	
});

$("#smsText").bind("keyup", function() {
	summ();	
});

/*$(".name_list").bind("change", function() {
	cb=$(".name_list option:selected").text();
	reg = /((\d+)\s+)/;
	res = reg.exec(cb);
	count_number = res[0][0];
	$("#result").html('Итого: '+count_number*0.3+' руб.');
});*/
	
//if(!$.browser.msie) $('.deleteDelivery').click($.removeDelivery);
});

