// JavaScript Document

var cur=-1, timer, timerMenu;

// --------------------------------------------------------------------------------------
// function work with menu

function menu_sel(num) {

//document.getElementById("m"+num).style.color='#A06D7D';
document.getElementById("m"+num).style.backgroundImage="url(../images/design/menu_hz_btn_bg_over.jpg)";

}

function menu_unsel(num) {
//document.getElementById("m"+num).style.color='#A06D7D';
document.getElementById("m"+num).style.backgroundImage="url(../images/design/menu_hz_btn_bg.jpg)";
}


function menu_up_sel(num) {

document.getElementById("mc_up"+num).style.backgroundColor ="#C4D9EA";
document.getElementById("ml_up"+num).innerHTML='<img src="../images/design/menu_up_l.gif" width="3" height="16" />';
document.getElementById("mr_up"+num).innerHTML='<img src="../images/design/menu_up_r.gif" width="3" height="16" />';
document.getElementById("ar"+num).innerHTML='<img src="../images/design/arr_r1.jpg"  />';
}

function menu_up_unsel(num) {
document.getElementById("mc_up"+num).style.backgroundColor ="#ffffff";
document.getElementById("ml_up"+num).innerHTML="";
document.getElementById("mr_up"+num).innerHTML="";
document.getElementById("ar"+num).innerHTML="";

}

function menu_left_sel(num) {

document.getElementById("mc_left"+num).style.backgroundColor ="#CFDFEF";
document.getElementById("mc_left"+num).style.color ="#E45A1C";
document.getElementById("ml_left"+num).innerHTML='<img src="../images/design/menu_left_l.gif" width="3" height="16"/>';
document.getElementById("mr_left"+num).innerHTML='<img src="../images/design/menu_left_r.gif" width="3" height="16"/>';
document.getElementById("ar_left"+num).innerHTML='<img src="../images/design/arr_leftm_r1.jpg" />';
}

function menu_left_unsel(num) {
document.getElementById("mc_left"+num).style.backgroundColor ="#fff";
document.getElementById("mc_left"+num).style.color ="#3382BD";
document.getElementById("ml_left"+num).innerHTML="";
document.getElementById("mr_left"+num).innerHTML="";
document.getElementById("ar_left"+num).innerHTML='<img src="../images/design/arr_leftm_r1_unact.jpg"/>';

}

/////// Флэшер
function flashDivId(id,n) { //закрывает все при открытии нового
	//alert(div.id);
	var clsdiv='s-'+id;
	sdo=document.getElementById(clsdiv);
	//alert(clsdiv);
	if(sdo.className=="divHide" ){
		sdo.className="divShow";
		
		
		for (i=0; i<n; i++){
			
	if (id!=i)  {	
var cd='s-'+i;	
hd=document.getElementById(cd);	
if (hd!=null)
hd.className="divHide";
	}
		}
		
	}
	else {
		sdo.className="divHide";
	}
	return sdo.className=="divShow"; 
}


function test() {
	alert()
}


function flashDiv2(id) { //Раскрывает и закрывает по желанию
	//alert(div.id);
	var clsdiv='s-'+id;
	sdo=document.getElementById(clsdiv);
	//alert(clsdiv);
	if(sdo.className=="divHide" ){
		sdo.className="divShow";
		
		
		
		
	}
	else {
		sdo.className="divHide";
	}
	return sdo.className=="divShow"; 
}

function unhide_div(id) { //Раскрывает и закрывает по желанию
	//alert(id);

	sdo=document.getElementById(id);
	//alert(clsdiv);
	if(sdo.className=="divHide" ){
		sdo.className="divShow";
		
	}

}

// ------------- 

function auth() {
	
	l=document.getElementById('login');
	p=document.getElementById('pass');
	
	xajax_auth(l.value,p.value);

}


function href_ofcreate(pid,refs) {

window.document.location.href="index.php?pid="+pid+refs;

}


// ------------------ФУНКЦИИ РАБОТЫ С INTRA_CELL--------------------------------------------------------------------
function ic_openwin(url, h, w){
	window.open(url,'_blank','top='+(window.screen.height-h)/2+',left='+(window.screen.width-w)/2+',height='+h+',width='+w+',status=no,toolbar=no,scrollbars=yes,titlebar=no,directories=no,resizable=no menubar=no,location=no');
	return;
}


function ic_openwin_instr(url, h, w){
	window.open(url,'_blank','top='+(window.screen.height-h)/2+',left='+(window.screen.width-w)/2+',height='+h+',width='+w+',status=no,toolbar=no,scrollbars=yes,titlebar=no,directories=no,resizable=no, menubar=yes,location=no');
	return;
}

// ------------------КОНЕЦ ФУНКЦИИ РАБОТЫ С INTRA_CELL--------------------------------------------------------------------


// ------------------ФУНКЦИИ РАБОТЫ С ИНВОЙСОМ--------------------------------------------------------------------
function calculate_invoice(){

var sum;
var total_sum;
sum=0;
total_sum=0;
n_items=save_chern.item_n.value;
//alert(n_items);
for (i=1; i<=n_items; i++){
//	alert(n_items);

	
qn=document.getElementById("inv_item_qn_"+i).value;
//alert(qn);
price=document.getElementById("inv_item_price_"+i).value;
//alert(price);
//alert("inv_item_price_"+i);
sum=qn*price;


document.getElementById("item_sum_"+i).innerHTML=sum;
//alert('d');
total_sum=total_sum+sum;
}
//alert(total_sum);
nds=total_sum*12/112
nds=nds.toFixed(2)


document.getElementById("inv_itog").innerHTML=total_sum;
document.getElementById("include_nds").innerHTML=nds;

document.getElementById("vsego_naim").innerHTML=total_sum;


var w1 = NumberToWords(total_sum,['тенге', 'тенге', 'тенге'],2);

document.getElementById("sum_propis").innerHTML=w1;



}





// ------------------КОНЕЦ ФУНКЦИИ РАБОТЫ С ИНВОЙСОМ--------------------------------------------------------------------

// ------------------ПЕРЕВОД ЧИСЕЛ В СЛОВА--------------------------------------------------------------------
//Источник: http://xpoint.ru/forums/programming/javascript/misc/thread/39532.xhtml
var NumberToWords = (function () {
    var words = [['ноль', ['один', 'одна', 'одно'],
            ['два', 'две', 'два'], 'три', 'четыре', 'пять',
                'шесть', 'семь', 'восемь', 'девять', 'десять',
                'одиннадцать', 'двенадцать', 'тринадцать',
                'четырнадцать', 'пятнадцать', 'шестнадцать',
                'семнадцать', 'восемнадцать', 'девятнадцать'],
            [,,'двадцать', 'тридцать', 'сорок', 'пятьдесят',
                'шестьдесят', 'семьдесят', 'восемьдесят',
                'девяносто'],
            [,'сто', 'двести', 'триста', 'четыреста', 'пятьсот',
                'шестьсот', 'семьсот', 'восемьсот', 'девятьсот'],
            ['тысяча', 'тысячи', 'тысяч'],
            ['миллион', 'миллиона', 'миллионов']],
        gap = String.fromCharCode(32),
        overdo = 'слишком много';
    function Convert(aNum, aCase, aBool) {
        var p, a;
        aNum = parseInt(aNum, 10);
        if (aNum < 20) {
            return ((aBool && !aNum)
                    ? gap
                    : ((a = words[0][aNum]) instanceof Array)
                       ? a[aCase || 0]
                       : a);
        }
        if (aNum < 100) {
            p = parseInt(aNum / 10, 10);
            return Join(words[1][p],
                        Convert(aNum % 10, aCase, true));
        }
        if (aNum < 1000) {
            p = parseInt(aNum / 100, 10);
            return Join(words[2][p],
                        Convert(aNum % 100, aCase, true));
        }
        if (aNum < 1000000) {
            p = parseInt(aNum / 1000, 10);
            return Join(Convert(p, 1, true),
                        Proper(p, words[3]),
                        Convert(aNum % 1000, aCase, true));
        }
        p = parseInt(aNum / 1000000, 10);
        return Join(Convert(p, 0, true),
                    Proper(p, words[4]),
                    Convert(aNum % 1000000, aCase, true));
    
    };
    function Proper(aNum, aArr) {
        aNum = Simple(aNum);
        return ((aNum == 1) 
                ? aArr[0]
                : ((aNum < 5 && aNum)
                   ? aArr[1]
                   : aArr[2]));
    };
    function Simple(aNum) {
        return ((aNum < 20)
                ? aNum
                : (aNum < 100)
                  ? aNum % 10
                  : arguments.callee(aNum % 100));
    }
    function Join() {
        return Array.prototype.join.call(arguments, gap);
    };
    return (function (aNum, aArr, aCase) {
        var b = (aNum > 999999999),
            w = (b) ? overdo
                    : Convert(aNum, aCase),
            i = (aArr instanceof Array)
                ? Proper(b ? 5 : aNum, aArr)
                : gap;
        return Join(w, i).replace(/\s\s\s?/g, gap);
    });
})();

//Проверка
//onload = function () {
 //   var w1 = NumberToWords(201503012,
//                          ['коромысло', 'коромысла', 'коромысел'],
//                          2),
 //       w2 = NumberToWords(95921110,
//                          ['кочерга', 'кочерги', 'кочерг'], 1),
//        w3 = NumberToWords(1000000, ['стул', 'стула', 'стульев'], 0),
//        w4 = NumberToWords(1000000000);
//    alert('1: ' + w1 + '\n' + '2: ' +  w2 + '\n' +
//          '3: ' + w3 + '\n' + '4: ' + w4);
//}
//Конец Проверка


// ------------------КОНЕЦ ПЕРЕВОД ЧИСЕЛ В СЛОВА--------------------------------------------------------------------

function check_inv_exist(){
	
inv_n=document.getElementById("inv_n").value;

xajax_check_inv_exist(inv_n)
//alert(document.getElementById("inv_n").value)

}

function set_sum_propis(sum) {
//alert(sum);
var w1 = NumberToWords(sum,['тенге', 'тенге', 'тенге'],2);

document.getElementById("sum_propis").innerHTML=w1;
	
}



//----------------------------------------------------------------

function showDiv(id) { //Раскрывает и закрывает по желанию
	//alert(div.id);
	var clsdiv=id;
	sdo=document.getElementById(clsdiv);
	//alert(clsdiv);
	if(sdo.className=="divHide" ){
		sdo.className="divShow";
		
		
		
		
	}
	else {
		sdo.className="divHide";
	}
	return sdo.className=="divShow"; 
}

function hiseDiv(id) { //Раскрывает и закрывает по желанию
	//alert(id);

	sdo=document.getElementById(id);
	//alert(clsdiv);
	if(sdo.className=="divHide" ){
		sdo.className="divShow";
		
	}

}
