JPGMAG.countWords=function(field){
	var text=field.val()+' ';
	text=text.replace(/^[^A-Za-z0-9]+/gi,"");
	do {text=text.replace('  ',' ');} while (text.search('  ')>-1);
    text=text.replace(/[^A-Za-z0-9]+/gi," ");
    wordCount=text.split(' ').length-1;
    return wordCount;
}

JPGMAG.checkStoryLength=function(){
	var words=$j(this).parent('div').find('span[id^="wordsSoFar"]');
	var minCount=$j(this).parent('div').find('input[id^="minCount"]').val();
	var maxCount=$j(this).parent('div').find('input[id^="maxCount"]').val();
	var check=$j(this).parent('div').find('span[id^="charleft"]');
	var wordCount=JPGMAG.countWords($j(this));
	words.html(($j(this).val().length==0)?'0':wordCount);
	if((wordCount>=minCount && wordCount<=maxCount && minCount>0) || (wordCount>minCount && wordCount<=maxCount)){check.html('<img src="/images/WriteCool.gif" alt="Cool!" />');}
	else if(wordCount<minCount){check.html('');}
	else if(wordCount>maxCount){check.html('<img src="/images/WriteTooMuch.gif" alt="Too Much!" />');}
}

JPGMAG.checkShortLength=function(){
      var words = $j('#shortWordsSoFar');
      var wordCount = JPGMAG.countWords($j(this));
      words.html(($j(this).val().length==0)?'0':wordCount);
      if(wordCount>25){$j('#shortWordsLeft').html('<img src="/images/WriteTooMuch.gif" alt="Too Much!" />');}
      else if(wordCount<26 && wordCount>0){$j('#shortWordsLeft').html('<img src="/images/WriteCool.gif" alt="Cool!" />');}
      else if(wordCount==0){$j('#shortWordsLeft').html('');}
}

JPGMAG.selectThemeView=function(){
	if($j(this).is(':checked')){$j('#themeLabelBox,#themeSelectBox').show();}
	else{$j('#themeLabelBox,#themeSelectBox').hide();$j('#themeSelect').val('');}
}
if(!JPGMAG.messagePreview){
	JPGMAG.messagePreview=function(){
		if($j(this).val().length>=500){$j(this).val($j(this).val().substr(0,499));}
		$j('#preview').html(JPGMAG.lb2br($j(this).val()));
	}
}

JPGMAG.emptyInput=function(){$j(this).val('').unbind('focus');}

JPGMAG.checkEmailAddress=function(){
	var el=$j(this);
	$j.ajax({data:{e:el.val()},url:'/actions/email_check.php',type:'POST',success:function(d){if(d!=0){el.removeClass('emError').removeClass('formError');}else{el.addClass('emError');}}});
}

JPGMAG.errorCheckEmailForm=function(){
	var f=$j('#voteTellForm .emailfield'),namefields=$j('#voteTellForm .vn'),emailFields=$j('#voteTellForm .vn');
	var needAlert=0, alertTxt='';
	$j('#voteTellForm .emailfield').each(function(){
		if($j(this).val()==''||$j(this).val()=='(name)'||$j(this).val()=='(email)'){
			needAlert++;
			$j(this).addClass('formError');
			//reset this value to (name) or (email)
			$j(this).val(($j(this).hasClass('ve'))?'(email)':'(name)');
			$j(this).focus(JPGMAG.emptyInput);
		}else if($j(this).is('.ve')&&($j(this).val().indexOf('@')<1||$j(this).val().length<6||($j(this).val().indexOf('.',$j(this).val().indexOf('@'))-$j(this).val().indexOf('@'))<2)){
			$j(this).addClass('emError');
		}
	});
	if(needAlert>0){alertTxt+=' You need to include a name and email address in the To and From lines.';}
	if($j('#voteTellForm .emError').length){
		alertTxt+=' '+((needAlert>0)?'Also, i':'I')+'t looks like one or both of the email fields contain an invalid email address.';
		needAlert++;
	}
	if(needAlert>0){
		alert('Oops! We can\'t send this yet.'+alertTxt+' Mind double-checking that? Thanks!');
		return false;
	}
}

$j(function(){
	$j('#emailLink').click(function(){$j('#voting').hide();$j('#emailAFriend').show();$j('#voteTellForm input,#voteTellForm textarea').attr('disabled',(($j('#emailAFriend').is(':hidden'))?'disabled':''));return false;});
    $j('#cancelEmail').click(function(){$j('#emailAFriend').hide();$j('#voting').show();$j('#voteTellForm input,#voteTellForm textarea').attr('disabled',(($j('#emailAFriend').is(':hidden'))?'disabled':''));return false;});
    $j('#toNameField').focus(JPGMAG.emptyInput);
    $j('#toEmailField').focus(JPGMAG.emptyInput);
	$j('.vn').blur(function(){if($j(this).val!=''){$j(this).removeClass('formError')}else{$j(this).addClass('formError')}});
	$j('.ve').blur(JPGMAG.checkEmailAddress);
    $j('#emailMessage').keyup(JPGMAG.messagePreview).keyup();
    if($j('#fromNameField').val()=='Your Name'){$j('#fromNameField').focus(JPGMAG.emptyInput);}
    if($j('#fromEmailField').val()=='Your Email'){$j('#fromEmailField').focus(JPGMAG.emptyInput);}
	$j('#voteTellForm').submit(JPGMAG.errorCheckEmailForm);
	$j('.storytextfield').keyup(JPGMAG.checkStoryLength).keyup();
	$j('#shortdesc').keyup(JPGMAG.checkShortLength).keyup();
	if($j('#submittedCheck').is(':not(:checked)')){$j('#themeLabelBox,#themeSelectBox').hide();}
	$j('#submittedCheck').click(JPGMAG.selectThemeView);
});