
// This function gets an element's id, and reposition it's location in the screen according
// to the user's relative position in the screen
function relocateDiv(id)
{
	var p=document.getElementById(id); 
	var curr_top
	
	// IE family
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		curr_top = document.body.scrollTop;
		p.style.left = "0px";
	}
	// Netscape family (mozila)
	else //if(navigator.appName == "Netscape")
	{
		curr_top = window.pageYOffset;
		p.style.left = "175px";
	}
	
	p.style.top = curr_top+40;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// This function pops a window using javascript that enables the users comment on a website
function write_comment(id,url)
{
	// color the screen in transperant gray
	var p1 = document.getElementById("over_bg");
	p1.style.visibility = "visible";

	// open the popup
	var p2 = document.getElementById("dragable");
	if (url.length > 45)
	{
	url = url.substr(0,42) + "...";
	}
	p2.innerHTML = "<div id=\"header\" style=\"height:20px; background:#3B5998; text-align:left;padding-left:5px;color:white;font-size:14px;font-family:arial;font-weight:bold;float:left;width:425px; #width:420px;\">Write a comment about this site</div><div style=\"float:right;width:20px;height:20px;background:url(http://www.wisejump.com/images/close.gif); #cursor:hand;cursor:pointer;\" onmouseover=\"this.style.background='url(http://www.wisejump.com/images/close_over.gif)'\" onmouseout=\"this.style.background='url(http://www.wisejump.com/images/close.gif)'\" onclick=\"document.getElementById('over_bg').style.visibility = 'hidden'; document.getElementById('dragable').innerHTML='';\"></div><div id=\"comment_small_header\" style=\"text-align:left;margin-left:5px;margin-top:5px;\">What do you think about " + url + "?</div><form method=\"POST\" onsubmit=\"return ajax_website_comment_verify();\"><textarea name=\"comment\" id=\"comment\" style=\"float:left;margin-left:5px;\"></textarea><input id=\"id\" type=\"hidden\" name=\"id\" value=\"" + id + "\"><input type=\"image\" src=\"http://www.wisejump.com/images/submit.gif\" onmouseover=\"this.src='http://www.wisejump.com/images/submit_hover.gif'\" onmouseout=\"this.src='http://www.wisejump.com/images/submit.gif'\" value=\"Submit\" style=\"float:left;margin-left:5px;margin-top:15px;\"></form>";
	
	// make this div dragable
	//Drag.init(document.getElementById("dragable"));
	relocateDiv("dragable");
}
//////////////////////////////////////////////////////////////////////////////////////////////////
function ajax_website_comment_verify()
{
	if (document.getElementById('comment').value.length > 0)
	{
		// Save the message
		document.getElementById('comment_small_header').innerHTML="<img src=\"http://www.wisejump.com/images/loading.gif\" border=\"0\">Saving...";
		ajax_website_comment(document.getElementById('id').value,document.getElementById('comment').value);
		
		return false;
	}
	else
	{
		alert('Please write something...');
		return false;
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
function ajax_website_comment(id,message)
{
	var xmlHttp;
	var params;
	
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				// The client's browser does not support AJAX

			}
		}
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			// Hide the message div
			document.getElementById('over_bg').style.visibility = 'hidden'; 
			document.getElementById('dragable').innerHTML='';
			
			// Update the comments number on the screen
			var loc = document.getElementById(id).innerHTML.search(" ");
			if (loc != -1)
			{
				document.getElementById(id).innerHTML = (Number(document.getElementById(id).innerHTML.substring(0,loc))+1) + " Comments";
			}
		}
	}
	
	// set the page i am about to open on the server
	xmlHttp.open("POST","http://www.wisejump.com/scripts/save_website_comment.php",true);
	params = "id=" + id + "&message=" + message;
	
	// Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	
	xmlHttp.send(params);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
function open_wj_score_explenation()
{
	// color the screen in transperant gray
	var p1 = document.getElementById("over_bg");
	p1.style.visibility = "visible";

	// open the popup
	var p2 = document.getElementById("dragable");
	
	// fill the popup with content
	p2.innerHTML = "<div id=\"header\" style=\"height:20px; background:#3B5998; text-align:left;padding-left:5px;color:white;font-size:14px;font-family:arial;font-weight:bold;float:left;width:425px; #width:420px;\">About the WiseJump Score</div><div style=\"float:right;width:20px;height:20px;background:url(http://www.wisejump.com/images/close.gif); #cursor:hand;cursor:pointer;\" onmouseover=\"this.style.background='url(http://www.wisejump.com/images/close_over.gif)'\" onmouseout=\"this.style.background='url(http://www.wisejump.com/images/close.gif)'\" onclick=\"document.getElementById('over_bg').style.visibility = 'hidden'; document.getElementById('dragable').innerHTML='';\"></div><div style=\"text-align:left;margin-left:10px;margin-right:10px;\"><p style=\"margin-top:10px;\">The Wisejump Score is a very Sophisticated evaluation method for websites. It was invented and implemented by the Wisejump team, which have years of experience in internet Development.<p><p>Each and every site inside Wisejump's database is being evaluated constantly according to this sophisticated algorithm. This means that if a website was found today and given a low score, we will constantly check this site again to see the changes occured there, and reevalute our score, if needed.</p><p>You can read more about our algorithm at the about page.</p></div>";
	
	// make this div dragable
	Drag.init(document.getElementById("dragable"));
	relocateDiv("dragable");
}
///////////////////////////////////////////////////////////////////////////////////////////////////

function signup_focus_step1()
{
	document.getElementById('account_details').style.background = "#F7F7F7";
	document.getElementById('account_details').style.border = "1px solid #3B5998";
	document.getElementById('personal_details').style.background = "none";
	document.getElementById('personal_details').style.border = "0px";
	document.getElementById('fav_categories').style.background = "none";
	document.getElementById('fav_categories').style.border = "0px";
}
///////////////////////////////////////////////////////////////////////////////////////////////////
function signup_focus_step2()
{
	document.getElementById('account_details').style.background = "none";
	document.getElementById('account_details').style.border = "0px";
	document.getElementById('personal_details').style.background = "#F7F7F7";
	document.getElementById('personal_details').style.border = "1px solid #3B5998";
	document.getElementById('fav_categories').style.background = "none";
	document.getElementById('fav_categories').style.border = "0px";
}
function signup_focus_step3()
{
	document.getElementById('account_details').style.background = "none";
	document.getElementById('account_details').style.border = "0px";
	document.getElementById('personal_details').style.background = "none";
	document.getElementById('personal_details').style.border = "0px";
	document.getElementById('fav_categories').style.background = "#F7F7F7";
	document.getElementById('fav_categories').style.border = "1px solid #3B5998";
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// This function checks if a desierd username is free or not
function signup_verify_username()
{
	var username_obj;
	username_obj = document.getElementById('user_name_lbl');
	
	if (document.getElementById('user_name').value != "")
	{
		// print a "loading" image on the screen
		username_obj.innerHTML = "<img src=\"http://www.wisejump.com/images/loading.gif\" border=\"0\">";
		
		ajax_verify_username(document.getElementById('user_name').value);
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// This function checks if a desierd username is free or not using AJAX
function ajax_verify_username(user_name)
{
	var xmlHttp;
	var params;
	
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				// The client's browser does not support AJAX

			}
		}
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			var username_obj;
			username_obj = document.getElementById('user_name_lbl');
			
			// clear the loading image
			username_obj.innerHTML = "";
			
			// check for the server's response
			if (xmlHttp.responseText.search('good') != -1)
			{
				// username is taken
				username_obj.innerHTML = "This username is taken";
				username_obj.style.color = "red";
			}
			else
			{
				// username is free
				username_obj.innerHTML = "This username is free";
				username_obj.style.color = "blue";
			}
		}
	}
	
	// set the page i am about to open on the server
	xmlHttp.open("POST","scripts/check_username.php",true);
	params = "username=" + user_name;
	
	// Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	
	xmlHttp.send(params);

}
///////////////////////////////////////////////////////////////////////////////////////////////////
function check_password()
{
	var password_obj, pass_inp;
	password_obj = document.getElementById('password_lbl');
	pass_inp = document.signup_form.password;
	
	// Check if the password's length is at least 6 characters
	if (pass_inp.value.length < 5)
	{
		password_obj.innerHTML = "Must be at least 6 characters";
	}
	else
	{
		password_obj.innerHTML = "";
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
function check_re_password()
{
	var password_obj, pass_inp;
	password_obj = document.getElementById('re_password_lbl');
	re_pass_inp = document.signup_form.re_password;
	pass_inp = document.signup_form.password;
	
	// Check if the password equals the re typed password
	if (pass_inp.value != re_pass_inp.value)
	{
		password_obj.innerHTML = "Re-typed password does not match the password";
	}
	else
	{
		password_obj.innerHTML = "";
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// This function validates (client-side) that the form was filled good
function validate_signup_form()
{
	// make sure a username was chosen
	if (document.signup_form.user_name.value.length == 0)
	{
		document.signup_form.user_name.focus();
		document.getElementById('user_name_lbl').style.color = "red";
		document.getElementById('user_name_lbl').innerHTML = "Please choose a user name";
		return false;
	}
	// make sure a password was chosen
	else if (document.signup_form.password.value.length == 0)
	{
		document.signup_form.password.focus();
		document.getElementById('password_lbl').style.color = "red";
		document.getElementById('password_lbl').innerHTML = "Please choose a password";
		return false;
	}
	// make sure a VALID password was chosen
	else if (document.signup_form.password.value.length < 6)
	{
		document.signup_form.password.focus();
		document.getElementById('password_lbl').style.color = "red";
		document.getElementById('password_lbl').innerHTML = "Longer than 5 characters...";
		return false;
	}
	// make sure the password was retyped correctly
	else if (document.signup_form.password.value != document.signup_form.re_password.value)
	{
		document.signup_form.re_password.focus();
		document.signup_form.re_password.value = '';
		document.getElementById('re_password_lbl').style.color = "red";
		document.getElementById('re_password_lbl').innerHTML = "Retype the password again";
		return false;
	}
	// make sure the name field was filled
	else if (document.signup_form.name.value.length == 0)
	{
		document.signup_form.name.focus();
		document.getElementById('name_lbl').style.color = "red";
		document.getElementById('name_lbl').innerHTML = "Fill in your name";
		return false;
	}
	// make sure the last name field was filled
	else if (document.signup_form.last_name.value.length == 0)
	{
		document.signup_form.last_name.focus();
		document.getElementById('last_name_lbl').style.color = "red";
		document.getElementById('last_name_lbl').innerHTML = "Fill in your last name";
		return false;
	}
	// make sure the email field was filled
	else if (document.signup_form.email.value.length == 0)
	{
		document.signup_form.email.focus();
		document.getElementById('email_lbl').style.color = "red";
		document.getElementById('email_lbl').innerHTML = "Fill in your Email adress";
		return false;
	}
	// make sure the email is valid
	else if (!validate_email(document.signup_form.email))
	{
		document.signup_form.email.focus();
		document.getElementById('email_lbl').style.color = "red";
		document.getElementById('email_lbl').innerHTML = "Your Email adress is invalid";
		return false;
	}
	else
	{
		return true;
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
// This function validates an email adress
function validate_email(field)
{
	with (field)
	{
		apos=value.indexOf("@")
		dotpos=value.lastIndexOf(".")
		if (apos<1||dotpos-apos<2) 
  		{
  			return false
  		}
		else 
		{
			return true
		}
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
// perform actions (focus,notification), when the user_name field in the signup form gets focus
function signup_onfocus_actions()
{
	// make the step1 div highlighted
	signup_focus_step1();
	
	if (document.getElementById('user_name_lbl').innerHTML != "Please choose a user name")
	{ 
		document.getElementById('user_name_lbl').innerHTML='';
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
// verify the websites submittion form
function validate_submit_link_form()
{
	// initiate all error messages to be empty
	document.getElementById('url_lbl').innerHTML = "";
	document.getElementById('title_lbl').innerHTML = "";
	document.getElementById('tags_lbl').innerHTML = "";
	document.getElementById('description_lbl').innerHTML = "";
	
	// make sure the url field was filled
	if (document.submit_link_form.url.value.length < 12 || document.submit_link_form.url.value.search('http://') == -1)
	{
		document.submit_link_form.url.focus();
		document.getElementById('url_lbl').style.color = "red";
		document.getElementById('url_lbl').innerHTML = "Invalid URL";
		return false;
	}
	// make sure the title field was filled
	else if (document.submit_link_form.title.value.length == 0)
	{
		document.submit_link_form.title.focus();
		document.getElementById('title_lbl').style.color = "red";
		document.getElementById('title_lbl').innerHTML = "Write a title";
		return false;
	}
	// make sure the title field was filled
	else if (document.submit_link_form.title.value.length > 100)
	{
		document.submit_link_form.title.focus();
		document.getElementById('title_lbl').style.color = "red";
		document.getElementById('title_lbl').innerHTML = "Title is too long";
		return false;
	}
	// make sure the tags field was filled
	else if (document.submit_link_form.tags.value.length == 0)
	{
		document.submit_link_form.tags.focus();
		document.getElementById('tags_lbl').style.color = "red";
		document.getElementById('tags_lbl').innerHTML = "Tag the site";
		return false;
	}
	// make sure the description field was filled
	else if (document.submit_link_form.description.value.length == 0)
	{
		document.submit_link_form.description.focus();
		document.getElementById('description_lbl').style.color = "red";
		document.submit_link_form.description.value = "Write a description";
		return false;
	}
	// make sure the description field was filled
	else if (document.submit_link_form.description.value.length > 300)
	{
		document.submit_link_form.description.focus();
		document.getElementById('description_lbl').style.color = "red";
		document.getElementById('description_lbl').innerHTML = "Long";
		return false;
	}
	else
	{
		return true;
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
function validate_update_details_form()
{
	// make sure the name field was filled
	if (document.signup_form.name.value.length == 0)
	{
		document.signup_form.name.focus();
		document.getElementById('name_lbl').style.color = "red";
		document.getElementById('name_lbl').innerHTML = "Fill in your name";
		return false;
	}
	// make sure the last name field was filled
	else if (document.signup_form.last_name.value.length == 0)
	{
		document.signup_form.last_name.focus();
		document.getElementById('last_name_lbl').style.color = "red";
		document.getElementById('last_name_lbl').innerHTML = "Fill in your last name";
		return false;
	}
	// make sure the email field was filled
	else if (document.signup_form.email.value.length == 0)
	{
		document.signup_form.email.focus();
		document.getElementById('email_lbl').style.color = "red";
		document.getElementById('email_lbl').innerHTML = "Fill in your Email adress";
		return false;
	}
	// make sure the email is valid
	else if (!validate_email(document.signup_form.email))
	{
		document.signup_form.email.focus();
		document.getElementById('email_lbl').style.color = "red";
		document.getElementById('email_lbl').innerHTML = "Your Email adress is invalid";
		return false;
	}
	else
	{
		return true;
	}
}