﻿function ReplaceAll(iStr, v1, v2)
{
	var i = 0, oStr = '', j = v1.length;

	while (i < iStr.length)
	{
		if (iStr.substr(i, j) == v1)
		{
			oStr+=v2;
			i+=j
		}
		else
		{
			oStr+=iStr.charAt(i);
			i++;
		}
	}

	return oStr;
}
function CheckThisVote(field)
{
	form = field.form;
	if (field.checked)
	{
		form.fvotefor.value = field.value;
	}
	else
	{
		form.fvotefor.value = '';
		return;
	}

	for (i=0; i < form.elements.length - 2; i++)
	{
		if(form.elements[i].type=='checkbox')
			if (form.elements[i] != field)
				if (form.elements[i].checked)
					form.elements[i].checked = false;
	}
}

function SubmitVote(sform, saction)
{
	if (saction==0)
	{
		if (sform.fvotefor.value=='')
		{
			alert('Hay chon mot trong cac muc truoc khi bieu quyet');
			return;
		}
	}

	var form = sform;
	var j = 0
	for (i=0; i < form.elements.length - 2; i++)
		{
			if(form.elements[i].type=='checkbox'){
				j = j + 1
			}
		}
	var sheight = (j * 40) + 80;
	if (sheight < 250){
		sheight = 250;
	}
	open('', sform.name, 'scrollbars=yes,resizeable=no,locationbar=no,width=550,height='+sheight+',left='.concat((screen.width - 500)/2).concat(',top=').concat((screen.height - 250)/2));
	sform.faction.value = saction;
	sform.action = 'http://www.hoahoctro.vn/VoteManager/VoteResult.aspx';
	sform.submit();
}

function AddVote(SubjectID, PageID, VoteID, Align, VoteTitle, Color, BgColor, Width, NumItem, ItemArray, Description, Column, VoteImage, ContactLink)
{
	document.writeln('<table width="', Width, '" border=0 cellspacing=0 cellpadding=1 bgcolor="#B3B3B3" ', (Align=='') ? '' : ' align='.concat(Align), '>');
	if (VoteTitle!='')
	{
		document.writeln('<tr><td><Table bgcolor=',BgColor,' cellspacing=0 cellpadding=3 width="100%"><tr><td><p class=BoxTitle style="margin-left: 3; color: ', Color, '">', VoteTitle, '</p></td></tr></Table></td></tr>');
	}

	if (typeof(Description)=='undefined')
	{
		Description = '';
	}
		if (VoteImage!='')
	{
		document.writeln('<tr><td><Table bgcolor=',BgColor,' cellspacing=0 cellpadding=1 width="100%"><tr><td><img src="', VoteImage, '" width="178" border=0></td></tr></Table></td></tr>');
	}
	if (Description!='')
	{
		document.writeln('<tr><td><Table bgcolor=',BgColor,' cellspacing=0 cellpadding=3 width="100%"><tr><td><p class=BoxTitle style="margin-left: 3; color: ', Color, '">', Description, '</p></td></tr></Table></td></tr>');
	}

	if (typeof(Column)=='undefined')
	{
		Column = 1;
	}

	document.writeln('<tr>');
	document.writeln('<form method="POST" target="Frm_', VoteID, '" name="Frm_', VoteID, '">');
	document.writeln('<td>');
	document.writeln('<table border=0 cellpadding=0 cellspacing=0 width="100%" bgcolor="#E6DECD">');

	document.writeln('<input type="hidden" name="fsubjectid" value=', SubjectID, '>');
	document.writeln('<input type="hidden" name="fpageid" value=', PageID, '>');
	document.writeln('<input type="hidden" name="fvoteid" value=', VoteID, '>');
	document.writeln('<input type="hidden" name="fvotetitle" value="', ReplaceAll(VoteTitle, '"', '&quot;'), '">');
	document.writeln('<input type="hidden" name="fvotefor" value="">');
	document.writeln('<input type="hidden" name="faction" value="0">');
	document.writeln('<input type="hidden" name="fDescription" value="', ReplaceAll(Description, '"', '&quot;'), '">');

	document.writeln('<input type="hidden" name="fnumitem" value=', NumItem, '>');
	document.writeln('<tr><td width="100%" height=5></td></tr>');
    
	document.writeln('<tr><td>');
	document.writeln('<table width="100%" cellspacing=0 cellpadding=0 border=0>');

	var i, j, k;

	for (i=0; i < NumItem; )
	{
		document.writeln('<tr>');
		
		for (j=0; j < Column && i < NumItem; j++, i++)
		{
			document.writeln('<input type="hidden" name="fT_', i, '" value="', ReplaceAll(ItemArray[i][0], '"', '&quot;'), '">');
			document.writeln('<input type="hidden" name="fI_', i, '" value="', ItemArray[i][1], '">');
			document.writeln('<input type="hidden" name="fN_', i, '" value="', ItemArray[i][2], '">');
			document.writeln('<td valign=top width=20 align=right><input type="checkbox" name="fC_', i, '" value=', ItemArray[i][2], ' class=VoteField onClick="CheckThisVote(this)"></td>');
			if (i + 1 < NumItem || Column==1)
			{
				document.writeln('<td><p  class=VoteItem>', ItemArray[i][0], '</p></td>');
			}
			else
			{
				document.writeln('<td colspan=', (Column - j - 1)*2,'><p  class=VoteItem>', ItemArray[i][0], '</p></td>');
			}
		}		

		document.writeln('</tr>');
	}

	document.writeln('</table>');
	document.writeln('</td></tr>');

	document.writeln('<tr><td width="100%" class=BreakLine height=4>&nbsp;</td></tr>');
	document.writeln('<tr><td width="100%" class=BreakLine height=1 bgcolor="', BgColor, '"></td></tr>');
	document.writeln('<tr><td width="100%" height=40 bgcolor="#F2EDDD" align="center">&nbsp;<input type="button" hidefocus value="Bi&#7875;u quy&#7871;t" name="Vote" class=VoteButton style="width: 70" onClick="SubmitVote(this.form, 0)">&nbsp;<input type="button" value="Xem kết quả" name="View" class=VoteButton style="width: 70" hidefocus onClick="SubmitVote(this.form, 1)"></td></tr>');
	document.writeln('<tr><td><Table bgcolor=',BgColor,' cellspacing=0 cellpadding=3 width="100%"><tr><td style="margin-left: 3;" class="Time">Nếu bạn có lựa chọn khác, có thể chia sẻ <a a href="', ContactLink, '" class=Time style="color: ', Color, '"><b>tại đây</b></a></td></tr></Table></td></tr>');		
	document.writeln('</table>');
	document.writeln('</td>');
	document.writeln('</form>');
	document.writeln('</tr>');
	document.writeln('</table>');
}