var retImageStr = "WAppServerPage.jsp?TransID=RVEIMAGE&File=";
var newPollURL = "WAppServerPage.jsp?TransID=NEWPOLLR&VoteID=";
var penPollURL = "WAppServerPage.jsp?TransID=RPENPOLL";

function getPollTypes() {

	return [["P", "Regular Poll"], 
			["S", "Showdown"]];
}

function getCategoryTypes() {

	return [["P", "Regular Poll"], 
			["S", "Showdown"]];
}

function PicItem(doc) {

	this.itemNum = getNodeValue(doc, "ItemNum");
	this.main = (getNodeValue(doc, "Main") == "true");
	this.file = getNodeValue(doc, "File");
	this.ownerid = getNodeValue(doc, "UserID");
	this.owner = getNodeValue(doc, "Username");
	this.mA = (getNodeValue(doc, "MA") == "true");
	this.big = getNodeValue(doc, "BigFile");
	this.admin = (getNodeValue(doc, "Admin") == "true");
	this.lastupdated = getNodeValue(doc, "LastUpdated");
}

function AltPicItem(filename, bigfilename, owner) {

	var fileStr = filename.split(".");
	this.main = false;
	this.file = filename;
	this.ownerid = fileStr[3];
	this.owner = owner;
	this.mA = fileStr[4] == "Y";
	this.big = bigfilename;
}

function cCreatePoll(response) {

	reloadWindow(newPollURL + getNodeValue(response, "VoteID"));
}
/*
*/

function postApprove(actionDiv) {

	document.getElementById('ShoutType').value='G';
	ajaxSubmit(actionDiv);
}

function postReject(actionDiv) {

	document.getElementById('ShoutType').value='B';
	ajaxSubmit(actionDiv);
}

function updatePoll() {

	showPopWin('PollWindow', true);
	curvyCorners(setting, "#PollWindow");
}

function cUpdatePoll(response) {
	
	document.getElementById('LastUpdated').value = getNodeValue(response, "LastUpdated");  
	document.getElementById('VoteDescTitle').innerHTML = getNodeValue(response, "VoteDesc"); 
	document.getElementById('CategoryTitle').innerHTML = getNodeValue(response, "Category") + " - " + getNodeValue(response, "SubCategory");

	closePopWin('PollWindow');
}

function approvePoll() {

	showPopWin('ApproveWindow', true);
}

/*
*/

function holdPoll() {

	showPopWin('HoldWindow');
}

function rejectPoll() {

	showPopWin('RejectWindow');
}

function addComments(obj) {

	showPopWin('CommentsWindow');
}

function addVoteItem() {

	showPopWin('NewItemWindow');
}

function cAddVoteItem(response) {

	var newVoteItem = getNodeValue(response, "ItemCount");
	var voteItemDesc = getNodeValue(response, "ItemDesc");
	var itemStatus = getNodeValue(response, "ItemStatus");
	var itemUser = getNodeValue(response, "ItemUser");
	var pollType = getNodeValue(response, "Type");

	document.getElementById('LastUpdated').value = getNodeValue(response, "LastUpdated");  
	
	var newChoiceDiv = '<div class="VoteItemDiv" ><table style="width:100%;" cellpadding="1" cellspacing="0">';
	newChoiceDiv += '<tr><td class="VoteItemHeader' + (itemStatus == 'H' ? 'Pending' : 'Accept') + '" >' + itemUser + '</td></tr>';
	newChoiceDiv += '<tr><td id="MainPicActions' + newVoteItem + '" class="DisplayValue6" style="text-align:left;">&nbsp;</td></tr>'; 
	newChoiceDiv += '<tr><td style="text-align:center;"><img id="MainPic' + newVoteItem + '"class="VoteImage" alt="No Picture" src="../images/NoPic.jpg" /></td></tr>'; 
	newChoiceDiv += '<tr><td id="MainPicOwner' + newVoteItem + '" class="DisplayValue7" style="text-align:center;">&nbsp;</td></tr>';
	newChoiceDiv += '<tr><td style="text-align:center;" class="DisplayValue6">';
	newChoiceDiv += '<span class="AjaxLinks" onclick="editVoteItem(' + newVoteItem + ', this); \">Edit</span>&nbsp;&nbsp;'; 
	newChoiceDiv += '<span class="AjaxLinks" onclick="editItemImage(' + newVoteItem + ', this);\">Upload Pic</span>';
	newChoiceDiv += '&nbsp;&nbsp;<span class="AjaxLinks" onclick="rejectPollItem(true, ' + newVoteItem + ');\">Reject</span>'; 
	newChoiceDiv += '</td></tr><tr><td><div class="VoteItemDescDiv" onmouseover="className=\'VoteItemDescDivHover\';" onmouseout="className=\'VoteItemDescDiv\';" id="VoteItemDesc' + newVoteItem + '" class="DisplayValue" style="' + (pollType == "S" ? "text-align:center;" : "text-align:left;") + '">' + voteItemDesc + '</div></td></tr>'; 
	newChoiceDiv += '</table></div>';

	newChoiceDiv += '<div class="AjaxPopupWindowParent" ><div class="AjaxPopupWindowBackground" ></div><div id="AltImages' + newVoteItem + '"  class="AjaxPopupWindow" >'; 
	newChoiceDiv += '<table style="width:100%;" cellpadding="10" cellspacing="0"><tr>';
	newChoiceDiv += '<td class="AjaxPopupWindowHeader"><table style="width:100%;" cellspacing="0" cellpadding="0"><tr><td style="text-align:left;">Alternate Images:</td><td style="text-align:right;"><span style="cursor:pointer;" onclick="closePopWin(\'AltImages' + newVoteItem + '\');">close[x]</span></td></tr></table></td></tr>';
	newChoiceDiv += '<tr><td>Users can upload an alternate image for a choice. Voote.com will select the best picture.</td></tr>';
	newChoiceDiv += '<tr><td id="AltImagesContent' + newVoteItem + '" style="text-align:center;"></td></tr></table></div></div>';
    
	document.getElementById('ChoicesTable').innerHTML = newChoiceDiv + document.getElementById('ChoicesTable').innerHTML;
	closePopWin('NewItemWindow');
}

function editVoteItem(itemNum, obj) {

	showPopWin('UpdateItemWindow');

	document.getElementById('VoteItemID').value = itemNum;
	var itemDescObj = document.getElementById('VoteItem');
	itemDescObj.value = document.getElementById('VoteItemDesc' + itemNum).innerHTML.convertAmpersand();
	itemDescObj.onkeyup();
	
}

function cEditVoteItem(response) {

	var voteItem = getNodeValue(response, "ItemID");
	document.getElementById('LastUpdated').value = getNodeValue(response, "LastUpdated");  
	document.getElementById('VoteItemDesc' + voteItem).innerHTML = getNodeValue(response, "ItemDesc"); 
	closePopWin('UpdateItemWindow');
}

function editItemImage(itemNum, obj) {

	showPopWin('UploadImageWindow');

	var imageForm = document.getElementById('UploadPics');
	imageForm.elements["VoteItemID"].value = itemNum;
}

function showAltImages(itemNum, obj) {

	showPopWin('AltImages' + itemNum);
}

function rejectPollItem(reject, itemNum) {

	var transId = "XUITMS";
	var voteId = document.getElementById('VoteID').value;
	var voteItemId = itemNum;
	var status = (reject ? "R" : "N");
    var parms = "TransID=" + transId + "&VoteID=" + voteId + "&VoteItemID=" + voteItemId + "&Status=" + status;

    ajaxCall(urlHost, parms);    
}

function approvePollItem(itemNum) {

	var transId = "XAITEM";
	var voteId = document.getElementById('VoteID').value;
	var voteItemId = itemNum;
	var status = "N";
    var parms = "TransID=" + transId + "&VoteID=" + voteId + "&VoteItemID=" + voteItemId + "&Status=" + status;

    ajaxCall(urlHost, parms);        
}

function uploadImageSubmit(actionDiv) {

	var uploadForm = document.getElementById(actionDiv.transid);
	uploadForm.elements['LastUpdated'].value = document.getElementById('LastUpdated').value;
	uploadForm.submit();
}

function loadPic(actionDiv) {

	if (!actionDiv.transid) return;

	var targetFrame = document.getElementById(actionDiv.transid + ".Target");
	var content = targetFrame.contentWindow.document;
	var results = new iFrameReturn(content);
	var voteId = document.getElementById("VoteID");

    if (results.code == "") {
    	// do nothing
    }
    else if (results.code == 0) {
        clearFields(actionDiv);
	    var pic = new PicItem(content.XMLDocument ? content.XMLDocument : content);
	    if (pic.main) {
		    loadMainPic(pic);
	    }
	    else {
	    	loadAltPic(pic, voteId, pic.admin); 
	    }
		document.getElementById('LastUpdated').value = pic.lastupdated  
	    closePopWin('UploadImageWindow');
    }
    else if (results.code == 1) {
        clearFields(actionDiv);
        reloadWindow();
    }
    else {
    	actionDiv.msgDiv.innerHTML = formatError(results.message);
    }
}

function loadMainPic(pic) {

	var itemNum = pic.itemNum;
	document.getElementById("MainPic" + itemNum).src = '../images/' + pic.file + "?" + (new Date()).getTime();
	document.getElementById("MainPicOwner" + itemNum).innerHTML = pic.owner;
	var picMA = (pic.mA ? '<span style="color:#FF0000;">MA</span>&nbsp;&nbsp;' : '');
	var picBig = (pic.big == '' ? '' : '<a target="_blank" href="' + '../images/' + pic.big + '">Big Pic</a>&nbsp;&nbsp;');
	document.getElementById("MainPicActions" + itemNum).innerHTML = picMA + picBig + '&nbsp;';  
}

function loadAltPic(pic, voteid, admin) {

	var altPicDiv = getAltImage(pic.file, pic.big, pic.owner, pic.itemNum, voteid, admin)
	document.getElementById("AltImagesContent" + pic.itemNum).innerHTML = altPicDiv + document.getElementById("AltImagesContent" + pic.itemNum).innerHTML;
	var morePics = '<span class="AjaxLinks" onclick="showAltImages(' + pic.itemNum + ', this);" style="">More Pics</span>';
	var imgActions = document.getElementById("MainPicActions" + pic.itemNum).innerHTML;
	if (imgActions.indexOf('More Pics') < 0) document.getElementById("MainPicActions" + pic.itemNum).innerHTML = imgActions + morePics;  
}

function getAltImage(filename, bigFilename, owner, itemNum, voteId, admin) {

	var pic = new AltPicItem(filename, bigFilename, owner);
	var uqid = filename;
	
	var onclick = (admin ? 'switchAltImage(this, ' + itemNum + ', \'' + pic.file + '\'); ' : '');
	
	var htmlCode = '<div class="ImageDiv" style="position:relative;" > \
			<table class="DisplayValue7"> \
			<tr><td colspan="2">' + pic.owner + '</td></tr> \
			<tr><td colspan="2" onclick="' + onclick + '" style="text-align:center;"> \
						<img src="../images/' + pic.file + '" style="" /></td></tr> \
			<tr><td style="text-align:left;color:#FF0000;">' + (pic.mA ? "MA" : "&nbsp;") + '</td> \
			<td style="text-align:right;">' + (pic.big == "" ? "" : "<a target='_blank' href='../images/" + pic.big + "'>Larger</a>") + '</td></tr> \
			</table> \
			</div>';
	
    return htmlCode;
}

function showAltImage(filename, bigFilename, owner, itemNum, voteId, admin) {

    document.write(getAltImage(filename, bigFilename, owner, itemNum, voteId, admin));
}

function switchAltImage(div, itemNum, file) {
	
	var voteid = document.getElementById("VoteID").value;
	var url = urlHost + "?TransID=XSIMG&VoteID=" + voteid + "&VoteItemID=" + itemNum + "&File=" + encodeURI(file) + '&Ajax=N&rt=' + (new Date()).getTime();
	ajaxCall(url);
	//window.location=urlHost + "?" + requestHeader;
}

function setCalendarPos(obj, calId) {

	var divWin = document.getElementById(calId);
	divWin.style.top = (getY(obj) - getY(divWin.parentNode) + 20) + 'px';
	divWin.style.left = (getX(obj) - getX(divWin.parentNode) - 0) + 'px';
}

function getPostComments() {

    var url = urlHostJsp + '/GetPostComments.jsp?' + '&rt=' + (new Date()).getTime();
    ajaxCall(url, null, cGetPostComments);
}

function cGetPostComments(response) {

    setInnerHTML(document.getElementById('PostComments'), response);
}






/*
function loadPics(content, itemNum, voteId, num, admin) {

	if (content.getElementById("Main" + (num ? num : ""))) {
	    var pic = new PicItem(content, num);
	    if (pic.main) {
	    	loadMainPic(pic, itemNum);
	    }
	    else {
	    	loadAltPic(pic, itemNum, voteId, admin);
	    }
	}	
}


function getItem(itemNum) {

	var voteid = document.getElementById("VoteID").value;
	var requestHeader = "?TransID=XRITEM&VoteID=" + voteid + "&VoteItemID=" + itemNum;
    var url = urlHost + requestHeader + '&rt=' + (new Date()).getTime();
    return getPage(url);
}

function showItem(itemNum) {

    document.write(getItem(itemNum));
}

function loadAltPic(pic, itemNum, voteId, admin) {

    var altImages = document.getElementById("AltImages" + itemNum);
    var newImage = getAltImage(pic.file, pic.big, pic.owner, itemNum, voteId, admin);
    altImages.innerHTML += newImage;  
}

function createPollSubmit(actionDiv) {

	var transId = actionDiv.transid;
    var parms = "TransID=" + transId + getParms(actionDiv);

    var results = new AjaxReturn(getPage(urlHost, parms));
    if (results.code == 0) {
    	var voteid = results.response;
        reloadWindow(newPollURL + voteid);
    }
    else {
    	actionDiv.msgDiv.innerHTML = formatError(results.response);
    }
}

function approvePollSubmit(actionDiv) {

	var transId = actionDiv.transid;
    var parms = "TransID=" + transId + getParms(actionDiv);

    var results = new AjaxReturn(getPage(urlHost, parms));
    if (results.code == 0) {
        reloadWindow(penPollURL);
    }
    else {
    	actionDiv.msgDiv.innerHTML = formatError(results.response);
    }
}

*/
