

// ------------------ REGISTRATION FORM SCRIPTS ------------------------

/**
*	var ErrorCode
*	Contains error messages for all errors on the website
*	@example	ErrorCode.username[2]
*/

var ErrorCode = {
	// Username
	'username':new Array(
		'Username not available',
		'Username should be atleast 4 characters long',
		'Username cannot be more than 12 characters',
		'Username cannot contain the space character',
		'Username can contain only alphanumeric characters'
	),

	// Password
	'password':new Array(
		'Passwords do not match',
		'Password should be atleast 6 characters long'
	),

	// E-mail
	'email':new Array(
		'Invalid e-mail address'
	),

	// Birthdate
	'birthdate':new Array(
		'Please select your birthdate',
		'Invalid birthdate'
	),

	// Captcha
	'captcha':new Array(
		'The characters you typed did not match'
	),

	// TOS
	'tos':new Array(
		'Please check the Terms of Service checkbox'
	)

};


/**
*
*	function displayErrorMsg
*   Displays errors messages
*
*	@param	field	- Which form field
*	@param	msg		- Error message
*	@return	void
*	@vars	ErrorCode
*	@call	none
*
*/

function displayErrorMsg(field, msg) {
	$(field).innerHTML = msg;
}



/**
*
*	function checkAvailability
*	Check fo the availability of a username
*
*/

function checkAvailability() {
	var username = $('regForm').reg_username.value;
	new Ajax.Request('/register/available/' + encodeURIComponent(username), {
	  method: 'get',
	  onSuccess: function(transport) {
		var r =transport.responseText;
		$('reg_username_err').childNodes[1].className = 'clr-red';
		if (r == 'unique') { $('reg_username_err').childNodes[1].innerHTML = username+' is not Available';	}
		else if (r == 'alphanum') { $('reg_username_err').childNodes[1].innerHTML = 'Only alphanumeric characters, please.'; }
		else if (r == 'min') { $('reg_username_err').childNodes[1].innerHTML = 'Username should be atleast four characters.'; }
		else if (r == 'required') { $('reg_username_err').childNodes[1].innerHTML = 'Please enter a username.'; }
		else {
			$('reg_username_err').childNodes[1].className = 'clr-green';
			$('reg_username_err').childNodes[1].innerHTML =  username+' is Available';
		}
	  }
	});
}



/**
*
*
*
*/

function login() {
	//var p = $('loginForm').dwp.value;
	//$('loginForm').dwp.value = hex_sha1(p);

	return true;
}






// VOTE

function vote(type, id) {	

	var params = 'id=' + id;
	new Ajax.Request('/vote/'+type, {
	  method: 'post',
	  parameters: params,
	  onSuccess: function(x) {
		var msg = x.responseText;
		if (msg == '0:0') { alert('Please log in to vote'); }
		else if (msg == 'f:0') { alert('You have used up your votes'); }
		else if (msg == 'f:1') { alert('You have already voted this item'); }
		else if (msg == 'f:2') { alert('You have already trashed this itemr'); }
		else {
			document.location = document.location;
		}
	  }
	});
}

// TRASH

function trash(type, id) {	

	var params = 'id=' + id;
	new Ajax.Request('/trash/'+type, {
	  method: 'post',
	  parameters: params,
	  onSuccess: function(x) {
		var msg = x.responseText;
		if (msg == '0:0') { alert('Please log in to vote'); }
		else if (msg == 'f:0') { alert('You have used up your votes'); }
		else if (msg == 'f:1') { alert('You have already voted this item'); }
		else if (msg == 'f:2') { alert('You have already trashed this itemr'); }
		else {
			document.location = document.location;
		}
	  }
	});
}


// LIST QUESTIONS

function list(type) {
	
	switch (type) {
		
		case 'new':
			document.location = '/questions/n';
			break;
		
		case 'pending':
			document.location = '/questions/p';
			break;

		case 'confirmed':
			document.location = '/questions/c';
			break;
		case 'disputed':
			document.location = '/questions/d';
			break;

	}
}

