function show_help(service, lng, ctx) {
    if( isNaN(service) ) {
	help1.ShowInner(service);
    } else {
	help1.Show('/help.php?service='+service+'&lng='+lng+'&hlp_ctx='+ctx);
    }
}

function hide_help() {
    if (BROWSER != IE) {
	set_dhtml_frame_url('help', 'help.php');
    }
    hide_dhtml_popup('help');
}

function hidehelp() {
    help1.Hide();
}

function mx_check_ctg_boxes() {
    if (document.f1.cbCheckAll.checked == true){
	for(i = 0; i < document.f1.elements.length; i ++) {
	    if(document.f1[i].type == 'checkbox' && 
	       !document.f1[i].checked &&
	       document.f1[i].name != 'cbCheckAll' &&
	       document.f1[i].name == 'aCtg[]') {

		document.f1[i].checked = true;
	    }
	}
    } else {
	for(i = 0; i < document.f1.elements.length; i ++) {
	    if(document.f1[i].type == 'checkbox' &&
	       document.f1[i].checked &&
	       document.f1[i].name != 'cbCheckAll' &&
	       document.f1[i].name == 'aCtg[]') {

		document.f1[i].checked = false;
	    }
	}
    }
}

function check_all_boxes() {
    if (document.f1.check_all.checked == true) {
	for(i = 0; i < document.f1.elements.length; i ++) {
	    if (document.f1[i].type == 'checkbox' && 
		!document.f1[i].checked && 
		document.f1[i].name != 'check_all' && 
		document.f1[i].name != 'val[]')  {

		document.f1[i].checked = true;
	    }
        }
    } else {
	for (i = 0; i < document.f1.elements.length; i ++) {
	    if (document.f1[i].type == 'checkbox' &&
		document.f1[i].checked && 
		document.f1[i].name != 'check_all' && 
		document.f1[i].name != 'val[]') {
		
		document.f1[i].checked = false;
	    }
        }
    }
}

function check_all_val_boxes() {
    if ( document.f1.check_all.checked==true ) {
	for (i = 0; i < document.f1.elements.length; i ++) {
	    if (document.f1[i].type == 'checkbox' && 
		!document.f1[i].checked && 
		document.f1[i].name != 'check_all' && 
		document.f1[i].name == 'val[]') {

		document.f1[i].checked = true;
	    }
        }
   } else {
	for(i = 0; i < document.f1.elements.length; i++) {
	    if (document.f1[i].type == 'checkbox' && 
		document.f1[i].checked && 
		document.f1[i].name != 'check_all' && 
		document.f1[i].name == 'val[]')  {

		document.f1[i].checked = false;
	    }
        }
    }
}

function HideShowAnF4(year) {
    var dc = window.top.document;
    var P1_ESN = dc.getElementById('divP1_ESN');
    var P1_OSV = dc.getElementById('divP1_OSV');
    var P2_ESN = dc.getElementById('divP2_ESN');
    var P2_OSV = dc.getElementById('divP2_OSV');
    var Health = dc.getElementById('divHealth');
    var Title = dc.getElementById('divTitle');
    var P1_OSV2011 = dc.getElementById('divP1_OSV2011');
    var P2_AI = dc.getElementById('divP2_AI');
  
    P1_ESN.style.display = 'none';
    P1_OSV.style.display = 'none';
    P2_ESN.style.display = 'none';
    P2_OSV.style.display = 'none';
    Health.style.display = 'none';
    Title.style.display = 'none';
    P1_OSV2011.style.display = 'none';
    P2_AI.style.display = 'none';

    // задизейблим те "галки", которые в данный момент не видны, чтобы они не передавались на сервер
    for (i = 0; i < document.f1.elements.length; i ++) {
	if (document.f1[i].type == 'checkbox') {
	    var id_name = document.f1[i].id.split('_');
	    if ((((year < 2010) && (Number(id_name[1]) > 2009)) ||
		 ((year > 2009) && (Number(id_name[1]) < 2010))) &&
		(Number(id_name[1]) != 0)) {
		document.f1[i].disabled = true;
	    } else {
		document.f1[i].disabled = false;
	    }
	}
    }
  
    if (year < 2010) {
	P1_ESN.style.display = 'block';
	P2_ESN.style.display = 'block';
	Health.style.display = 'block';
	P1_OSV.style.display = 'none';
	P2_OSV.style.display = 'none';
	Title.style.display = 'none';
	P1_OSV2011.style.display = 'none';
	P2_AI.style.display = 'none';
    } else if (year == 2010) {
	P1_ESN.style.display = 'none';
	P2_ESN.style.display = 'none';
	Health.style.display = 'none';
	P1_OSV.style.display = 'block';
	P2_OSV.style.display = 'block';
	Title.style.display = 'none';
	P1_OSV2011.style.display = 'none';
	P2_AI.style.display = 'none';
    } else {
	P1_ESN.style.display = 'none';
	P2_ESN.style.display = 'none';
	Health.style.display = 'none';
	P1_OSV.style.display = 'none';
	P2_OSV.style.display = 'none';
	Title.style.display = 'block';
	P1_OSV2011.style.display = 'block';
	P2_AI.style.display = 'block';
    }
}

function enable_disable_how_accept() {
    var dc = window.top.document;
    dc.f1.how_accept[0].disabled = !dc.f1.taccept.checked;
    dc.f1.how_accept[1].disabled = !dc.f1.taccept.checked;
}

function enable_disable_how_accept2011() {
    var dc = window.top.document;
    dc.f1.eds[0].disabled = !dc.f1.taccept.checked;
    dc.f1.eds[1].disabled = !dc.f1.taccept.checked;
}

function check_regnum_fill() {
    if (BROWSER == IE) {
	mes = document.all.tags('INPUT');
    } else if (BROWSER == MZ) {
	mes = document.getElementsByTagName('INPUT');
    }
    var len = mes.length;
    for (i = 0; i < len; i ++) {
	if (mes[i].id == 'strah') {
	    if (document.f1.new_regnum.value != '') {
		//mes[i].checked = true;
	    } else {
		mes[i].checked = false;
		alert('Необходимо заполнить Регистрационный номер страхователя! ');
	    }
	}
    }
}

function checkid() {
    if (BROWSER == IE) {
	mes = document.all.tags('TABLE');
    } else if (BROWSER == MZ) {
	mes = document.getElementsByTagName('TABLE');
    }

    for (i = 0; i < mes.length; i ++) {
	if (mes[i].id == 'excel') {
	    return(true);
	}
    }
    return(false);
}

function loadcard(sid) {
    if (document.all) {
        topoffset=document.body.scrollTop;
        leftoffset=document.body.scrollLeft;
    } else {
        topoffset=pageYOffset;
        leftoffset=pageXOffset;
    }

    newtop = ((getClientHeight() - 400) / 2) + topoffset;
    newleft = getClientWidth() - 600;

    move_div('helpdiv', newtop, newleft);
    doc = window.top.document;
    n = doc.getElementById('new_regnum').value;
    show_dhtml_popup('help', 'index.php?reg_num='+n+'&service=83&section=g&to=20&PHPSESSID='+sid+'&rmode=0&cardonly=1');
}

function loadcardins(sid, regnum) {
    if (document.all) {
        topoffset=document.body.scrollTop;
        leftoffset=document.body.scrollLeft;
    } else {
        topoffset=pageYOffset;
        leftoffset=pageXOffset;
    }

    newtop=((getClientHeight()-400)/2)+topoffset;
    newleft=getClientWidth()-600;
    
    move_div('helpdiv', newtop, newleft);
    show_dhtml_popup('help', 'index.php?reg_num='+regnum+'&service=83&section=g&to=20&PHPSESSID='+sid+'&rmode=0&cardonly=1');
}

function loadcrtf1( sid, spt ) {
    obj=getobj('hiddiv');
    obj.visibility=HID;
    doc = window.top.document;
    s = doc.getElementById('SSRTF').value;
    n = doc.getElementById('NSRTF').value;

    if( spt == 0 ) spt = 1;
    if(BROWSER==IE && BROWSER2!=OP){
	top.frames['hidframe'].document.URL = 'index.php?service=55&crtf1get='+spt+'&PHPSESSID='+sid+'&tser='+s+'&tno='+n;
    } else {
	frm=document.getElementById('hidframe');
	frm.src = 'index.php?service=55&crtf1get='+spt+'&PHPSESSID='+sid+'&tser='+s+'&tno='+n;
    }
}

function getcrtf1vals( l1,l2,l3,l4,l5,p1,p2,p3,p4,p5,p6,p7,d1,d2,d3,m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,s1,s2,s3 ) {

    doc = window.top.document;
    form = doc.forms['form2'];
    if( form.elements['TDOC'] ) {       form.elements['TDOC'].value = p1;   }
    if( form.elements['DDOC'] ) {       form.elements['DDOC'].value = p2;   }

    if( form.elements['SDOC'] ) {       form.elements['SDOC'].value = p5;   }
    if( form.elements['NDOC'] ) {       form.elements['NDOC'].value = p6;   }
    if( form.elements['ODOC'] ) {       form.elements['ODOC'].value = p7;   }

    if( form.elements['DSRTF'] ) {       form.elements['DSRTF'].value = d1;   }

    if( form.elements['SLIN'] ) {       form.elements['SLIN'].value = l1;   }
    if( form.elements['NLIN'] ) {       form.elements['NLIN'].value = l2;   }

    if( form.elements['DLIN'] ) {       form.elements['DLIN'].value = l3;   }
    if( form.elements['DCCR'] ) {       form.elements['DCCR'].value = m1;   }

    if( form.elements['NCCR'] ) {   form.elements['NCCR'].value = m4;   }
    if( form.elements['SNILS'] ) {       form.elements['SNILS'].value = m5;   }
    if( form.elements['SIPLC'] ) {       form.elements['SIPLC'].value = m6;   }
    if( form.elements['NIPLC'] ) {       form.elements['NIPLC'].value = m7;   }
    if( form.elements['ADRES'] ) {       form.elements['ADRES'].value = m8;   }

    if( form.elements['BDATE'] ) {       form.elements['BDATE'].value = m9;   }

    if( form.elements['LNAME'] ) {       form.elements['LNAME'].value = s1;   }
    if( form.elements['FNAME'] ) {       form.elements['FNAME'].value = s2;   }
    if( form.elements['MNAME'] ) {       form.elements['MNAME'].value = s3;   }

    return(true);
}

function getcrtf3vals( l1,l2,l3,l4,l5,p1,p2,p3,p4,p5,p6,p7,d1,d2,d3,m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,s1,s2,s3, cs ) {

    doc = window.top.document;
    form = doc.forms['form2'];
    if( form.elements['TDOC'] ) {       form.elements['TDOC'].value = p1;   }
    if( form.elements['DDOC'] ) {       form.elements['DDOC'].value = p2;   }
    if( form.elements['SDOC'] ) {       form.elements['SDOC'].value = p5;   }
    if( form.elements['NDOC'] ) {       form.elements['NDOC'].value = p6;   }
    if( form.elements['ODOC'] ) {       form.elements['ODOC'].value = p7;   }

    if( form.elements['DSRTF'] ) {       form.elements['DSRTF'].value = d1;   }

    if( form.elements['SLIN'] ) {       form.elements['SLIN'].value = l1;   }
    if( form.elements['NLIN'] ) {       form.elements['NLIN'].value = l2;   }

    if( form.elements['DLIN'] ) {       form.elements['DLIN'].value = l3;   }
    if( form.elements['DCCR'] ) {       form.elements['DCCR'].value = m1;   }
    if( form.elements['NCCR'] ) {   form.elements['NCCR'].value = m4;   }
    if( form.elements['SNILS'] ) {       form.elements['SNILS'].value = m5;   }
    if( form.elements['SIPLC'] ) {       form.elements['SIPLC'].value = m6;   }
    if( form.elements['NIPLC'] ) {       form.elements['NIPLC'].value = m7;   }
    if( form.elements['ADRES'] ) {       form.elements['ADRES'].value = m8;   }

    if( form.elements['BDATE'] ) {       form.elements['BDATE'].value = m9;   }
    if( form.elements['LNAME'] ) {       form.elements['LNAME'].value = s1;   }
    if( form.elements['FNAME'] ) {       form.elements['FNAME'].value = s2;   }
    if( form.elements['MNAME'] ) {       form.elements['MNAME'].value = s3;   }

    var n = " ";
    var i = 0;
    var cs2l = 0;
    fields = new Array("bSEX[","bBDATE[","bFNAME[","bLNAME[","bMNAME[","bSIPLC[","bNIPLC[","bBDPOST[","bBSLOOK[" );
    while ( cs.indexOf(';',0) != -1 ) {
	cs1 = cs.substring(0,cs.indexOf(';',0));

	cs2 = cs1.substring(0,cs1.indexOf('|',0));
	cs2l = 0;
	k = 0;
	while(k != fields.length) {
	    cs2l += cs2.length+1;
	    n = fields[k]+i+"]";
	    if( form.elements[n] ) {
		el= form.elements[n];
		el.value = cs2;
	    }
	    cs2 = cs1.substring(cs2l,cs1.indexOf('|',cs2l));
	    k++;
        }
	i++;
	cs = cs.substring(cs.indexOf(';',0)+1);
    }
    return(true);
}

function win2unicode(str) {
    var charmap   = unescape(
        "%u0402%u0403%u201A%u0453%u201E%u2026%u2020%u2021%u20AC%u2030%u0409%u2039%u040A%u040C%u040B%u040F"+
        "%u0452%u2018%u2019%u201C%u201D%u2022%u2013%u2014%u0000%u2122%u0459%u203A%u045A%u045C%u045B%u045F"+
        "%u00A0%u040E%u045E%u0408%u00A4%u0490%u00A6%u00A7%u0401%u00A9%u0404%u00AB%u00AC%u00AD%u00AE%u0407"+
        "%u00B0%u00B1%u0406%u0456%u0491%u00B5%u00B6%u00B7%u0451%u2116%u0454%u00BB%u0458%u0405%u0455%u0457");
    var code2char = function(code) {
	if(code >= 0xC0 && code <= 0xFF)
	    return String.fromCharCode(code - 0xC0 + 0x0410);
	if(code >= 0x80 && code <= 0xBF) 
	    return charmap.charAt(code - 0x80);
	return String.fromCharCode(code)
    }
    var res = "";
    for(var i = 0; i < str.length; i ++)
	res = res + code2char(str.charCodeAt(i));
    return res
}

function to_clipboard(tbl, calc) {
    var clipboard='<span style="height:200px;"><br></span><table border=0><tr><td></td></tr></table><table border="1">\r\n';
    var tmptrs=tbl.rows;
    for(i = 0; i < tmptrs.length; i ++) {
	if (tmptrs[i].className != 'noid') {
	    clipboard += '<tr>\r\n';
	    var tmptr = tmptrs[i].cells;
	    for(j = 0; j < tmptr.length; j ++) {
		var tmptd=tmptr[j];
		clipboard += '<td';
		if (tmptd.colSpan > 1) clipboard+=' colspan="'+tmptd.colSpan+'"';
		if (tmptd.rowSpan > 1) clipboard+=' rowspan="'+tmptd.rowSpan+'"';
		var text=tmptd.innerText;
		if (text.match(/^[\d\s]+\.\d+$/)) text=text.replace('.',',');
		if (calc == 1) text = win2unicode(text);
		clipboard += ' align="center" valign="middle">'+(text == '&nbsp;' ? '' : text) + '</td>\r\n';
	    }
	    clipboard += '</tr>\r\n';
	}
    }

    clipboard += '</table>\r\n';

    window.clipboardData.setData("Text", clipboard);

    if (calc == 1) {
	alert("Данные скопированы в буфер обмена Windows. Для вставки в Calc используйте опцию Вставить (Paste).");
    } else {
	alert("Данные скопированы в буфер обмена Windows. Для вставки в Excel используйте опцию Вставить (Paste).");
    }
}


function TableToClipbord(calc) {
    var e;
    e = document.getElementById('excel');
    if (e != null) {
  	to_clipboard(e, calc);
    }
}

function switch_disabled( formid, elementid, enabled) {
    var d, f, e;
    f = document.forms[formid];
    e = f.elements[elementid];
    e.disabled = !enabled;
}
