/* Voting system */
function vote(vote, voteEl, rating) {
	ajaxRequest.call('vote', {vote: vote, voteEl:voteEl, rating:rating}, 'post');
}

function fillStars(webroot, voteVal, voteEl) {

	for (i=1 ; i<=voteVal ; i++) {
		if (i%2 == 1) {
	    	$('star' + voteEl + i).src = "/"+webroot+"/images/icoLeftHalfFullStar.png";
		} else {
	    	$('star' + voteEl + i).src = "/"+webroot+"/images/icoRightHalfFullStar.png";
		}
	}
	for (var i=voteVal+1 ; i<=10 ; i++) {
		if (i%2 == 1) {
	    	$('star' + voteEl + i).src = "/"+webroot+"/images/icoLeftHalfEmptyStar.png";
		} else {
	    	$('star' + voteEl + i).src = "/"+webroot+"/images/icoRightHalfEmptyStar.png";
		}
	}
	
	displayRatingText(voteEl, voteVal);
}

function resetStars(webroot, voteEl, voteVal) {

	for (i=1 ; i<=voteVal ; i++) {
		if (i%2 == 1) {
	    	$('star' + voteEl + i).src = "/"+webroot+"/images/icoLeftHalfFullStar.png";
		} else {
	    	$('star' + voteEl + i).src = "/"+webroot+"/images/icoRightHalfFullStar.png";
		}
	}
	for (var i=voteVal+1 ; i<=10 ; i++) {
		if (i%2 == 1) {
	    	$('star' + voteEl + i).src = "/"+webroot+"/images/icoLeftHalfEmptyStar.png";
		} else {
	    	$('star' + voteEl + i).src = "/"+webroot+"/images/icoRightHalfEmptyStar.png";
		}
	}
}
/* End of voting system */


function postTag(id, controller) {
	ajaxRequest.call('posttag', {tagName: $('tagName').value}, 'post');
}

function editTag(tagId) {
	
	var txt = $('newTagName_'+tagId).value; 
	var params = 'tagId='+tagId+'&newtagName='+txt+'&action=edit';
	
	ajaxRequest.call("tagedit", params, 'post');
}

function deleteTag(tagId) {
	
	var params = 'tagId='+tagId+'&action=delete';
	ajaxRequest.call("tagedit", params, 'post');
}

function switchElem(tagId,elemId,tagName){
	 
	$(elemId).update("<input type='text' name='newTagName_"+tagId+"' id='newTagName_"+tagId+"' size='15' maxlength='20' onkeypress=\"if (checkEnter(event)) { editTag(" + tagId +"); }\" value='"+tagName+"'\/>"+
					 "<input onclick=\"editTag(" + tagId +");\" type=\"button\" id='btnAdd' value='Edit' />&nbsp;"+
					 "<input onclick=\"cancel('" + elemId +"','" + tagName +"');\" type=\"button\" id='btnCancel' value='Cancel' />");
} 

function cancel(elemId,tagName){
	$(elemId).update(tagName);
}

function toggler(id){
			
	obj = $(id);
	
	x = $(id + '_img');
	
 	Effect.toggle(obj, 'slide',{ duration: 0.2 });
 	
 	if(x.title=='expand'){
 		
 		x.title='colapse';
 		x.src = 'http://psn.staticcache.com/common/images/toggleDLminus.gif';
 		
 		
 	}else{
 		
 		x.title='expand';
 		x.src = 'http://psn.staticcache.com/common/images/toggleDLplus.gif';
 	
 	}
		 			 
}

/* Account Information */
var globalCharCode = true;
var globalSubmit = false;

function validate(value, checkfield, updatefield, submitButton) {
	
	if (globalSubmit == true) {
		globalCharCode = false;
		globalSubmit = false;
		if(submitButton != '' && $(submitButton).disabled == false){
			ajaxRequest.call(updatefield, {fieldName:value}, 'post');
		}
		else{
			return false;
		}
	}
	else if (globalCharCode == false) {
		return false;
	}

	ajaxRequest.call(checkfield, {fieldName: value}, 'post');
	return true;
}

function checkChars(e, regularExpression) {
	
	globalCharCode = false;
	
	try {
		var charCode = (e.which) ? e.which : event.keyCode;
	} catch (exception) {
		globalCharCode = true;
		return true;
	}
	
	if(charCode == 13){
		globalSubmit = true;
		return false;
	}
	if (charCode < 31){
		globalCharCode = true;
		return true;
	}
	else{
		if(regularExpression == 'numeric' && !(charCode < 48 || charCode > 57) ){
			globalCharCode = true;
			return true;
		}
		if(regularExpression == 'alphabetic' && ( !(charCode < 97 || charCode > 122) || !(charCode < 65 || charCode > 90) || charCode == 39 || charCode == 32 || charCode == 45 ) ){
			globalCharCode = true;
			return true;
		}
		if(regularExpression == 'alphabeticnumeric' && ( !(charCode < 97 || charCode > 122) || !(charCode < 48 || charCode > 57) || !(charCode < 65 || charCode > 90) || charCode == 45 || charCode == 46 || charCode == 95 ) ){
			globalCharCode = true;
			return true;
		}
		if(regularExpression == 'alphabeticusername' && ( !(charCode < 97 || charCode > 122) || !(charCode < 48 || charCode > 57) || !(charCode < 65 || charCode > 90) || charCode == 95 ) ){
			globalCharCode = true;
			return true;
		}
		if(regularExpression == 'printableexceptblank' && !(charCode < 33 || charCode > 126)){
			globalCharCode = true;
			return true;
		}
		return false;
	}		

}

//**************
function checkCharacters(e, regularExpression) {
	
	globalCharCode = false;
	
	try {
		var charCode = (e.which) ? e.which : event.keyCode;
	} catch (exception) {
		globalCharCode = true;
		return true;
	}
	
	if(charCode == 13){
		globalSubmit = true;
		return false;
	}
	if (charCode < 31){
		globalCharCode = true;
		return true;
	}
	else{
		if(regularExpression == 'numeric' && !(charCode < 48 || charCode > 57) ){
			globalCharCode = true;
			return true;
		}
		if(regularExpression == 'alphabetic' && ( !(charCode < 97 || charCode > 122) || !(charCode < 65 || charCode > 90) || charCode == 39 || charCode == 32 || charCode == 45 ) ){
			globalCharCode = true;
			return true;
		}
		if(regularExpression == 'alphabeticnumeric' && ( !(charCode < 97 || charCode > 122) || !(charCode < 48 || charCode > 57) || !(charCode < 65 || charCode > 90) || charCode == 45 || charCode == 46 || charCode == 95 ) ){
			globalCharCode = true;
			return true;
		}
		if(regularExpression == 'alphabeticusername' && ( !(charCode < 97 || charCode > 122) || !(charCode < 48 || charCode > 57) || !(charCode < 65 || charCode > 90) || charCode == 95 ) ){
			globalCharCode = true;
			return true;
		}
		if(regularExpression == 'printableexceptblank' && !(charCode < 33 || charCode > 126)){
			globalCharCode = true;
			return true;
		}
		return false;
	}		

}
//**************


/* Community functions */
function checkReportForm(errorMsg){

	if($('problemMessage').value.blank()){			
		alert(errorMsg);
		return;	
	}		
	
	params = $('formReportProblem').serialize();
	ajaxRequest.call('reportproblem', params, 'post');
}

/* Comments */
function postComment() {

	var params = null; 
	var params = $('commentForm').serialize();
	ajaxRequest.call('postcomment', params, 'post');
}

function inputlimit(textarea, maxcharlimit, maxlinelimit, charfield, linefield, error){
	
	//first make sure the number of max character limit is not violated
	if(textarea.value.length > maxcharlimit){		
		textarea.value= $('wallOldText').value.substring(0, maxcharlimit);	//take the old value	
		return;				
	}
	
	var val=textarea.value.replace(/\r/g,'').split('\n');	
    if (val.length >=6){
      	alert(error);
	  	textarea.value= $('wallOldText').value.substring(0, maxcharlimit);	//take the old value	
		return;	
    }
	
	//save the current value
	$('wallOldText').value = textarea.value;	
	
	//update the display of left char and line 
	charfield.innerHTML = maxcharlimit - textarea.value.length;
	if(linefield){
		linefield.innerHTML = val.length;
	}
}
