function is_enter(function_name, e) {
	if(e && e.keyCode == 13) {
		eval(function_name+'();');
	}
}

function trim(str) {
	var s = new String(str);
	return s.trim();
}

String.prototype.trim = function() {
	return this.replace(/^\s*|\s(?=\s)|\s*$/g, "");
}

function email_validation(email) {
	var filter	= /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if(filter.test(email))
		return true;
	else
		return false;
}

function username_validation(username) {
	var filter	= /^([a-zA-Z0-9_\-]{2,25})+$/;
	
	if(filter.test(username))
		return true;
	else
		return false;
}

function toggleList(lname) {
	if($('#'+lname).attr('className') == 'sel')
		$('#'+lname).attr('className', '');
		
	else
		$('#'+lname).attr('className', 'sel');
	
	$('#'+lname+' ul').slideToggle();
	
	return false;
}

function toggleSection(sid) {
	$('#'+sid).slideToggle();
	return false;
}

function validateFields(validationArray) {
	for(i in validationArray) {
		var n = validationArray[i][0];
		var f = validationArray[i][1];
		var r = validationArray[i][2];

		if($('#'+i).val() == '' && r) {
			validationError('Please enter your '+n+'!');
			return false;
		
		} else if($('#'+i).val() == '' && !r) {
			
		} else if(!validateField(i, n, f, r)) {
			return false;
		}
	}
	
	return true;
}

function validateField(v, n, f, r) {
	var val = $('#'+v).val();
	
	switch(f) {
		case 'notEmpty':
			if(val == '') {
				validationError('Please enter your '+n+'!');
				return false;
			}
			
			return true;
			break;
		
		case 'notZero':
			if(val == '0') {
				validationError('Please select your '+n+'!');
				return false;
			}
			
			return true;
			break;
		
		case 'username':
			var usernameRE = /^[0-9a-z_-]{2,25}/i;
			
			if(!val.match(usernameRE)) {
				validationError('Please enter a valid username [Letters, numbers _ and - only]');
				return false;
			}
			
			return true;
			break;
			
		case 'dollarAmount':
			var numberRE = /^[0-9\,]{1,10}((\.)?[0-9]{1,2})?/i;
			
			if(!val.match(numberRE)) {
				validationError('Please enter a valid dollar amount ('+n+') [Ex: 120000]');
				return false;
			}
			
			return true;
			break;
		
		case 'date':
			var dateRE = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}/i;
			
			if(!val.match(dateRE)) {
				validationError('Please enter a valid date ('+n+') [Ex: 01/01/2009]');
				return false;
			}
			
			return true;
			break;
				
		case 'email':
			var emailRE = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
			
			if(!val.match(emailRE)) {
				validationError('Please enter a valid e-mail address ('+n+')');
				return false;
			}
			
			return true;
			break;
			
		case 'zip':
			var zipRE = /^[0-9]{5}(\-[0-9]{4})?$/i;

			if(!val.match(zipRE)) {
				validationError('Please enter a valid zip code ('+n+') [Ex: 12345]');
				return false;
			}
			
			return true;
			break;
			
		case 'phone':
			var phoneRE = /^[0-9]{3}\-[0-9]{3}\-[0-9]{4}?$/i;
			
			if(!val.match(phoneRE)) {
				validationError('Please enter a valid phone # ('+n+') [Ex: 111-222-1234]');
				return false;
			}
			
			return true;
			break;
			
		case 'ssn':
			var ssnRE	= /^[0-9]{3} [0-9]{2} [0-9]{4}?$/i;
			var ssnRE2	= /^[0-9]{3}\-[0-9]{2}\-[0-9]{4}?$/i;
			var ssnRE3	= /^[0-9]{9}?$/i;
			
			if(!val.match(ssnRE) && !val.match(ssnRE2) && !val.match(ssnRE3)) {
				validationError('Please enter a valid SSN ('+n+') [Ex: 111 22 1234]');
				return false;
			}
			
			return true;
			break;
			
		case 'password':
			if(val.length < 6) {
				validationError('Please enter a valid password ('+n+') [Must be at least 6 characters]');
				return false;
			}
			
			return true;
			break;
		
		case 'passwordC':
			if(val != $('#f_password').val()) {
				validationError('Your passwords do not match!');
				return false;
			}
			
			return true;
			break;
				
		default:
			return true;
			break;
	}
}

function validationError(v) {
	alert(v);
}

function getVals(v) {
	var v = new String(v);
	return v.split('|*|');
}


/* !STATUS POSTING */
function postStatus() {
	var s = $('#status-txt').val();
	
	$.ajax({
		type:		"POST",
		url:		"http://modelingintl.com/includes/ajax/ajax.php",
		data:		{ func: 'postStatus', status: s },
		cache:		false,
		success:	function(val){ postStatus_cb(getVals(val)); }
	});
	
	return false;
}

function postStatus_cb(vars) {
	if(vars[0] == '0')
		alert(vars[1]);
	
	else {
		$('#status-txt').val('');
		$('#current-status').html(vars[1]+' <em>&nbsp;Posted 1 second ago</em>');
		//$('#current-status').effect("highlight", {}, 3000);
	}
}


/* !FRIEND REQUEST */
function friendRequest(u) {
	$.ajax({
		type:		"POST",
		url:		"http://modelingintl.com/includes/ajax/ajax.php",
		data:		{ func: 'friendRequest', uid: u },
		cache:		false,
		success:	function(val){ friendRequest_cb(getVals(val)); }
	});
	
	return false;
}

function friendRequest_cb(vars) {
	if(vars[0] == '1') {
		$('#fs'+vars[1]).attr('onclick', 'return false;');
		$('#fs'+vars[1]+' img').attr('src', 'http://modelingintl.com/images/btn-request-pending.gif');

	}
	
	alert(vars[2]);
}

function friendRequest2(u) {
	$.ajax({
		type:		"POST",
		url:		"http://modelingintl.com/includes/ajax/ajax.php",
		data:		{ func: 'friendRequest', uid: u },
		cache:		false,
		success:	function(val){ friendRequest2_cb(getVals(val)); }
	});
	
	return false;
}

function friendRequest2_cb(vars) {
	if(vars[0] == '1') {
		$('#friendRequest').attr('onclick', 'return false;');
		$('#friendRequest').attr('src', 'http://modelingintl.com/images/btn-request-pending.gif');

	}
	
	alert(vars[2]);
}




/* !STATI/COMMENTS */
function moreProfilePosts() {
	post_offset += post_inc;
	
	$.ajax({
		type:		"POST",
		url:		"http://modelingintl.com/includes/ajax/ajax.php",
		data:		{ func: 'moreProfilePosts', offset: post_offset, inc: post_inc, view: post_view },
		cache:		false,
		success:	function(val){ moreProfilePosts_cb(getVals(val)); }
	});
	
	return false;
}

function moreUserPosts(uid) {
	post_offset += post_inc;
	
	$.ajax({
		type:		"POST",
		url:		"http://modelingintl.com/includes/ajax/ajax.php",
		data:		{ func: 'moreUserPosts', userID: uid, offset: post_offset, inc: post_inc, view: post_view },
		cache:		false,
		success:	function(val){ moreProfilePosts_cb(getVals(val)); }
	});
	
	return false;
}

function moreProfilePosts_cb(vars) {
	if(!vars[0])
		alert(vars[1]);
	
	else {
		$('#status-window').append(vars[1]).css('display', 'none');
		$('#status-window').slideDown();
		
		if(post_total <= post_offset+post_inc)
			$('#view-more-bar').fadeOut();
	}
}

function viewProfileStati(type) {
	post_offset	= 0;
	
	$('#sel-'+post_view).attr('className', '');
	post_view	= type;
	
	$('#status-window').slideUp();
	$('#view-more-bar').fadeOut();
	$('#sel-'+post_view).attr('className', 'sel');
	
	$.ajax({
		type:		"POST",
		url:		"http://modelingintl.com/includes/ajax/ajax.php",
		data:		{ func: 'viewProfileStati', offset: post_offset, inc: post_inc, view: post_view },
		cache:		false,
		success:	function(val){ viewProfileStati_cb(getVals(val)); },
		error:		function(XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest) }

	});
	
	return false;
}

function viewProfileStati_cb(vars) {
	if(!vars[0]) {
		post_total = 0;	
		alert(vars[1]);
	
	} else {
		post_total = vars[2];
		
		$('#status-window').html(vars[1]);
		$('#status-window').slideDown();
		
		if(post_total > post_offset+post_inc)
			$('#view-more-bar').fadeIn();
	}
}


/* !FORUM THREAD FUNCTIONS */
function prevForumThread() {
	$('#forum-next').fadeIn();
	forum_offset -= 1;
	
	return viewForumThread();
}

function nextForumThread() {
	$('#forum-prev').fadeIn();
		
	forum_offset +=1;
	
	if(forum_offset+1 >= forum_num)
		$('#forum-next').fadeOut();
		
	return viewForumThread();
}

function viewForumThread() {
	if(forum_offset < 0)
		forum_offset = 0;
		
	if(forum_offset == 0)
		$('#forum-prev').fadeOut();
	
	$('#forum-entry').slideUp();
	
	$.ajax({
		type:		"POST",
		url:		"http://modelingintl.com/includes/ajax/ajax.php",
		data:		{ func: 'viewForumThread', offset: forum_offset },
		cache:		false,
		success:	function(val){ viewForumThread_cb(getVals(val)); }
	});
	
	return false;
}

function viewForumThread_cb(vars) {
	if(vars[0] == 0)
		alert(vars[1]);
	else {
		$('#forum-entry').html(vars[1]);
		$('#forum-entry').slideDown();
	}
}


/* !CASTING CALL FUNCTIONS */
function prevCastingCall() {
	$('#call-next').fadeIn();
	call_offset -= 1;
	
	return viewCastingCall();
}

function nextCastingCall() {
	$('#call-prev').fadeIn();
		
	call_offset +=1;
	
	if(call_offset+1 >= call_num)
		$('#call-next').fadeOut();
		
	return viewCastingCall();
}

function viewCastingCall() {
	if(call_offset < 0)
		call_offset = 0;
		
	if(call_offset == 0)
		$('#call-prev').fadeOut();
	
	$('#cc-entry').slideUp();
	
	$.ajax({
		type:		"POST",
		url:		"http://modelingintl.com/includes/ajax/ajax.php",
		data:		{ func: 'viewCastingCall', offset: call_offset },
		cache:		false,
		success:	function(val){ viewCastingCall_cb(getVals(val)); }
	});
	
	return false;
}

function viewCastingCall_cb(vars) {
	if(vars[0] == 0)
		alert(vars[1]);
	else {
		$('#cc-entry').html(vars[1]);
		$('#cc-entry').slideDown();
	}
}


/* !ARTICLES FUNCTIONS */
function prevArticles() {
	$('#articles-next').fadeIn();
	articles_offset -= 1;
	
	return viewArticles();
}

function nextArticles() {
	$('#articles-prev').fadeIn();
		
	articles_offset +=1;
	
	if(articles_offset+1 >= articles_num)
		$('#articles-next').fadeOut();
		
	return viewArticles();
}

function viewArticles() {
	if(articles_offset < 0)
		articles_offset = 0;
		
	if(articles_offset == 0)
		$('#articles-prev').fadeOut();
	
	$('#articles-entry').slideUp();
	
	$.ajax({
		type:		"POST",
		url:		"http://modelingintl.com/includes/ajax/ajax.php",
		data:		{ func: 'viewArticles', offset: articles_offset },
		cache:		false,
		success:	function(val){ viewArticles_cb(getVals(val)); }
	});
	
	return false;
}

function viewArticles_cb(vars) {
	if(vars[0] == 0)
		alert(vars[1]);
	else {
		$('#articles-entry').html(vars[1]);
		$('#articles-entry').slideDown();
	}
}


/* !QnA FUNCTIONS */
function prevQnA() {
	$('#qa-next').fadeIn();
	qna_offset -= 1;
	
	return viewQnA();
}

function nextQnA() {
	$('#qa-prev').fadeIn();
		
	qna_offset +=1;
	
	if(qna_offset+1 >= qna_num)
		$('#qa-next').fadeOut();
		
	return viewQnA();
}

function viewQnA() {
	if(qna_offset < 0)
		qna_offset = 0;
		
	if(qna_offset == 0)
		$('#qa-prev').fadeOut();
	
	$('#qa-entry').slideUp();
	
	$.ajax({
		type:		"POST",
		url:		"http://modelingintl.com/includes/ajax/ajax.php",
		data:		{ func: 'viewQnA', offset: qna_offset },
		cache:		false,
		success:	function(val){ viewQnA_cb(getVals(val)); }
	});
	
	return false;
}

function viewQnA_cb(vars) {
	if(vars[0] == 0)
		alert(vars[1]);
	else {
		$('#qa-entry').html(vars[1]);
		$('#qa-entry').slideDown();
	}
}

/* !TAB SWITCHING */
var currentTab = '';

function switchTab(tab) {
	$('#tab-'+currentTab).attr('className', '');
	$('#tab-'+tab).attr('className', 'sel');
	
	$('#content-'+currentTab).fadeOut(500, function() { $('#content-'+tab).fadeIn(500); } );
	
	currentTab = tab;
	
	return false;
}

/* !ADD PHOTO TO CONTEST */
function addPhotoToContest(cid, pid) {
	if(enteredToday < 3) {
		if(confirm('Are you sure you want to add this photo to this category?')) {
			$.ajax({
				type:		"POST",
				url:		"http://modelingintl.com/includes/ajax/ajax.php",
				data:		{ func: 'addPhotoToContest', contestID: cid, photoID: pid },
				cache:		false,
				success:	function(val){ addPhotoToContest_cb(getVals(val)); }
			});
		}
	} else
		alert('You have already entered the maximum number of photos in this category today');

	return false;
}

function addPhotoToContest_cb(vars) {
	if(vars[0] == 0)
		alert(vars[1]);
	else {
		enteredToday++; photosLeft--;
		$('#pic'+vars[1]).fadeOut('normal');
		
		document.location.href = contestURL+'add/';
		/*if(photosLeft <= 0) {
			$('#category-info .msg').html('<img src="/images/all-photos-entered.gif" alt="All your photos have been entered in this category!" />');
			$('#add-photos').slideUp('normal');
			$('#add-btn').fadeOut('fast');
		
		} else if(enteredToday >= 3) {
			$('#category-info .msg').html('<img src="/images/entries-max.gif" alt="Max entries have been met for this category!" />');
			$('#add-photos').slideUp('normal');
			$('#add-btn').fadeOut('fast');
		}*/
	}
}

/* !PHOTO VOTING */
function contestVote(cid, pid) {
	$.ajax({
		type:		"POST",
		url:		"http://modelingintl.com/includes/ajax/ajax.php",
		data:		{ func: 'contestVote', contestID: cid, photoID: pid },
		cache:		false,
		success:	function(val){ contestVote_cb(getVals(val)); }
	});
	
	return false;
}

function contestVote_cb(vars) {
	if(vars[0] == 0)
		alert(vars[1]);
	else {
		votesLeft--;
		
		if(!votesLeft)
			$('[id^="voting"]').slideUp();

		$('#voting'+vars[1]).html('<img src="/images/voted.gif" alt="Voted!" />');
		
		$('#vote-count').html(votesLeft);
	}
}

function contestVoteSingle(cid, pid) {
	$.ajax({
		type:		"POST",
		url:		"http://modelingintl.com/includes/ajax/ajax.php",
		data:		{ func: 'contestVote', contestID: cid, photoID: pid },
		cache:		false,
		success:	function(val){ contestVoteSingle_cb(getVals(val)); }
	});
	
	return false;
}

function contestVoteSingle_cb(vars) {
	if(vars[0] == 0)
		alert(vars[1]);
	else {
		votesLeft--;
		
		$('#votings').html('<img src="/images/voted.gif" alt="Voted!" />');
		$('#vote-count').html(votesLeft);
	}
}

/* !PHOTO REPORTING */
function reportPhoto(pid) {
	var r;
	
	if(confirm('Are you sure you want to report this photo for a violation of the terms of service?')) {
		if(r=prompt('Why does this photo violate the terms of service?')) {
			$.ajax({
				type:		"POST",
				url:		"http://modelingintl.com/includes/ajax/ajax.php",
				data:		{ func: 'reportPhoto', photoID: pid, reason: r },
				cache:		false,
				success:	function(val){ reportPhoto_cb(getVals(val)); }
			});
		}
	}
	
	return false;
}

function reportPhoto_cb(vars) {
	if(vars[0] == 0)
		alert(vars[1]);
	else {
		$('#reportPhotoL').removeAttr('onclick');
		$('#reportPhotoI').attr('src', '/images/btn-pending-review.gif');
	}
}

/* !PHOTO COMMENTS */
function photoComment() {
	var c = $('#comment').val();
	
	if(c != '') {
		$.ajax({
			type:		"POST",
			url:		"http://modelingintl.com/includes/ajax/ajax.php",
			data:		{ func: 'photoComment', photoID: pid, comment: c },
			cache:		false,
			success:	function(val){ photoComment_cb(getVals(val)); }
		});
	}
	
	return false;
}

function photoComment_cb(vars) {
	if(vars[0] == 0)
		alert(vars[1]);
	else
		window.location.reload();
}

function morePhotoComments(pid) {
	if(pg < pgs) {
		pg++;
		
		$.ajax({
			type:		"POST",
			url:		"http://modelingintl.com/includes/ajax/ajax.php",
			data:		{ func: 'morePhotoComments', photoID: pid, page: pg, perPage: 10 },
			cache:		false,
			success:	function(val){ morePhotoComments_cb(getVals(val), pg, pgs); }
		});
	}
	
	return false;
}

function morePhotoComments_cb(vars, page, pages) {
	if(vars[0] == 0)
		alert(vars[1]);
	else {
		if(page == pages)
			$('#photo-comments .more-btn').fadeOut();
		
		$('#comment-entries').append(vars[1]).slideDown();
	}
}


/* !QnA */
function moreAnswers(q) {
	pg++;
	
	$.ajax({
		type:		"POST",
		url:		"http://modelingintl.com/includes/ajax/ajax.php",
		data:		{ func: 'moreAnswers', page: pg, perPage: per_page, questionID: q },
		cache:		false,
		success:	function(val){ moreAnswers_cb(getVals(val)); }
	});
	
	return false;
}

function moreAnswers_cb(vars) {
	if(!vars[0])
		alert(vars[1]);
	
	else {
		$('#status-window').append(vars[1]).css('display', 'none');
		$('#status-window').slideDown();
		
		if(pg >= pgs)
			$('#full-block-comment .more-btn').fadeOut();
	}
}

function postAnswer(q) {
	var s = $('#status-txt').val();
	
	$.ajax({
		type:		"POST",
		url:		"http://modelingintl.com/includes/ajax/ajax.php",
		data:		{ func: 'postAnswer', answer: s, questionID: q },
		cache:		false,
		success:	function(val){ postAnswer_cb(getVals(val)); }
	});
	
	return false;
}

function postAnswer_cb(vars) {
	if(vars[0] == '0')
		alert(vars[1]);
	
	else {
		$('#status-txt').val('');
		$('#full-block-comment .directions').slideDown();
		//$('#full-block-comment .directions').effect("highlight", {}, 3000);
		
		setTimeout("$('#full-block-comment .directions').slideUp();", 10000);
	}
}

/*!COMMENT */
function postComment() {
	var s = $('#status-txt').val();
	
	$.ajax({
		type:		"POST",
		url:		"http://modelingintl.com/includes/ajax/ajax.php",
		data:		{ func: 'postComment', comment: s, userID: u },
		cache:		false,
		success:	function(val){ postComment_cb(getVals(val)); }
	});
	
	return false;
}

function postComment_cb(vars) {
	if(vars[0] == '0')
		alert(vars[1]);
	
	else {
		$('#status-txt').val('');
		$('#content-content .directions').slideDown();
		//$('#content-content .directions').effect("highlight", {}, 3000);
		
		setTimeout("document.location.href = document.location.href;", 5000);
	}
}

function deleteComment(c) {
	if(confirm('Are you sure you want to delete this comment?')) {
		$.ajax({
			type:		"POST",
			url:		"http://modelingintl.com/includes/ajax/ajax.php",
			data:		{ func: 'deleteComment', commentID: c },
			cache:		false,
			success:	function(val){ deleteComment_cb(getVals(val)); }
		});
	}
	
	return false;
}

function deleteComment_cb(vars) {
	if(vars[0] == '0')
		alert(vars[1]);
	
	else
		$('#s'+vars[1]).slideUp();
}

function deleteFriend(f) {
	if(confirm('Are you sure you want to delete this friend?')) {
		
		$.ajax({
			type:		"POST",
			url:		"http://modelingintl.com/includes/ajax/ajax.php",
			data:		{ func: 'deleteFriend', friendID: f },
			cache:		false,
			success:	function(val){ deleteFriend_cb(getVals(val)); }
		});
	}
	
	return false;
}

function deleteFriend_cb(vars) {
	if(vars[0] == '0')
		alert(vars[1]);
	else
		$('#f'+vars[1]).slideUp();
}

function acceptFriendRequest(f) {
	$.ajax({
		type:		"POST",
		url:		"http://modelingintl.com/includes/ajax/ajax.php",
		data:		{ func: 'acceptFriendRequest', friendID: f },
		cache:		false,
		success:	function(val){ deleteFriend_cb(getVals(val)); }
	});
	
	return false;
}

function ignoreFriendRequest(f) {
	$.ajax({
		type:		"POST",
		url:		"http://modelingintl.com/includes/ajax/ajax.php",
		data:		{ func: 'ignoreFriendRequest', friendID: f },
		cache:		false,
		success:	function(val){ deleteFriend_cb(getVals(val)); }
	});
	
	return false;
}

function removeFromContest(p, c) {
	if(confirm('Are you sure you want to remove this photo from voting? Any and all votes will be lost.')) {
		$.ajax({
			type:		"POST",
			url:		"http://modelingintl.com/includes/ajax/ajax.php",
			data:		{ func: 'removeFromContest', photoID: p, contestID: c },
			cache:		false,
			success:	function(val){ removeFromContest_cb(getVals(val)); }
		});
	}
		
	return false;
}

function removeFromContest_cb(vars) {
	if(vars[0] == '0')
		alert(vars[1]);
	else {
		alert('The photo has been removed from voting!');
		location.reload(true);
	}
}

function clearUserAvatar(u) {
	if(confirm('Are you sure you want to clear this user\'s avatar?')) {
		$.ajax({
			type:		"POST",
			url:		"http://modelingintl.com/includes/ajax/ajax.php",
			data:		{ func: 'clearUserAvatar', userID: u },
			cache:		false,
			success:	function(val){ clearUserAvatar_cb(getVals(val)); }
		});
	}
	
	return false;
}

function clearUserAvatar_cb(vars) {
	location.reload(true);
}