// JavaScript Document
function groupUpdate(url,groupid)
{
	document.jitter.action=url+"mygroupupdate/"+groupid;
	document.jitter.submit();
}
function disappearSearchtext() {
	searchtext = document.getElementById('edit-Keywords').value;
	if(searchtext == 'Search' || searchtext == 'Please enter the search text')
		document.getElementById('edit-Keywords').value = '';
}

function regainSearchtext() {
	if(document.getElementById('edit-Keywords').value == '')
		document.getElementById('edit-Keywords').value = 'Search';
}

function form_submit(url) {
 	document.jitter.action=url+"user";
	document.jitter.submit();
}

// Function to logout for jitter server
function logout(url) {
	document.jitter.action= Drupal.settings.jitterserver+"/jitter_logout.php";
	document.jitter.submit();
}

// Function to search amazon products
function amazonSubmit(url) {
	searchtext = document.getElementById('edit-Keywords').value;	
	if(searchtext == 'Search' || searchtext == 'Please enter the search text') {
		document.getElementById("edit-Keywords").style.border = "solid #FF0000 1px";
		document.getElementById('edit-Keywords').value = common_search_text;
		return false;
	}
	else {
		if ($("#edit-SearchIndex").val() == 'Community') {
			  var key = $.trim($("#edit-Keywords").val());
			  var url = location.protocol +"//"+ location.host + Drupal.settings.basePath +"search/node/"+key;
			  window.location = url;
			  return false;
		}
		else if ($("#edit-SearchIndex").val() == 'User') {
			  var key = $.trim($("#edit-Keywords").val());
			  var url = location.protocol +"//"+ location.host + Drupal.settings.basePath +"search/user/"+key;
			  window.location = url;
			  return false;
		}
		else {
			document.amazonsearch.action=url+"amazon/search";
			document.amazonsearch.submit();
		}
		
	}	
}

// Function to display the popup forgot password
function showForgotpassword() {	
	if(document.getElementById('popup_window').style.display == 'none') {
	  document.getElementById('popup_window').style.display = 'block';
	  document.getElementById('forgotMsg').innerHTML = '';
	  document.getElementById("forgot_message").style.display = 'none';		
	  document.getElementById("editname").focus();
	}
}

// Function to display the popup forgot password
function closeForgotpassword() {	
	if(document.getElementById('popup_window').style.display == 'block') {
		document.getElementById('popup_window').style.display = 'none';
	}
}

// Function to submit password
function password_submit() {
	document.getElementById("popupmessage").style.display = 'none';
	document.getElementById("spinner").style.display = 'none';	
	pwd = document.getElementById("editname").value;	
	if(pwd.charAt(0)==' ') {
		document.getElementById("forgot_message").style.display = 'block';	
		document.getElementById('forgotMsg').innerHTML = forgotpwd_error_spaces_mail;		
		document.getElementById('editname').focus();
		return false;
	}
	else if(pwd == "") {
		document.getElementById("forgot_message").style.display = 'block';
		document.getElementById('forgotMsg').innerHTML = user_register_error_empty_mail;	
		document.getElementById('editname').focus();
		return false;
	}
	else if(!validate_Email(pwd, 'forgot')) {
		return false;
	}
	document.getElementById("forgot_message").style.display = 'none';	
	passwordinfo(pwd);
}

// Function to retainText in textbox
function retain_Text(val) {
	var id = document.getElementById(val);	
	switch (val){
		case "edit-name":
			if(id.value == "")
				id.value = "Email";
		break;
		/*case "edit-pass":
			if(id.value == "")
				id.value = "Password";
		break;*/
	}
}

// Function to initialise Textbox
function show_Text(val) {
	var id = document.getElementById(val);	
	switch (val){
		case "edit-name":
			if(id.value == "Email"){
				id.value = "";
				id.focus();
			}
		break;
		case "edit-pass":
			if(id.value == "Password") {
				id.value = "";
				id.focus();
			}
		break;
	}
}

// Function to validate email
function validate_Email(mail, status) {		
	if(!/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test(mail)){
		if(status == 'login') {
			document.getElementById('script_message').style.display = 'block';
			document.getElementById('scriptMessage').innerHTML = user_register_error_mail_valid;	
			document.getElementById('edit-name').focus();
			return false;
		}
		else {
			document.getElementById("forgot_message").style.display = 'block';
			document.getElementById('forgotMsg').innerHTML = user_register_error_mail_valid;
			document.getElementById('editname').focus();			
			return false;
		}
	}	
	return true;
}

// Function to validate email
function validateEmail(mail) {	
	if(mail == 'Email') {
		document.getElementById('script_message').style.display = 'block';
		document.getElementById('scriptMessage').innerHTML = user_register_error_mail;		
		document.getElementById('edit-name').focus();
		return false;
	}
	else if(mail == '') {		
		document.getElementById('script_message').style.display = 'block';
		document.getElementById('scriptMessage').innerHTML = user_register_error_empty_mail;		
		document.getElementById('edit-name').focus();
		return false;
	}
	else if(mail.charAt(0)==' ') {
		document.getElementById('script_message').style.display = 'block';
		document.getElementById('scriptMessage').innerHTML = user_register_error_spaces_mail;			
		document.getElementById('edit-name').focus();
		return false;
	}
	else if(!validate_Email(mail, 'login')) {
		return false;
	}
	return true;
}

// Function to validate password
function validatePassword(pwd) {	
	re = /\s/;	
	if(pwd == 'Password') {
		document.getElementById('script_message').style.display = 'block';
		document.getElementById('scriptMessage').innerHTML = user_register_error_password;		
		document.getElementById('edit-pass').focus();
		return false;
	}	
	else if(pwd == '') {		
		document.getElementById('script_message').style.display = 'block';
		document.getElementById('scriptMessage').innerHTML = user_register_error_empty_password;		
		document.getElementById('edit-pass').focus();
		return false;
	}
	else if(pwd.match(re) != null) {	
		document.getElementById('script_message').style.display = 'block';
		document.getElementById('scriptMessage').innerHTML = login_incorrect_pwd;
		document.getElementById('edit-pass').value = "";
		document.getElementById('edit-pass').focus();
		return false; 
	}
	else if(pwd.charAt(0)==' ') {
		document.getElementById('script_message').style.display = 'block';
		document.getElementById('scriptMessage').innerHTML = user_register_error_spaces_password;
		document.getElementById('edit-pass').value = "";
		document.getElementById('edit-pass').focus();
		return false;
	}		
	return true;	
}

var base_url;
// Function to login
function login_submit(b_url) {
	base_url = b_url;	
	document.getElementById('loginmsg').style.display = 'none';
	document.getElementById('script_message').style.display = 'none';
	document.getElementById("login_msg").style.display = 'none';
	mail = document.getElementById("edit-name").value;
	pwd = document.getElementById("edit-pass").value;
	
	if(!validateEmail(mail)) {			
		return false;
	}	
	else if(!validatePassword(pwd)) {
		return false;
	}		
	if(!logininfo(base_url, mail, pwd))	{					
		return false;
	}
	else {		
		return true;
	}
}

// Function to implement Ajax for User login
function logininfo(base_url, mail, pwd) {
	xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert ("Your browser does not support AJAX!");
			return;
		} 
	var url=base_url+"userlogin";
	url=url+"?mail="+mail+"&pass="+pwd;	
	xmlHttp.onreadystatechange=hidden_Changed;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

// Function to implement Ajax for Forgot password
function passwordinfo(pwd) {
	xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert ("Your browser does not support AJAX!");
			return;
		} 
	var url="forgot_password";
	url=url+"?mail="+pwd;		
	xmlHttp.onreadystatechange=hiddenChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

// Function to implement Ajax for Group Discussion
function groupread(base_url,group_id,id) {
	
	var chks = document.getElementsByName('group_diss[]');
	var hasChecked = false;
	var discussion="";
	
	for (var i = 0; i < chks.length; i++)
	{
		if (chks[i].checked)
		{
			hasChecked = true;
			if(discussion=="")
			{
				discussion=chks[i].value;
			}
			else
			{
				discussion=discussion+","+chks[i].value;
			}
		}
	}
	if (hasChecked == false)
	{
		alert("Please choose at least one discussion to read.");
		return false;
	}
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	else
	{
		window.location=base_url+id+"/"+group_id;
	}
	var url=base_url+"groupdetail/"+group_id+"/read";
	url=url+"?grp_diss_id="+discussion;
	xmlHttp.onreadystatechange=hidden_Changed;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}

function GetXmlHttpObject() {
	var xmlHttp=null;
		try
		{
			// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		}
		catch (e)
		{
			// Internet Explorer
			try
			{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e)
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
	return xmlHttp;
}

function hiddenChanged() {
	document.getElementById("spinner").style.display = 'block';
	if (xmlHttp.readyState==4) { 			
		document.getElementById("spinner").style.display = 'none';	
		document.getElementById("popupmessage").style.display = 'block';
		if(xmlHttp.responseText == 1) {
			document.getElementById("popupmessage").innerHTML = user_register_error_mail;	
		}
		else if(xmlHttp.responseText == 2) {
			document.getElementById("popupmessage").innerHTML = user_register_error_mail_valid;
		}
		else if(xmlHttp.responseText == 3) {
			document.getElementById("popupmessage").innerHTML = forgot_pwd_notexist_mail;
		}
		else if(xmlHttp.responseText == 4) {
			document.getElementById("popupmessage").innerHTML = forgot_pwd_send_mail;
			document.getElementById("popupmessage").style.color = "#4BA362";
		}
		else {
			document.getElementById("popupmessage").innerHTML = forgot_pwd_check_mail;
		}
	}	
}

function hidden_Changed() {	
	if (xmlHttp.readyState==4) { 
		if(xmlHttp.responseText == 0) {					
			document.getElementById("login_msg").style.display = 'block';	
			document.getElementById('edit-pass').focus();
			document.getElementById('edit-pass').value = "";
			return false;
		}
		else {
			form_submit(base_url);
			return true;
		}
	}
}

function seeallUserJoin(type,joinstatus,gid,basepath) 
{
	document.getElementById('SeeallErrMsg').style.display="block";
	if(joinstatus == "request")
	{
		var joinmsg="&nbsp;&nbsp;<a href='"+basepath+"og/subscribe/"+gid+"?destination=groupdetail/"+gid+"'>Click here</a> to send request membership to join in this group";	
	}
	else
	{
		var joinmsg="&nbsp;&nbsp;<a href='"+basepath+"og/subscribe/"+gid+"?destination=groupdetail/"+gid+"'>Click here</a> to join in this group";	
	}
	
	
	if(type=="new")
	{
		document.getElementById('SeeallErrMsg').innerHTML = 'Please join in this group to create new discussion'+joinmsg;
	}
	else if(type=="anonnew")
	{
		document.getElementById('SeeallErrMsg').innerHTML = 'Please register to create new discussion.&nbsp;&nbsp;<a href="'+basepath+'user/register">Click here</a> to register';
	}
	else if(type=="reply")
	{
		document.getElementById('SeeallErrMsg').innerHTML = 'Please register to reply for the discussion. &nbsp;&nbsp;<a href="'+basepath+'user/register">Click here</a> to register';
	}
}

function seeallFreeze() 
{
	document.getElementById('group_show_messages').style.display="none";
	document.getElementById('SeeallErrMsg').style.display="block";
	document.getElementById('SeeallErrMsg').innerHTML = groupdetail_freeze_discussion;
}

/*function trimAll( strValue ) {

 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}
*/

