function checkDuplicateUsername(username) {
    document.getElementById("statusBoxUsername").innerHTML = "Checking for Duplicates...";

    var url = '/?event=user.checkForDuplicateUsername&username=' + username;
    var update = new Ajax.Request(
        url,
        {
        method: 'get',
        onComplete: checkDuplicateUsernameResults
       
    });
    return true;
}

function checkDuplicateUsernameResults(request) {
    var returnedText = request.responseText.replace(/^\s+|\s+$/g,""); //trim spaces
	
    if (returnedText==1)
    {
        document.getElementById("statusBoxUsername").className = "duplicateUser";
        document.getElementById("statusBoxUsername").innerHTML = "This username is already taken; you will have to pick another.";   
    	document.useredit.username.focus();
    }
    else
    {
        document.getElementById("statusBoxUsername").className = "availableUser";
        document.getElementById("statusBoxUsername").innerHTML = "Congratulations! This user name is available.";
    }
}

function updateGroupEmailType(alertStatus,groupID) {
	document.getElementById("groupEmailTypeStatus").style.display = "";
    document.getElementById("groupEmailTypeStatus").innerHTML = "Saving Email Preferences...";

    var url = '/?event=user.updateGroupEmailType&alertStatus=' + alertStatus + '&groupID=' + groupID;
    var update = new Ajax.Request(
        url,
        {
        method: 'get',
        onComplete: updateGroupEmailTypeResults
       
    });
    return true;
}

function updateGroupEmailTypeResults(request) {
    var returnedText = request.responseText.replace(/^\s+|\s+$/g,""); //trim spaces
	console.log('returnedText: ' + returnedText);
    if (returnedText==1)
    {
       document.getElementById("groupEmailTypeStatus").innerHTML = "<strong>Saved.</strong";   
    }
}

function checkDuplicateGroupName(groupName) {
    document.getElementById("statusBoxGroupName").innerHTML = "Checking for Duplicates...";
   
    var url = '/?event=group.checkForDuplicategroupName&groupName=' + groupName;
    var update = new Ajax.Request(
        url,
        {
        method: 'get',
        onComplete: checkDuplicateGroupNameResults
       
    });
    return true;
}

function checkDuplicateGroupNameResults(request) {
    var returnedText = request.responseText.replace(/^\s+|\s+$/g,""); //trim spaces
    if (returnedText.charAt(0)==1)
    {
        document.getElementById("statusBoxGroupName").className = "duplicateUser";
        document.getElementById("statusBoxGroupName").innerHTML = "This group name is already taken; you will have to pick another.";   
    	document.useredit.username.focus();
    }
    else
    {
        document.getElementById("statusBoxGroupName").className = "availableUser";
        document.getElementById("statusBoxGroupName").innerHTML = "Congratulations! This group name is available.";
    }
}


function checkDuplicateEmail(email) {
    document.getElementById("statusBoxEmail").innerHTML = "Checking for Duplicates...";
   
    var url = '/?event=user.checkForDuplicateEmail&email=' + email;
    var update = new Ajax.Request(
        url,
        {
        method: 'get',
        onComplete: checkDuplicateEmailResults
       
    });
    return true;
}

function checkDuplicateEmailResults(request) {
    var returnedText = request.responseText.replace(/^\s+|\s+$/g,""); //trim spaces

    if (returnedText==1)
    {
        document.getElementById("statusBoxEmail").className = "duplicateUser";
        document.getElementById("statusBoxEmail").innerHTML = "This email address is already in user; you will have to pick another.  Did you <a href='/?event=forgetEmail'>forget your password</a>";   
    }
    else
    {
        document.getElementById("statusBoxEmail").className = "availableUser";
        document.getElementById("statusBoxEmail").innerHTML = "Yippee! This email address has not been used yet.";
 
    }
}


function removeNonAlpha(domain){
	var temp = '';
	temp =  domain.replace(/[^a-zA-Z]+/g,'');
	document.getElementById("domainName").value = temp;
}


function voteIdea(ideaID,voteValue) {
    
    var url = '/?event=idea.vote&ideaID=' + ideaID + '&voteValue=' + voteValue;
	
    var update = new Ajax.Request(
        url,
        {
        method: 'get',
        onComplete: voteIdeaResults
       
    });
    return true;
}



function votecomment(commentID,voteValue) {
     var url = '/?event=comment.vote&commentID=' + commentID + '&voteValue=' + voteValue;
    var update = new Ajax.Request(
        url,
        {
        method: 'get',
        onComplete: voteCommentResults
       
    });
    return true;
}


function voteBestComment(commentID,ideaID) {
     var url = '/?event=comment.voteBestComment&commentID=' + commentID + '&ideaID=' + ideaID;
    var update = new Ajax.Request(
        url,
        {
        method: 'get',
        onComplete: voteBestCommentResults
       
    });
    return true;
}


function voteBestCommentResults(request) {
	var returnedText = request.responseText.replace(/^\s+|\s+$/g,"");
	var splitResult = returnedText.split(",");
	// status code
	var r_statusCode = parseFloat(splitResult[0]);
	// the total votecount div we need to update
	var r_targetDivID = splitResult[1];
	// the new vote count

	
	if (r_statusCode == 1){
		var resulthtml = "Thanks for voting!" ;
		document.getElementById(r_targetDivID).innerHTML = resulthtml ;	
		// hide the voting links 
		//$id('votebestcomment').style.visibility='none';	
		//alert($id('votebestcomment'));	
	} else if (r_statusCode == 0){
		document.getElementById(r_targetDivID).innerHTML = "You have already voted for your favorite comment for this idea posting" ;	
	}
}



function voteCommentResults(request) {
	var returnedText = request.responseText.replace(/^\s+|\s+$/g,"");
	
	var splitResult = returnedText.split(",");
	// status code
	var r_statusCode = parseFloat(splitResult[0]);
	// the total votecount div we need to update
	var r_targetDivID = splitResult[1];
	// the new vote count
	
	var r_newCount = parseFloat(splitResult[2]);
	// How many people have voted
	
	var r_positiveCount = parseFloat(splitResult[3]);
	
	var r_voteValue = parseFloat(splitResult[4]);
	
	if (r_statusCode == 1){
		var resulthtml = r_positiveCount + " of " + r_newCount + " people found this useful " ;
		document.getElementById(r_targetDivID).innerHTML = resulthtml ;	
		
		
	}
}



  
function voteIdeaResults(request) {
		
	var returnedText = request.responseText.replace(/^\s+|\s+$/g,"");
	var splitResult = returnedText.split(",");
	// status code
	var r_statusCode = parseFloat(splitResult[0]);
	// the total votecount div we need to update
	var r_targetDivID = splitResult[1];
	// the new vote counts
	var r_newCountPositive = splitResult[2];
	var r_newCountNegative = splitResult[3];
	// the users vote
	var r_voteValue = parseFloat(splitResult[4]);
	
	
	if (r_statusCode == 1){
		document.getElementById(r_targetDivID).innerHTML='';
		if (r_newCountNegative!=0)
		 document.getElementById(r_targetDivID).innerHTML = document.getElementById(r_targetDivID).innerHTML+ "-"+r_newCountNegative;	
		if (r_newCountPositive!=0 )
		  document.getElementById(r_targetDivID).innerHTML = document.getElementById(r_targetDivID).innerHTML+ "   +" +r_newCountPositive ;
		 //update selected vote indicator based on vote		 
		 if (r_voteValue == 1){
		 	document.getElementById(r_targetDivID + '_yes').className = "vote-up-sel";
		 	document.getElementById(r_targetDivID + '_no').className = "vote-down";
		 }
		 
		 else
		 {
		 	document.getElementById(r_targetDivID + '_no').className = "vote-down-sel";
		 	document.getElementById(r_targetDivID + '_yes').className = "vote-up";
		 }		 
	}
}



function flagIdea(ideaID) {
    var flaggingReason = prompt("Why are you flagging this?","");
    var url = '/?event=idea.flag&ideaID=' + ideaID + '&flaggingReason=' + flaggingReason;
    var update = new Ajax.Request(
        url,
        {
        method: 'get',
        onComplete: flagIdeaResults
       
    });
    return true;
}

function flagIdeaResults() {
	alert('The group Moderator has been notified.  Thank you');
}
