/*
* USER.JS
*
* Provides utility functions for the display of user-related stuff.
*
* sep 15 2006, MA Hartman: created the file.
*/

function show_star() {
	if(document.getElementById('star')) {
		document.getElementById('star').style.display = 'block';
	}
}
function hide_star() {
	if(document.getElementById('star')) {
		document.getElementById('star').style.display = 'none';
	}
}

/*
* Make sure fields are not empty and compute SHA-1 hash of password.
*/
function process_backend_login()
{
	var form = document.forms[0];

	// Compute the SHA-1 hash of the password and clear the password field:
	form.hash.value = hex_sha1(form.password.value);
	form.password.value = '';

	return false;
}

/*****************************************************************************
* eval_login(sid)
*
* Evaluate the login form: check username and password using AJAX call
*/
function eval_login(sid)
{	
	var form = document.login_form;
	var warn = document.getElementById('small_form_warning');
	show_star();

	// Make sure the user has entered both a username and password:
	if (color_required() == false) {
		warn.style.display = 'block';
		warn.innerHTML = PLEASE_PW_USERNAME;
		hide_star();
		return false;
	}

	/** AjaxAgent call **/
	var username = form.username.value;
	var hash = form.password.value;
	form.hash.value = hash;
	var url = "ajax/user.php?task=check_login&csid="+sid+"&username="+username+"&hash="+hash+"&entropy="+Math.random();
	var ajaxCallResult = agent.call(url,'handle_check_login','','');
	
	if (ajaxCallResult == '0') {
		warn.style.display = 'block';
		warn.innerHTML = LOGIN_FAILED;
		hide_star();
	}
	else {
		form.submit();
	}
	return false;
}
/*****************************************************************************/



/*
* Check the account form for all the required fields.
* Also check if the username does not already exist in the DB (if new account is created.
*/
function check_account_form(sid)
{
	var form = document.account_form;
	var warn = document.getElementById('small_form_warning');
	warn.style.display = 'none';
	var error = false;
	show_star();


	// Test the required fields:
	if (!color_required() ) {
		warn.style.display = 'block';
		warn.innerHTML = REQ_FIELDS_EMPTY;
		hide_star();
		error = true;
	}
	if(!check_email(form.username.value)) {
		warn.style.display = 'block';
		warn.innerHTML = INVALID_MAIL;
		error = true;
	}


	if(error) {
		warn.style.display = 'block';
		return false;
	}
	// If a new password was entered, check if they're the same:
	if(document.getElementById('pass_01').style.display != 'none')
	{
		if (form.password.value != '') {
			if (form.password.value == form.confirm_password.value) {
				form.hash.value = hex_sha1(form.password.value);
				form.password.value = '';
				form.confirm_password.value = '';
			}
			else {
				//form.password.value = '';
				form.password.style.backgroundColor = '#ffbbaa';
				form.confirm_password.value = '';
				form.confirm_password.focus();
				form.confirm_password.style.backgroundColor = '#ffbbaa';
				warn.style.display = 'block';
				warn.innerHTML = NO_PW_MATCH;
				hide_star();
				return false;
			}
		}
	}

	// All information is set, make AJAX call to check for name availability if new account:
	var username = document.getElementById('username').value;

	if (form.registered.value == '0') {
		xmlHttp = GetXmlHttpObject();
		if (xmlHttp != null) {
			var url = "ajax/user.php?task=check_username&csid="+sid+"&username="+username+"&entropy="+Math.random();

			xmlHttp.onreadystatechange = function()
			{
				if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
					if (xmlHttp.responseText == '1') {
						var url = "ajax/user.php?task=reset_password&username="+username+"&entropy="+Math.random();
						var message = ACCOUNT_DETAILS_SENT;
						warn.innerHTML = MAIL_ALREADY_IN_USE;
						warn.style.display = 'block';
						hide_star();
					}
					else {
						form.submit();
					}
				}
			}
			xmlHttp.open("GET", url, true);
			xmlHttp.send(null);
		}
		hide_star();
		return false;
	}
	else {
		hide_star();
		form.submit();
		return true;
	}
}

function reset_password(){
	var u_field = document.getElementById('username');
	var username = u_field.value;
	if(u_field.getAttribute('sended') == 'y') {
		alert(ACCOUNT_DETAILS_SENT);
		return false;
	}
	if(username==''){
		alert(ENTER_USERNAME);
		document.getElementById('username').focus();
		return false;
	}
	u_field.setAttribute('sended', 'y');
	var warn = document.getElementById('small_form_warning');
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp != null) {
		var url = "ajax/user.php?task=reset_password&username="+username+"&entropy="+Math.random();

		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
				//warn.innerHTML = xmlHttp.responseText + '!';
				alert(xmlHttp.responseText);
				return false;
			}
		}
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
	return false;
}

function do_login() {
	var username = document.getElementById('login_username').value;
	var password = document.getElementById('login_password').value;
	var remember = document.getElementById('remeber_me');
	var rem = 'false'
	if(remember.checked) {
		rem = 'true';
	}

	if((username != '' && password != '') && (username != 'username' && password != 'password')) {
		window.location="./index.php?task=login&username="+username+"&hash="+hex_sha1(password)+"&remember="+rem;
	}
	else {}
}
function do_login_by_enter(code) {
	if(code == 13) {
		var username = document.getElementById('login_username').value;
		var password = document.getElementById('login_password').value;

		if((username != '' && password != '') && (username != 'username' && password != 'password')) {
			window.location="./index.php?task=login&username="+username+"&hash="+hex_sha1(password);
		}
	}
	else {}
}
function do_logout() {
	window.location="./index.php?task=logout";
}

var clear_field_ar = new Array();

function clear_field(element, inner) {
	if(element.value == inner) {
		element.value = '';
	}
	return;
}

function select_button() {
	return document.getElementById('login_button').focus();
}

var drop_el = function (el) {
	var theDiv = document.getElementById(el + '_div');
	var el = document.getElementById(el);

	if(el.style.display == 'none') {
		if (navigator.appName == "Microsoft Internet Explorer") {
			el.style.display = 'block';
		}
		else {
			el.style.display = 'table-row';
		}

		theDiv.className = 'required';
	}
	else {
		el.style.display = 'none';
		theDiv.className = 'optional';
	}
}

var key2function = function (func, key) {
	if(window.keyCodeVar == key)
	func();
}

window.keyCodeVar = 0;

var returnKeycode = function (e) {
	if (window.event)
	window.keyCodeVar = window.event.keyCode;
	else if (e)
	window.keyCodeVar = e.which;
}

document.onkeydown = returnKeycode;

/*
* Check the account form for all the required fields.
* Also check if the username does not already exist in the DB (if new account is created.
*/
function check_contact_form(uid)
{
	var form = document.account_form;
	var warn = document.getElementById('small_form_warning');
	warn.style.display = 'none';
	var error = false;
	show_star();


	// Test the required fields:
	if (!color_required() ) {
		warn.style.display = 'block';
		warn.innerHTML = REQ_FIELDS_EMPTY;
		hide_star();
		error = true;
	}


	if(!check_email(document.getElementById('contact_mail').value)) {
		warn.style.display = 'block';
		warn.innerHTML = INVALID_MAIL;
		error = true;
	}

	if(error) {
		warn.style.display = 'block';
		hide_star();
		return false;
	}

		var name = document.getElementById('fullname').value;
		var email = document.getElementById('contact_mail').value;
		var type = document.getElementById('type_response').value;
		var info = document.getElementById('info').value;

		info = info.replace(/</g, '<-');
		info = info.replace(/>/g, '->');
		info = info.replace(/\n/g, '<br />');
		info = info.replace(/'/g, '`');
		//info = info.replace(/"/g, '`');

		var url = "../ajax/user.php?task=send_form&uid="+uid+"&name="+name+"&email="+email+"&type="+type+"&info="+info;

//	var ajaxCallResult = agent.call(url,'handle_send_form','','');
//	alert('result'+ajaxCallResult);

	// All information is set, make AJAX call to send info
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp != null) {


		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {

				if (xmlHttp.responseText == '1') {
					warn.style.display = 'block';
					warn.innerHTML = ERROR;
				}
				else {
					var msg = xmlHttp.responseText;
					msg = msg.replace("<name>", name);

					document.getElementById('contact_form').innerHTML = msg;
				}

				hide_star();
			}
		}
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
	
	return false;
}

var makeActive = function (el, elButton) {
	el.style.borderColor = '#000';
	el.style.color = '#000';
	if(elButton) {
		document.getElementById(elButton).style.borderColor = '#7a7a7a';
		document.getElementById(elButton).style.color = '#7d7d7d';
	}
}

var nonActive = function (el, elButton) {
	el.style.borderColor = '#8B8B8B';
	el.style.color = '#8d8d8d';
	if(elButton) {
		document.getElementById(elButton).style.borderColor = '#C0C0C0';
		document.getElementById(elButton).style.color = '#9F9C9C';
	}
}
/*
function checkEmail (email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		//return 'eee';
		return false;
	}
	else {
		//return 'ja';
		return true;
	}
}

function check_email (email) {

	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (email.match(filter))
		return true;
	else
		return false;
}
*/
function check_email(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    //alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}
function bogus()
{
	if (document.getElementById('small_form_warning').innerHTML != LOGIN_FAILED) {
		document.getElementById('small_form_warning').innerHTML = ERROR;
		hide_star();
	}
}

function bogus2()
{
	if (document.getElementById('small_form_warning').innerHTML != USERNAME_TAKEN) {
		document.getElementById('small_form_warning').innerHTML = ERROR;
		hide_star();
	}
}

function checkAdvancedSearch() {
	var searchPrice = document.getElementById('s_ad_price').value;
	var searchCurrency = document.getElementById('s_ad_currency').value;
	if(searchPrice != "" && searchCurrency == "") {
		alert(NO_CURRENCY_SELECTED);
		return false;
	}
	else {
		return true;
	}
}