
function hideshow(which)
{
if (!document.getElementById|document.all){
        return
}else{
        if (document.getElementById)
                oWhich = eval ("document.getElementById('" + which + "')")
        else
                oWhich = eval ("document.all." + which)
        }

        window.focus()

        if (oWhich.style.display=="none")
                oWhich.style.display=""
        else
                oWhich.style.display="none"
}


function hideshowPlus(which, imag)
{
if (!document.getElementById|document.all){
        return
}else{

        if (document.getElementById){
                oWhich = eval ("document.getElementById('" + which + "')");
                oImag = eval ("document.getElementById('" + imag + "')");
        }else{
                oWhich = eval ("document.all." + which);
                oImag = eval ("document.all." + imag);
        }
        }

        window.focus();

        if (oWhich.style.display=="none"){
            oImag.src="img/minus.gif";
                oWhich.style.display=""
        }else{
                oWhich.style.display="none"
                oImag.src="img/plus.gif";
        }
}

function isEmpty(idElem,errorMsg){
  if (document.getElementById(idElem).value==''){
		if(errorMsg!=undefined){
      showError(errorMsg);
    }
    return true;
	}else{
		return false;
  }
}
function isEmail(idInp,errorMsg){
	if (document.getElementById(idInp).value.match('^[a-zA-Z0-9\._-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]{2,4}$')!=null)
		return true;
	else{
    if(errorMsg!=undefined){
      showError(errorMsg);
    }
		return false;
  }
}

function showError(error){
  $('#errorContent').append(error+'<br/>');
  $a('errs').style.display='block';
}

function auto_fill(form,field,text){
	eval('document.'+form+'.'+field+'.value="'+text+'"');
}

function delconfirm(get){
 
  if( confirm("Уверены, что хотите удалить?")){
    window.document.location.href=get;
  }
}

function set_date(day){
  action.date_open.value=day
  }

function open_calendar(href){
  popup = window.open(href,'calendar', 'location=0, toolbar=0, menubar=0, resizable=0, scrollbars=0,width=195,height=180,left=150, top=200')
 }


 function $a(id){
 	return document.getElementById(id);
 }
 
 function delBlock(name_form,id) {
	$a(name_form).removeChild($a('file_'+id));
	hidId=parseInt($a(idParent+'_hid').value);
	hidId-=1;
	$a(idParent+'_hid').value=hidId;
}


function addBlock(idParent,curId,block) {
	
	 var newup =document.createElement('div');
	 var str;
	 var i;
	 var hidId;
	 hidId=parseInt($a(idParent+'_hid').value);
	 
	 if (curId+1<=hidId){
	 	return;
	 }
	hidId += 1;
	$a(idParent+'_hid').value=hidId;
	id = hidId;
	newup.id = 'file_'+id;
	str='';
	for (i=0;i<arBlocks.length;i++){
		str+='<div style="width: 200px; float:left;padding-left:10px;">'+arTits[i]+'</div><div style="width: 150px; float:left;padding-left:5px;"><input type="text" name="'+arBlocks[i]+'[]" onkeydown="addBlock(\''+idParent+'\','+id+');"></div><br style="clear:both">'
	}
	str+='<div  style="width: 15px;position:relative; top:-15px;right:0px;float:right; height:15px;"><span style="cursor:pointer" onclick="delBlock(\''+idParent+'\','+id+')"><img src="img/del.gif"></span></div><br>';
	newup.innerHTML = str;
	$a(idParent).appendChild(newup);
}
 
function displayBySel(obj,hideVal,idDisp){

 if (obj.value==hideVal){
    $a(idDisp).style.display="none";        
  }else{
    $a(idDisp).style.display="block"
  }
  
}

function changeCourse(){
  var idFrom=$a('id_purse_from').value;
  var idTo=$a('id_purse_to').value;
  if(window.courses[idFrom][idTo]==0){
    $a('course').innerHTML='<span style="color:#af3333;">Недопустимая операция</span>';
  }else{
    $a('course').innerHTML=window.courses[idFrom][idTo];
  }

  if(window.min_sum[idFrom][idTo]==0){
    $a('min_sum').innerHTML='<span style="color:#af3333;">Недопустимая операция</span>';
  }else{
    $a('min_sum').innerHTML=window.min_sum[idFrom][idTo]+' '+window.purses[idFrom];
  }

    $a('course_hid').value=window.courses[idFrom][idTo];    
    $a('amount').innerHTML=window.amounts[idTo]+' '+window.purses[idTo];;
    $a('purse').innerHTML=window.purses[idTo];
    changeCount('from_count','to_count',false);
}

function changeCount(fromIdent,toIdent,inverse){

  var idFrom=$a('id_purse_from').value;
  var count, recount, reference ,count2;
  if(inverse){
    count=$a(fromIdent).value/$a('course_hid').value;
    //reference=1*count+1*referenceWm(count);
    //reference= reference.toFixed(2);

   // $a(fromIdent).value=parseInt(count*$a('course_hid').value*100)/100;
    count=count.toFixed(2);
    $a(toIdent).value=count;
   // $a(toIdent+'_wm').value=reference;
    //$a(toIdent+'_purse').innerHTML=' '+ window.purses[idFrom];
    changeCount(toIdent,fromIdent,false);
  }else{
    count=$a(fromIdent).value*$a('course_hid').value;
    count=parseInt(count*100)/100;
    reference=1*$a(fromIdent).value+1*referenceWm($a(fromIdent).value);
    reference= reference.toFixed(2);
    count=count.toFixed(2);
    $a(fromIdent+'_wm').value=reference;
    $a(fromIdent+'_purse').innerHTML=' '+ window.purses[idFrom];
    $a(toIdent).value= count;
    

  }

}

function changeCountAll(fromIdent,toIdent,inverse){
  var idFrom=$a('id_purse_from').value;
  var count,amount;
    amount=1*refunWm($a(fromIdent+'_wm').value);
    count=amount*$a('course_hid').value;
    count=count.toFixed(2);
    amount=amount.toFixed(2);

  //  reference=1*amount+1*referenceWm(amount);
  //  reference= reference.toFixed(2);
  //  $a(fromIdent+'_wm').value=reference;
    $a(fromIdent).value=amount;
    $a(toIdent).value= count;
 
}
function cangeAmount(fromIdent,toIdent,inverse){
      var idTo=$a('id_purse_to').value;
      $a(fromIdent).value= window.amounts[idTo];
      changeCount(fromIdent,toIdent,inverse)
}

function referenceWm (amount){
        var reference;
	  if(amount>0){
		  reference=amount*0.008;
		  if(reference<0.01){
			  reference=0.01;
		  }
		  reference=Math.round(reference*100)/100;
		  return reference;
	  }
	  return amount;
}

function refunWm (amount){
          var reference;
	  if(amount>0.02){

		  reference=1*amount/(100+0.8)*100;

		  if((reference*0.08)<0.01){
			  reference=1*amount-0.01;
		  }
		  reference=Math.round(reference*100)/100;
		  return reference;
	  }
	  return 0;
}

