var targetFile = '/community/user-reviews/index.cfm';

function getTitles(obj) {
	url = targetFile +'?mode=ajax&type=get_titles&platform=' + obj.value;
	new Ajax.Updater('titles',url);
	$('titles').innerHTML = "loading " + $('platform').options[$('platform').selectedIndex].text + " titles..."
}

function setTitle(obj) {
	$('product_title').innerHTML = obj.options[obj.selectedIndex].text + " [ " + $('platform').options[$('platform').selectedIndex].text + " ]";
	new Effect.Appear('product_title');
	$('submit_button').removeAttribute("disabled");
}

function writeReview(formObj) {
	if ($('global_id').value && $('platform').value) {
		newAction = "/community/user-reviews/review/compose/" + $('global_id').value + "/" + $('platform').value + "/";
		formObj.setAttribute("action",newAction);
		formObj.submit();
	} else {
		alert('please select a product'); return;	
	}
}

function setReviewPlatform(platform, global_id) {
	targetName = platform + "_review";
	$(targetName).addClassName("on");
	platOptions = $A($('platform').getElementsByTagName("option"));
	loopCount = 0;
	platOptions.each(function(optionAt){
		if (optionAt.value == platform) {
			$('platform').selectedIndex = loopCount;
			url = targetFile +'?mode=ajax&type=get_titles&platform=' + platform;
			new Ajax.Updater('titles',url);
			$('titles').innerHTML = "loading " + platform + " titles..."
		}
		loopCount++;
	});
	if (global_id) {
		function setProductHere() {
			optionList = $A($('global_id').options);
			loopCount = 0;
			optionList.each(function(optionAt) {
			if (optionAt.value == global_id) {
				$('global_id').selectedIndex = loopCount;
				setTitle($('global_id'));
			}
			loopCount++;
			});
		}
		window.setTimeout(setProductHere, 1000);
	}
}

titleMax = 70;

function countText(obj) {
	textCount = obj.value.length;
	targetObj = $(obj.id + '_count');
	if (obj.id == 'title') {
		targetObj.innerHTML = textCount + ' <i>[ 70 characters maximum ]</i>';
	} else {
		targetObj.innerHTML = textCount + ' <i>[ 200 characters minimum ]</i>';
	}
	$(targetObj).addClassName("on");
	if (obj.id != 'body' && textCount > eval(obj.id + 'Max')) {
		$(targetObj).addClassName("error");
	} else {
		$(targetObj).removeClassName("error");
	}
}

function validateReview (formObj) {
	failPoint = 0;
	errorMssg = '';
	
	if (!$('title').value || $('title').value.length > titleMax) {
		failPoint++;
		errorMssg += "<li>Your title is either missing or too long. </li>";
	}
	
	if (!$('score').value) {
		failPoint++;
		errorMssg += "<li>Your score is missing. Please select one.</li>";
	}
	
	if (!$('body').value) {
		failPoint++;
		errorMssg += "<li>Your review is missing. Please enter your user review.</li>";
	} else if (!$('body').value.indexOf(" ") == -1 || $('body').value.length < 200) {
		failPoint++;
		errorMssg += "<li>Your review is too short. Please meet the 200 character minimum as stated.</li>"
	}
		
	if (failPoint != 0) {
		$('error_mssg').innerHTML = "<h4>There is an Error</h4><ol>" + errorMssg + "</ol>";
		new Effect.ScrollTo('error_mssg');
		new Effect.Appear('error_mssg');
		return;
	} else {
		url = window.location.href.split("/");
		global_id = url[7];
		platform = url[8];
		newAction  = "/community/user-reviews/review/write/" + global_id + "/" + platform + "/";
		$('review_form').setAttribute("action",newAction);
		formObj.submit();	
	}
}




