﻿function replace_string(txt,cut_str,paste_str){ 
    var f=0;
    var ht='';
    ht = ht + txt;
    f=ht.indexOf(cut_str);
    while (f!=-1){f=ht.indexOf(cut_str);if (f>0){ht = ht.substr(0,f) + paste_str + ht.substr(f+cut_str.length);};};
    return ht
};

function Recalculate(addPrice,cnt,mainID,mainPrice,addID){
    var doc=window.document;
    var checkBut;
    var total; 
    var str_additions;
    var str_additions_new;     
    checkBut=document.getElementsByName(mainID)[cnt].checked;
	total = Number(document.getElementById("total").value);
	str_additions=document.getElementById("isCheck").value;
	str_additions_new=str_additions;
	if (checkBut==true){
           total=total + Number(addPrice);
           str_additions=str_additions+mainID+'_'+addID+'_';	
           document.getElementById("total").value = total;
           document.getElementById("total_all").value=total+Number(mainPrice);
           document.getElementById("isCheck").value = str_additions;           
        }   
    else {
           total=total - Number(addPrice);
           str_additions_new=replace_string(str_additions,mainID+'_'+addID+'_','');
           document.getElementById("total").value = total;
           document.getElementById("total_all").value=total+Number(mainPrice);
           document.getElementById("isCheck").value = str_additions_new;            
         }
}

function RecalculateRadio(addPrice,mainID,radioOldID,mainPrice,addID){
    var doc=window.document;
    var totalRadio=0;
    var total=0; 
    var str_additions;
    var str_additions_new;
    str_additions=document.getElementById("isCheck").value;
	str_additions_new=str_additions;
	totalRadio = Number(document.getElementById(mainID).value);
	radioOld = document.getElementById(radioOldID).value;
	total = Number(document.getElementById("total").value)-totalRadio+Number(addPrice);
	if (radioOld=='')  {
	    str_additions_new=str_additions_new+mainID+'_'+addID+'_';
	}
	else {
	    str_additions_new=replace_string(str_additions,radioOld,'');
	    str_additions_new=str_additions_new+mainID+'_'+addID+'_';
	}
	document.getElementById("total").value = total;
	document.getElementById("total_all").value=total+Number(mainPrice); 	
    document.getElementById(mainID).value = Number(addPrice); 
    document.getElementById(radioOldID).value = mainID+'_'+addID+'_';
    document.getElementById("isCheck").value = str_additions_new; 
}

function RadioClear(mainID,radioOldID,mainPrice){
    var doc=window.document;
    var totalRadio=0;
    var total=0; 
    var str_additions;
    var str_additions_new;
    str_additions=document.getElementById("isCheck").value;
	str_additions_new=str_additions;
	totalRadio = Number(document.getElementById(mainID).value);
	radioOld = document.getElementById(radioOldID).value;
	total = Number(document.getElementById("total").value)-totalRadio;
	str_additions_new=replace_string(str_additions,radioOld,'');
	document.getElementById("total").value = total;
	document.getElementById("total_all").value=total+Number(mainPrice); 	
    document.getElementById(mainID).value = 0; 
    document.getElementById(radioOldID).value = '';
    document.getElementById("isCheck").value = str_additions_new; 
}

function RecalculateTying(cnt,mainID,idTying){
    var doc=window.document;
    var checkBut;
    var str_addTying;
    var str_addTying_new;
    if (document.getElementsByName(mainID)[cnt]){
        checkBut=document.getElementsByName(mainID)[cnt].checked;
    }
    else {
        checkBut=document.getElementById(mainID).checked;
    }
    str_addTying=document.theForm.isCheckTying.value;
	str_addTying_new=str_addTying;	 
    if (checkBut==true){
           str_addTying=str_addTying+'_'+idTying+'_';	
           document.theForm.isCheckTying.value = str_addTying;           
        }   
    else {
           str_addTying_new=replace_string(str_addTying,'_'+idTying+'_','');
           document.theForm.isCheckTying.value = str_addTying_new;            
         }
}
