var cur_pane = 'tab0';

function set_defaults()
{ 
	x=document.getElementById('container').style.display;
	if(x!='none')
	{ 
		new Effect.BlindUp('container'); 
	} 
	
	//new Effect.Opacity('register', {from: 1.0, to: 1.0});
}

function image_scale_view(div_id)
{ 
	new Effect.Opacity(div_id, {from: 0.7, to: 1.3});
	//new Effect.Scale(div_id, 150);
}

function image_scale_reduce(div_id)
{ 
	new Effect.Opacity(div_id, {duration: 2.0, from: 0.7, to: 1.0});
	//new Effect.Scale(div_id, 66);
}

function dim_non_register_icons()
{ 
	document.getElementById('register_icon').style.backgroundPosition = "0 0px";
	document.getElementById('search_icon').style.backgroundPosition = "0 -197px";
	document.getElementById('login_icon').style.backgroundPosition = "0 -197px";
}
function dim_non_search_icons()
{ 
	document.getElementById('register_icon').style.backgroundPosition = "0 -197px";
	document.getElementById('search_icon').style.backgroundPosition = "0 0px";	
	document.getElementById('login_icon').style.backgroundPosition = "0 -197px";
}
function dim_non_login_icons()
{ 
	document.getElementById('register_icon').style.backgroundPosition = "0 -197px";
	document.getElementById('search_icon').style.backgroundPosition = "0 -197px";
	document.getElementById('login_icon').style.backgroundPosition = "0 0px";
}
function highlight_register_icon()
{ 
	document.getElementById('register_icon').style.backgroundPosition = "0 0px";
	document.getElementById('search_icon').style.backgroundPosition = "0 -150px";
	document.getElementById('login_icon').style.backgroundPosition = "0 -150px";
}
function highlight_search_icon()
{ 
	document.getElementById('register_icon').style.backgroundPosition = "0 -150px";
	document.getElementById('search_icon').style.backgroundPosition = "0 0px";	
	document.getElementById('login_icon').style.backgroundPosition = "0 -150px";
}
function highlight_login_icon()
{ 
	document.getElementById('register_icon').style.backgroundPosition = "0 -150px";
	document.getElementById('search_icon').style.backgroundPosition = "0 -150px";
	document.getElementById('login_icon').style.backgroundPosition = "0 0px";
}
function highlight_icons()
{
	document.getElementById('register_icon').style.backgroundPosition = "0 0px";
	document.getElementById('search_icon').style.backgroundPosition = "0 0px";
	document.getElementById('login_icon').style.backgroundPosition = "0 0px";
}

function pass_hint_rollover()
{
	document.getElementById('pass_hint').style.fontWeight = "bold";
	document.getElementById('pass_hint').style.color = "white";
}

function pass_hint_rollout()
{
	document.getElementById('pass_hint').style.fontWeight = "normal";
	document.getElementById('pass_hint').style.color = "white";
}


function verify_login_details()
{
	short_input_data = false;
	t = '';
	username_entered = document.getElementById('account_login').value;
	password_entered = document.getElementById('account_password').value;
	
	if ( username_entered == '' && password_entered == '' )
	{
		t = "Error: both username and password are blank";
		pre_login_response_parse(t);
		return;
	}
	else if (username_entered == '')
	{
		t = "Error: username is blank";
		pre_login_response_parse(t);
		return;
	}
	else if (password_entered == '')
	{
		t = "Error: password is blank";
		pre_login_response_parse(t);
		return;
	}
	
	if (username_entered.length < 6)
	{
		t = "Error: username is less than 6 characters<br />";
		short_input_data = true;
	}
	if (password_entered.length < 6)
	{
		t = t + "Error: password is less than 6 characters";
		short_input_data = true;
	}
	
	if (short_input_data == true)
	{
		pre_login_response_parse(t);
		return;
	}

	var handlerFunc = function(t) {
		login_response_parse(t);
	}

	var errFunc = function(t) {
		alert('Error ' + t.status + ' -- ' + t.statusText);
	}
	post_str = 'username=' + username_entered + '&password=' + password_entered;
	new Ajax.Request('global_scripts/database/login.php', {method:'post', parameters:post_str, onSuccess:handlerFunc, onFailure:errFunc});
}


function login_response_parse(t)
{
	//alert ('Hello '+t.responseText);
	if ( t.responseText == "\r\n" ) /* i.e. no response from php script */
		window.location = "http://www.clickinlove.com/dating_site/en/user_home.php";
	else	
		document.getElementById("login_error").innerHTML = t.responseText;
}

function pre_login_response_parse(t)
{
	document.getElementById("login_error").innerHTML = t;
}


function verify_register_details()
{
	yourgender_entered = document.register_form.yourgender_reg.value;
	yourage_entered = document.register_form.yourage_reg.value;
	yourstatus_entered = document.register_form.yourstatus_reg.value;
	yourchildren_entered = document.register_form.yourchildren_reg.value;
	youremail_entered = document.register_form.youremail_reg.value;
	yourcountryresidence_entered = document.register_form.yourcountryresidence_reg.value;
	yourcityresidence_entered = document.register_form.yourcityresidence_reg.value;
	yourusername_entered = document.register_form.yourusername_reg.value;
	yourpassword_entered = document.register_form.yourpassword_reg.value;
	yourpasswordconfirm_entered = document.register_form.yourpasswordconfirm_reg.value;
	mathguard_answer_entered = document.register_form.mathguard_answer.value;
	mathguard_code_entered = document.register_form.mathguard_code.value;

	var error_flag = false;
	
	if (yourcityresidence_entered == null || yourcityresidence_entered == "" || yourcityresidence_entered == " ")
	{
		document.getElementById("city_error").innerHTML = "*Please enter a city";
		error_flag = true;
	}
	else
		document.getElementById("city_error").innerHTML = "";
		
	if (yourusername_entered == null || yourusername_entered == "" || yourusername_entered == " ")
	{
		document.getElementById("username_error").innerHTML = "*Please enter a username";
		error_flag = true;
	}
	else
		document.getElementById("username_error").innerHTML = "";
		
	if (youremail_entered == null || youremail_entered == "" || youremail_entered == " ")
	{
		document.getElementById("email_error").innerHTML = "*Please enter an email address";
		error_flag = true;
	}
	else
		document.getElementById("email_error").innerHTML = "";
		
	if (yourpassword_entered == null || yourpassword_entered == "" || yourpassword_entered == " ")
	{
		document.getElementById("password_error").innerHTML = "*Please enter a password";
		error_flag = true;
	}
	else
		document.getElementById("password_error").innerHTML = "";
		
	if (yourpasswordconfirm_entered == null || yourpasswordconfirm_entered == "" || yourpasswordconfirm_entered == " ")
	{
		document.getElementById("password_confirm_error").innerHTML = "*Please confirm your password";
		error_flag = true;
	}
	else
		document.getElementById("password_confirm_error").innerHTML = "";
		
	if (yourusername_entered && yourusername_entered.length < 6)
	{
		document.getElementById("username_error").innerHTML = "*Username must be 6 or more chars";
		error_flag = true;
	}
	
	if (yourpassword_entered && yourpassword_entered.length < 6)
	{
		document.getElementById("password_error").innerHTML = "*Password must be 6 or more chars";
		error_flag = true;
	}
	
	if (yourpassword_entered && yourpasswordconfirm_entered)
	{	
		if (yourpassword_entered != yourpasswordconfirm_entered)
		{
			document.getElementById("password_confirm_error").innerHTML = "*Password confirmation is wrong";
			error_flag = true;
		}
	}
	
	if (youremail_entered && !validate_email(youremail_entered))
	{
		document.getElementById("email_error").innerHTML = "*Email is invalid";
		error_flag = true;
	}
		
	if (error_flag == true)
		return;

	var handlerFunc_reg = function(t) {
		register_response_parse(t);
	}

	var errFunc_reg = function(t) {
		alert('Error ' + t.status + ' -- ' + t.statusText);
	}
	post_str = 'yourgender_reg=' + yourgender_entered + '&yourage_reg=' + yourage_entered + '&yourstatus_reg=' + yourstatus_entered + '&yourchildren_reg=' + yourchildren_entered + '&youremail_reg=' + youremail_entered + '&yourcountryresidence_reg=' + yourcountryresidence_entered + '&yourcityresidence_reg=' + yourcityresidence_entered + '&yourusername_reg=' + yourusername_entered + '&yourpassword_reg=' + yourpassword_entered + '&mathcode_answer=' + mathguard_answer_entered + '&mathcode_code=' + mathguard_code_entered;
	//alert ('Username = ' + username_entered + ': Password = ' + password_entered);
	new Ajax.Request('global_scripts/database/register_verify.php', {method:'post', parameters:post_str, onSuccess:handlerFunc_reg, onFailure:errFunc_reg});
}


function register_response_parse(t)
{
	var str = t.responseText;
	if (str == null || str == "" || str == "<title>ch</title>")
		window.location = "http://www.clickinlove.com/dating_site/en/local_scripts/register_complete.php";
	else
	{
		if ( str.match("email") != null && str.match("username") == null )
			document.getElementById("email_error").innerHTML = "*" + str;
		else if ( str.match("username") != null && str.match("email") == null )
			document.getElementById("username_error").innerHTML = "*" + str;
		else if (str.match("username") != null && str.match("email") != null)
		{
			document.getElementById("email_error").innerHTML = "*Error: email already in use";
			document.getElementById("username_error").innerHTML = "*Error: username already in use";
		}
		else
			document.getElementById("code_error").innerHTML = str;
	}
}

function validate_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)
		return false

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		return false

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return false
	
	if (str.indexOf(at,(lat+1))!=-1)
		return false
		
	if (str.indexOf(dot,(lstr-1))!=-1)
		return false

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return false
	
	if (str.indexOf(dot,(lat+2))==-1)
		return false
	
	if (str.indexOf(" ")!=-1)
		return false

	return true;
}


function reload_images()
{
	var handlerFunc = function(transport, json) {
		reload_images_response_parse(transport, json);
	}

	var errFunc = function(transport, json) {
		alert('Error ' + transport.status + ' -- ' + transport.statusText);
	}
	
	cur_pane = getCookie("pane_var");
	if (cur_pane == 'tab0')
		post_str = 'gender=' + 'female';
	else
		post_str = 'gender=' + 'male';
		
	//new Ajax.Request('global_scripts/image_reload/reload.php', {method:'post', parameters:post_str, onSuccess:handlerFunc, onFailure:errFunc});
}

function reload_images_response_parse(transport, json)
{
	//alert (transport.responseText);
	//alert(json ? Object.inspect(json) : "no JSON object");
	//alert (json[0].country + '<br />' + json[1].path);
	var loop;
	var storage_id;
	
	if (cur_pane == 'tab0')
	{
		for ( loop=0;loop<24;loop=loop+1 )
		{
			var new_img = new Image( );
			random_no = rand(0,8);
			new_img.src = json[random_no].path;
		
			storage_id = 'image_storage0' + loop;
			var old_image = document.getElementById(storage_id);
			image_id = 'register0' + loop;
			document.getElementById(image_id).replaceChild(new_img,old_image);
			new_img.id = storage_id;
		}
	}
	else
	{
		for ( loop=0;loop<24;loop=loop+1 )
		{
			var new_img = new Image( );
			random_no = rand(0,8);
			new_img.src = json[random_no].path;
		
			storage_id = 'image_storage1' + loop;
			var old_image = document.getElementById(storage_id);
			image_id = 'register1' + loop;
			document.getElementById(image_id).replaceChild(new_img,old_image);
			new_img.id = storage_id;
		}
	}
}

function getCookie(Name)
{ //get cookie value
	var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
	if (document.cookie.match(re)) //if cookie found
		return document.cookie.match(re)[0].split("=")[1]; //return its value
	return "";
}


function rand( min, max ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Leslie Hoare
    // *     example 1: rand(1, 1);
    // *     returns 1: 1
 
    if( max ) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    } else {
        return Math.floor(Math.random() * (min + 1));
    }
}
       

function KeyCheck(e)
{
   var KeyID = (window.event) ? event.keyCode : e.keyCode;

   switch(KeyID)
   {
      case 37: case 39: 
      	//alert ('Shift has been pressed'); //document.Form1.KeyName.value = "Shift";
		reload_images();
      break; 
	  
	  default:
	  	//alert ('any key pressed');
	  break;
   }
}

function logout()
{
	var post_str;

	var handlerFunc = function(t) {
		logout_response_parse(t);
	}

	var errFunc = function(t) {
		alert('Error ' + t.status + ' -- ' + t.statusText);
	}
	
	new Ajax.Request('http://www.clickinlove.com/global_scripts/database/logout.php', {method:'post', parameters:post_str, onSuccess:handlerFunc, onFailure:errFunc});
}

function logout_response_parse(t)
{
	var str = t.responseText;
	if (str == null || str == "")
		window.location = "http://www.clickinlove.com/index.php";
}

function radio_button_save(public_id)
{
	var code=0;
	
	for (i=0;i<10;i++)
	{
		var id = "radio_button_" + i;
		if(document.getElementById(id).checked==true)
		{
			code=i;
			break;
		};
	}
	var post_str = 'radio_button=' + code + '&public_id=' + public_id;
	
	//alert("Radio button " + code + " has been selected.");
	
	var handlerFunc = function(t) {
		radio_button_save_response_parse(t);
	}

	var errFunc = function(t) {
		alert('Error ' + t.status + ' -- ' + t.statusText);
	}
	
	new Ajax.Request('http://www.clickinlove.com/global_scripts/radio_button_save.php', {method:'post', parameters:post_str, onSuccess:handlerFunc, onFailure:errFunc});
}

function radio_button_save_response_parse(t)
{
	var str = t.responseText;
	alert(str);
}
