var commentFormSubmitted = false;

 function toggleComment(strCommentId) {
	var commentDiv = document.getElementById('cm' + strCommentId);
	var shortDiv   = document.getElementById('shcm' + strCommentId);
	var hrefDiv    = document.getElementById('hr' + strCommentId);
	
	if (commentDiv.style.display == 'block') {
		commentDiv.style.display = 'none';
		shortDiv.style.display  = 'block';
		hrefDiv.innerHTML = 'read more &#187;';
		}
	else {
		commentDiv.style.display = 'block';
		shortDiv.style.display  = 'none';
		hrefDiv.innerHTML = '&#171; collapse';
		}
	}
	
  function toggleDrillDown() {
  	var drillDownDiv = document.getElementById('drillDownDiv');
  	if (drillDownDiv) 
  		 (drillDownDiv.style.display == 'block') ? $('#drillDownDiv').hide('fade') : $('#drillDownDiv').show('fade');
  	}
  	
function report_abuse(iCid) {
	if (confirm('Would you like to report this posting as inapproriate?')) {
		$.ajax({
			type: "GET",
			url: "abuse.php",
			data: 'cid=' + iCid,
			success: function(msg){
				alert(msg)
				}
			});
		}
	}
	
function send_to_friend(sFormBlock, sAction) {
	
	if (!$('#send_friend_mode').val()) {
		sSenderEmail	= '';
		sSenderName 	= '';
		sFriendEmail 	= '';
		sFriendName  	= '';
		sFriendMessage 	= '';
		sMode         	= '';
		}
	else {
		sSenderEmail 	= $('#sender_email').val();
		sSenderName  	= $('#sender_name').val();
		sFriendEmail 	= $('#friend_email').val();
		sFriendName  	= $('#friend_name').val();
		sFriendMessage 	= $('#friend_message').val();
		sMode   		= 'submit';
		}
    $('#sendbutton').val('Sending...');
    $('#sendbutton').attr("disabled","disabled");
	$.ajax({
			type: "POST",
			url: ROOT_URL + "comments/email_to_friend.php",
			data: 'send_friend_mode='  + sMode +
				  '&sender_email=' + sSenderEmail +
				  '&sender_name=' + sSenderName +
				  '&friend_email=' + sFriendEmail + 
				  '&friend_name=' + escape(sFriendName) +
				  '&url=' + escape(location.href) +
				  '&urltitle=' + document.title +
				  '&friend_message=' + escape(sFriendMessage),
			success: function(msg){
				$('#' + sFormBlock).html(msg);
				$('#send_friend_mode').val('submit');
				if ($('#sendbutton').val('Sending...')) $('#sendbutton').val('Send Email');
				}
			});
	}
  	
function load_comment_form(iMakeId, iModelId, iYear, iCatId, iParentId) {
	location.href="#commentform";
	if (!commentFormSubmitted) return;
	$('#statusbox').html('Loading Comment Form...').css('visibility','visible');
		$.ajax({
			type: "GET",
			url: "ajax_comment_form.php",
			data: 'makeId=' + iMakeId +
				  '&modelId=' + iModelId +
				  '&year=' + iYear +
				  '&catId=' + iCatId +
				  '&parentId=' + iParentId,
			success: function(msg){
				$('#commentFormDiv').html(msg);
				$('#help').corner('8px');
				$('#statusbox').html('').css('visibility','hidden');
				document.getElementById('email').focus();
				
				}
			});
	commentFormSubmitted = true;
	}
  	
 //////////////////////////
 
function update_comment_count() {
	var strComment = document.getElementById('usercomment');
	var charsLeftDiv = document.getElementById('cmt_char_count');
	
	var iCharsLeft = cUSERCOMMENT_LEN - strComment.value.length;
	
	if (iCharsLeft < 1) strComment.value = strComment.value.substring(0, cUSERCOMMENT_LEN);
	strComment.focus();
	charsLeftDiv.innerHTML = iCharsLeft;
	}

function submit_comment(strThreadType) {
	// get fields...
	
	var parentId	= document.getElementById('parentId').value;
	var makeId		= document.getElementById('makeId').value;
	var modelId		= document.getElementById('modelId').value;
	var year		= document.getElementById('year').value;
	var catId		= document.getElementById('catId').value;
	var email		= document.getElementById('email').value;
	var passwd		= document.getElementById('passwd').value;
	var location 	= document.getElementById('location').value;
	var usercomment	= document.getElementById('usercomment').value;
	var imgtext		= document.getElementById('imgtext').value;
	var errorMsg 	= '';

	// error check...
	if (!makeId) errorMsg += '\nPlease select a make, model & year';
	if (!errorMsg && !modelId) errorMsg += '\nPlease select a model & year';
	if (!errorMsg && !year) errorMsg += '\nPlease select a year';
	
	if (!email) errorMsg += '\nPlease enter your email address';
	if (!usercomment) errorMsg += '\nPlease enter your comment';
	if (!imgtext) errorMsg += '\nPlease enter the text you see in the security image';
	
	// trim user comment length if it is longer than permitted...
	if (usercomment.length > cUSERCOMMENT_LEN) usercomment = usercomment.substring(0,cUSERCOMMENT_LEN);

	if (errorMsg !='') {
		alert('Comment Submission Error: ' + errorMsg);
		}
	else {
		$('#statusbox').html('Submitting Comment...').css('visibility','visible');
		$.ajax({
			type: "POST",
			url: "ajax_comment_form.php",
			data: "mode=submit" + 
				  "&location=" + location +
				  "&makeId=" + makeId +
				  "&modelId=" + modelId +
				  "&year=" + year +
				  "&catId=" + catId +
				  "&parentId=" + parentId +
				  "&threadType=" + strThreadType +
				  "&email=" + email +
				  "&passwd=" + passwd +
				  "&imgtext=" + imgtext +
				  "&usercomment=" + usercomment,
			success: function(msg){
				$('#commentFormDiv').html(msg);
				$('#help').corner('8px');
				$('#statusbox').html('').css('visibility','hidden');
				document.getElementById('email').focus();
				}
			});
		commentFormSubmitted = true;
		}
	}

function setSelectOptions(ayKeyValues, strSelectId) {
	var selectElement = document.getElementById(strSelectId);
	if (!selectElement) {
		alert('Invalid select ID passed - ' + strSelectId);
		return;
		}
	
	selectElement.options.length = 0;
	var curOpt = 0;
	for (var thisKey in ayKeyValues) {
		selectElement.options[curOpt] = new Option(ayKeyValues[thisKey], thisKey);
		curOpt++;
		}
	}
	
	
function cmtSelectMake() {
	// get make current makeId...
	var makeId  = document.getElementById('makeId').value;
	
	// notify user we are getting years...
	$('#statusbox').html('Loading makes selection...').css('visibility','visible');
	$.ajax({
		  type: "GET",
		  url: "ajax_comment_dropmenus.php",
		  dataType: "html",
		  data: "mode=makes&makeId=" + makeId,
		  success: function(msg){
		  	$("#cmtMakeDiv").html(msg);
		  	$('#statusbox').html('').css('visibility','hidden');
		  	$("#cmtModelDiv").html('');
		  	$("#cmtYearDiv").html('');
		  	}
		  });
	}
	
function setMake() {
	var makeSelect = document.getElementById('makeSelect');
	var makeId     = document.getElementById('makeId');
	
	if (!makeSelect.options[makeSelect.options.selectedIndex].value) {
		alert('Please select a make');
		return;
		}
	
	makeId.value = makeSelect.options[makeSelect.options.selectedIndex].value;
	
	$('#cmtMakeDiv').html('<a href="javascript:cmtSelectMake();">change</a> <b>' + makeSelect.options[makeSelect.options.selectedIndex].innerHTML + '</b>');
	clearYear();
	clearModel();
	clearCat();
	setThreadType('new');
	cmtSelectModel();
	}
	
function clearMake() {
	document.getElementById('makeId').value = '';
	}
	
function cmtSelectModel() {
	// get make & model Ids...
	var makeId  = document.getElementById('makeId').value;
	var modelId = document.getElementById('modelId').value;

	// notify user we are getting years...
	$('#statusbox').html('Loading model selection...').css('visibility','visible');
	 
	$.ajax({
		  type: "GET",
		  url: "ajax_comment_dropmenus.php",
		  dataType: "html",
		  data: "mode=models&makeId=" + makeId + "&modelId=" + modelId,
		  success: function(msg){
		  	$("#cmtModelDiv").html(msg);
		  	$('#statusbox').html('').css('visibility','hidden');
		  	$("#cmtYearDiv").html('');
		  	}
		  });
	}

function setModel() {
	var modelSelect = document.getElementById('modelSelect');
	var modelId       = document.getElementById('modelId');
	
	if (!modelSelect.options[modelSelect.options.selectedIndex].value) {
		alert('Please select a model');
		return;
		}
	
	modelId.value = modelSelect.options[modelSelect.options.selectedIndex].value;
	
	$('#cmtModelDiv').html('<a href="javascript:cmtSelectModel();">change</a> <b>' + modelSelect.options[modelSelect.options.selectedIndex].innerHTML + '</b>');
	clearYear();
	clearCat();
	setThreadType('new');
	cmtSelectYear();
	}

function clearModel() {
	document.getElementById('modelId').value = '';
	}
	
function clearCat() {
	document.getElementById('catId').value = '';
	}
	
function cmtSelectYear() {
	// get make & model Id's...
	var makeId  = document.getElementById('makeId').value;
	var modelId = document.getElementById('modelId').value;
	var year    = document.getElementById('year').value;

	// notify user we are getting years...
	$('#statusbox').html('Loading year selection...').css('visibility','visible');
	 
	 
	//alert('Getting years for\nMake ID: ' + makeId + '\nModel ID: ' + modelId);
	
	$.ajax({
		  type: "GET",
		  url: "ajax_comment_dropmenus.php",
		  dataType: "html",
		  data: "mode=years&makeId=" + makeId + "&modelId=" + modelId + "&year=" + year,
		  success: function(msg){
		  	$("#cmtYearDiv").html(msg);
		  	$('#statusbox').html('').css('visibility','hidden');
		  	}
		  });
	}
	
function setYear() {
	var yearSelect = document.getElementById('yearSelect');
	var year       = document.getElementById('year');
	
	if (!yearSelect.options[yearSelect.options.selectedIndex].value) {
		alert('Please select a year');
		return;
		}
	
	year.value = yearSelect.options[yearSelect.options.selectedIndex].value;	
	$('#cmtYearDiv').html('<a href="javascript:cmtSelectYear();">change</a> <b>' + yearSelect.options[yearSelect.options.selectedIndex].innerHTML + '</b>');
	clearCat();
	setThreadType('new');
	}
	
function setThreadType(strType) {
	document.getElementById('threadType').value = (strType == 'reply') ? 'reply' : 'new';
	}
	
function clearYear() {
	document.getElementById('year').value = '';
	}
