var commentWindowFocused = false;

window.addEvent('load', function(){ 
        var profileItem = $('profileSignIn');
        if (profileItem) {
		    var loadUrl = 'ProfileProcessor.aspx?initialcheck=true';
		    new Ajax(loadUrl, {
			    method: 'get',
			    update: profileItem
		    }).request();
		}
});

function postBackSignIn(formName) {
    var queryString = getFormData(formName);
    var profileItem = $('profileSignIn');
	var loadUrl = 'ProfileProcessor.aspx?' + queryString;
	new Ajax(loadUrl, {
		method: 'get',
		update: profileItem
	}).request();   
}

function signOutUser() {
    var profileItem = $('profileSignIn');
	var loadUrl = 'ProfileProcessor.aspx?process=signout';
	new Ajax(loadUrl, {
		method: 'get',
		update: profileItem
	}).request();   
}

function postComment(formName) {
    var commentData = $('txtComment');
    var divCommentResponse = $('divCommentResponse');
    divCommentResponse.innerHTML = "";
    if (commentData.value != '') {
        var formData = getFormData(formName);
        var displayedItem = $('displayComment');
        var contentId = displayedItem.contentid;
	    var loadUrl = 'WageringCommentProcessor.aspx?action=post&contentId=' + contentId + '&' + formData;
	    new Ajax(loadUrl, {
		    method: 'get',
		    update: divCommentResponse,
		    onComplete: function(text) {
		        if (text) {
	                if (text.indexOf("submitted") != -1) {
	                    commentData.value = '';
	                }
	            }
		    }
		}).request();   
	}
}

function setCurrentComment(contentId, longTitle) {
    var commentData = $('txtComment');
    if (!commentWindowFocused && commentData.value == '') {
        var displayedItem = $('displayComment');
        displayedItem.contentid = contentId;
        displayedItem.innerHTML = longTitle;
    }    
}

function getFormData(formName) {
	formId=document.getElementById(formName);
	var postStr= '';
	var first = true;
	for (i = 0; i < formId.elements.length; i++) {
		formElem = formId.elements[i];
		switch (formElem.type) {
			case 'text':
			case 'password':
            case 'hidden':
			case 'textarea':
			case 'submit':
			{
			    if (first) {
                    postStr += formElem.name + '=' + urlEncode(formElem.value);
	                first = false;
			    } else {
                    postStr += '&' + formElem.name + '=' + urlEncode(formElem.value);
			    }
			}
		break;
		}
	}
	return postStr;
}

function urlEncode(encodeValue)
{
    encodeValue = escape(encodeValue);
    encodeValue = encodeValue.replace(/\//g,"%2F");
    encodeValue = encodeValue.replace(/\+/g,"%2B");    
    
    return encodeValue;
}

function commentFocus() {
    commentWindowFocused = true;
}

function commentBlur() {
    commentWindowFocused = false;
}

function openWindow(url,width,height) {
    window.open(url,"","width=" + width + ",height=" + height);
}
