﻿var attachID="";
var _fuid="";

function updateCharCount(obj,max,labelID)
{
var r=max-obj.value.length;
if(r>200||r==0)$("#"+labelID).removeClass().html("&nbsp;");
else if(r>0&&r<=200)$("#"+labelID).removeClass().html(r+" characters remaining.");
else $("#"+labelID).addClass("ErrorMessage").html("You have "+Math.abs(r)+" characters too many.");
}

function deleteFile()
{
$("#UploadDiv").show();
$("#ProcessingDiv").hide();
$("#FinishedDiv").hide();
if(parent.attachID)
$(parent.document).find("#"+parent.attachID).val("");
}

function startUpload()
{
$("#UploadDiv").hide();
$("#ProcessingDiv").show();
$("#FinishedDiv").hide();
parent.setTimeout("pollServer();",1500);
document.frmAttachFile.submit();
}

function endUpload(newf,oldf){setTimeout("finalizeUpload('"+newf+"','"+oldf+"');",50);}

function finalizeUpload(newf,oldf)
{
$("#UploadDiv").hide();
$("#ProcessingDiv").hide();
$("#FinishedDiv").show();
$("#FilenameDisplay").html(oldf);
$("#RemoveButton").html("Remove&nbsp;File");
if(parent.attachID)
$(parent.document).find("#"+parent.attachID).val(newf);
}

function uploadError(oldf)
{
$("#UploadDiv").hide();
$("#ProcessingDiv").hide();
$("#FinishedDiv").show();
$("#FilenameDisplay").html(oldf);
$("#RemoveButton").html("Upload&nbsp;New&nbsp;File");
}

function pollServer()
{
if(parent.attachID){$.get("CheckUpload.aspx?fuid="+encodeURIComponent(_fuid),"{}",function(data){pollHandler(data);},"text");}
}

function pollHandler(data)
{
	if(data == "ERROR")
	{
		if(!document.all)window.stop();
		else document.execCommand("stop");

		$("iframe")[0].src="AskQuestion_AttachFile.aspx?fuid=" + encodeURIComponent(_fuid) + "&error=Failed. Please try smaller file.";
	}
	else if (data == "UNAVAILABLE")
	{
		setTimeout("pollServer();",1500);
	}
}
function ShowReplyForm(obj,divID,btnID)
{
$("#" + divID).show().find("input.InputText").focus();
$("#" + btnID).hide();
$(obj).hide();
}

function onesubmit(element, delay)
{
	// This function takes an element (typically a submit button) and effectively
	// disables it immediately upon clicking so that duplicate submissions are not
	// sent to the server when users get a little click-happy.  The element is then
	// reset back to its original state after a delay of time (defaults to 1.5
	// seconds) so that users can use the buttons if they click Back in the browser.
	var tagname = element.tagName.toLowerCase();
	
	delay = (delay) ? delay : 1500;
	
	if(tagname == "a")
	{
		var href = element.href;
		var click = element.onclick;
		
		setTimeout(function() { element.href = "javascript:void(0);"; element.onclick = null; }, 0);
		setTimeout(function() { element.href = href; element.onclick = click; }, delay);
	}
	else if(tagname == "input")
	{
		var click = element.onclick;
		
		setTimeout(function() { element.disabled = true; element.onclick = null; }, 0);
		setTimeout(function() { element.disabled = false; element.onclick = click; }, delay);
	}
}

function pop(url, name, w, h)
{
    var props = 'height='+h+',width='+w+',close=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes';
    var win = window.open(url, name, props);
    win.focus();
}