var m_act;
var m2_act;

function initial(tabval,menuval){
	if(tabval==''){
		m_act= 8;
	}else{
		m_act =tabval;
	}
	
	if(menuval==''){
		m2_act= 10;
	}else{
		m2_act =menuval;
	}
	init();
	onloadsettheme();
	onloadsignin();
}
function init()
{
	if(m2_act != 10 && m2_act != 9)
	{	
		document.getElementById('tt_'+ m_act).className = "act";//alert(m_act);
	}
	document.getElementById('m_t_'+m2_act).className = "w_12_off";
	document.getElementById('m_i_'+m2_act).className = 'icons2 icon-m-'+m2_act;
	document.getElementById('m_cur1_'+m2_act).className = 'cur1_on';
	document.getElementById('m_cur2_'+m2_act).className = 'cur2_on';
	document.getElementById('m_curbg_'+m2_act).className = 'curbg_on';

}
function htab(a, b, c, d)
{

	for(var i=1; i<=b; i++)
	{
		if(i == a){
			
			
			document.getElementById(c+i).className = "act";
			document.getElementById(d+i).style.display = "block"
		}
		else{
			
			document.getElementById(c+i).className = "nact";
			document.getElementById(d+i).style.display = "none"
		}
	}
}
function ttab(a, b, c, d)
{
	for(var i=1; i<=b; i++)
	{
		if(i == a){
			document.getElementById(c+i).className = "act";
			m_act = i;
		}
		else{
			document.getElementById(c+i).className = "nact";
		}
	}
}

function over_ttab(a)
{
	document.getElementById('tt_'+a).className = "act";
}
function out_ttab(a)
{
	if(m_act == a)
	{
		document.getElementById('tt_'+a).className = "act";
	}
	else
	{
		document.getElementById('tt_'+a).className = 'nact';
	}
}
function ttab(a, b, c, d)
{
	for(var i=1; i<=b; i++)
	{
		if(i == a){
			//alert(c+i);
			document.getElementById(c+i).className = "act";
			document.getElementById(d+i).style.display="block";
			m_act=a;
		}
		else{
			document.getElementById(c+i).className = "nact";
			document.getElementById(d+i).style.display="none";
		}
	}
}

function tp_menu_off(a)
{
	document.getElementById('m_t_'+a).className = "w_12_off";
	document.getElementById('m_i_'+a).className = 'icons2 icon-m-'+a;
	document.getElementById('m_cur1_'+a).className = 'cur1_on';
	document.getElementById('m_cur2_'+a).className = 'cur2_on';
	document.getElementById('m_curbg_'+a).className = 'curbg_on';
}
function tp_menu_on(a)
{
	if(m2_act == a)
	{
		document.getElementById('m_t_'+a).className = "w_12_off";
		document.getElementById('m_i_'+a).className = 'icons2 icon-m-'+a;
		document.getElementById('m_cur1_'+a).className = 'cur1_on';
		document.getElementById('m_cur2_'+a).className = 'cur2_on';
		document.getElementById('m_curbg_'+a).className = 'curbg_on';

	}
	else
	{
		document.getElementById('m_t_'+a).className = "w_12_on";
		document.getElementById('m_i_'+a).className = 'icons icon-'+a;
		document.getElementById('m_cur1_'+a).className = 'cur1_off';
		document.getElementById('m_cur2_'+a).className = 'cur2_off';
		document.getElementById('m_curbg_'+a).className = 'curbg_off';
	}
}

function onloadsettheme(){
		if(readCookie('mytheme')){
			settheme(readCookie('mytheme'));	
		}else{
			setthemeclassname='mast';
			settheme(setthemeclassname);	
		}
	}
function settheme(themeclassname){		//Onclick will set themes
		url = "/active18/common/update_theme.php";			//alert(url);
		par="theme="+themeclassname;	//alert(url+par);
		document.getElementById('main').className=themeclassname;
		callAHAH(url, 'POST',par, displayContent);
		Set_Cookie( 'mytheme', themeclassname, '', '/', '', '' );
	}
function 	displayContent(){}//Coz cant do without it :)
function Set_Cookie( name, value, expires, path, domain, secure ){
		var today = new Date();
		today.setTime( today.getTime() );
		if ( expires )
		{
		expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
	}
function Get_Cookie( name ) {	
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) &&
		( name != document.cookie.substring( 0, name.length ) ) )
		{
		return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ";", len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	}	


function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


function loginverify(f_source)
{	

	if(f_source == '')
	{
		if (document.getElementById('f_sourceret') != null)
		{
				f_source = $('#f_sourceret').val();
		}
	}
	if(f_source == '')
		f_source = "jquery";

	var f_idval = "";
	if (document.getElementById('f_id') != null)
	{
			f_idval = $('#f_id').val();
	}
	if (document.getElementById('f_id_new') != null)
	{

		if(f_idval == "")
		{

				f_idval = $('#f_id_new').val();
		}
	}
	if (document.getElementById('f_id_listen') != null)
	{
		if(f_idval == "")
		{
				f_idval = $('#f_id_listen').val();
		}
	}
	if (document.getElementById('f_id_plan') != null)
	{
		if(f_idval == "")
		{
				f_idval = $('#f_id_plan').val();
		}
	}
	if (document.getElementById('f_id_read') != null)
	{
		if(f_idval == "")
		{
				f_idval = $('#f_id_read').val();
		}
	}

	if (f_idval=="")
	{		
		alert("please enter username");
		return;
	}
	
	f_fwdval= "";
	if (document.getElementById('f_pwd') != null)
	{
			f_fwdval = $('#f_pwd').val();
	}
	 if (document.getElementById('f_id_new') != null)
	{
			if(f_fwdval == "")
		{
			f_fwdval = $('#f_pwd_new').val();
		}
	}
	 if (document.getElementById('f_pwd_listen') != null)
	{
			if(f_fwdval == "")
		{
			f_fwdval = $('#f_pwd_listen').val();
		}
	}
	if (document.getElementById('f_pwd_plan') != null)
	{
			if(f_fwdval == "")
		{
			f_fwdval = $('#f_pwd_plan').val();
		}
	}
	if (document.getElementById('f_pwd_read') != null)
	{
			if(f_fwdval == "")
		{
			f_fwdval = $('#f_pwd_read').val();
		}
	}
	if (f_fwdval=="")
	{		
		alert("please enter password");
		return;
	}

	// Encryption of Username
//	var m_len = 0;
//	var m_value = "";
//	m_value = f_idval;
//	m_len = (m_value).length;
//	for(m_ctr=m_len; m_len <= 5; m_len++)
//		m_value += " ";
//	f_idval = IiI(IIj(m_value, jj, ""));
//
//	// Encryption of Password
//	var m_len = 0;
//	var m_value = "";
//	m_value = f_fwdval;
//	m_len = (m_value).length;
//	for(m_ctr=m_len; m_len <= 5; m_len++)
//		m_value += " ";
//	f_fwdval = IiI(IIj(m_value, jj, ""));

	newwinChecked = "";
	if (document.getElementById('newwinCheck') != null)
		newwinChecked = document.getElementById('newwinCheck').checked;
	$.post("/active18/loginverify.php", {'f_id': f_idval,'f_pwd': f_fwdval,'f_source': f_source}, function(data) {
	//	alert(data + f_source);
		returnstr = data.split("|");

		if(returnstr[0]=="Error")
		{
			err = returnstr[1];
							if (err == 1 ) { 
									
                                    messagedispaly = '<b>The password you entered is not correct. Password is casesensitive </b><span style="padding-top:5px;"><a href="#" class="bl_12" onclick="forgotin();">forgot password?</a></span>';
									msgdisp = '<b>The password you entered is not correct.</b>';
                                }
                                else if (err == 2)
                                {
                                    messagedispaly = '<b>Sorry! this is not a valid IN.com Username </b>';
									msgdisp = '<b>Sorry! this is not a valid IN.com Username </b>';
                                }
								 else if (err == 3)
                                {
									location.href='/login.php?err='+err;									
                                }
                               														
                                else if (err == 5)
                                {
                                    location.href='/login.php?err='+err;
                                }

								if (f_source == "home")
								{
									$('#disperrpop').html(msgdisp);
									return;
								}
								if (f_source == "jquery")
								{
									$('#disperrhome').html(msgdisp);
									return;
								}
								$('#displayerroralt').html(messagedispaly);
		}
		else if(f_source == 'email' && returnstr[0]=="success")
		{
			window.location.href="http://www.in.com/mails/mailstartup";
		}
		else if (f_source == 'social' && returnstr[0]=="success")
		{
			
			window.location.href =  "http://www.in.com/active18/social/userindex.php";
		}
		else if (f_source == 'myfeeds' && returnstr[0]=="success")
		{
			window.location.href =  "http://www.in.com/active18/readnow/";
		}
		else if(returnstr[0]=="success")
		{
				displaysignin_top();	
			if (document.getElementById('f_callback') != null)
			{
				var strcallback = $('#f_callback').val();
				if(strcallback.length > 0)
				{						
					location.href = strcallback ;
				}
				else
				{
					window.location.reload();
				}
			}
			else
			{
				window.location.reload();
			}

		}
		
		else
			{	
					displaysignin_top();	
				if (document.getElementById('f_callback') != null)
				{
					var strcallback = $('#f_callback').val();
					if(strcallback.length > 0)
					{						
						location.href = strcallback ;
					}
					else{
					window.location.reload();
					}
				}
				else
				{
					window.location.reload();
				}
				

			}
			
	});
}
	
function logoutfn()
{
	
	$.post("/logout",'', function(data) {
		
			str = '<div><h2><i>Signin</i></h2></div><div style="width:268px;"><div class="gD_12 PT10"><b>Username:</b></div><div><input type="text" name="f_id" id="f_id" value="" size="44" class="txtInput" /></div><div class="gD_12 PT10"><b>Password:</b></div><div><input type="password" name="f_pwd" id="f_pwd" value="" size="44" class="txtInput" /></div><div class="PT15"><div class="FL gD_12"><input type="checkbox" name="newwinCheck" id="newwinCheck" /> <b>Open mail in new window</b></div><div class="FR PR10"><a href="javascript:loginverify(\'ajaxhome\');"><img src="images/home_login.gif" /></a></div><div class="CL"></div></div><div class="PT10"><a href="javascript:mailsend();" class="bl_12" style="text-decoration:underline;">Forgot Password?</a> <a href="/mails/new_reg.php" class="bl_12" style="margin-left:74px;text-decoration:underline;" >Register Now!</a></div></div>';
			
			strhomelink = '<div style="padding: 8px 15px;"><h2>Sign <span class="gL_20">in</span></h2><div style="width: 268px;"><div class="gD_12 PT10"><b>Username:</b></div><div><input id="f_id" value="" size="44" class="txtInput" type="text"><input id="f_idmain" name="f_idmain" value="" size="27" class="txtInput4" type="hidden"><input id="f_pwdmain" name="f_pwdmain" value="" type="hidden"></div><div class="gD_12 PT10"><b>Password:</b></div><div><input id="f_pwd" value="" size="44" class="txtInput" onkeypress="{if (event.keyCode==13) loginverify(\'\')}" type="password"><input value="home" id="f_source" type="hidden"></div><div class="PT15 PB10"><div class="FL"><a href="javascript:void(0);"><img src="http://im.in.com/active18/homenew1/images/home_login.gif" onclick="loginverify(\'\')"></a></div><div class="FL PL10 PT10"><a href="#" class="bl_2_12" onclick="forgotin();">Forgot Password?</a></div><div class="CL"></div></div></div></div>';
		


			strlogout = '<div style="padding:3px 0px 5px 0px;"><a href="#" onclick="addsignin(\'home\',\'\',\'\');" class="w_11"><b>Sign in</b></a> &nbsp;|&nbsp; <a  href="/mails/new_reg.php" class="w_11"><b>Register</b></a></div>';

		
			if (document.getElementById('homesignin') != null)
						$('#homesignin').html(strhomelink);
			if (document.getElementById('homesignin1') != null)
						$('#homesignin1').show();
			if (document.getElementById('topsignin') != null)
				{					
						$('#topsignin').html(strlogout);
				}

				displaysignout_top();
				location.href="http://www.in.com/";
		});


}
/*if(readCookie('ui')!=''){	alert(readCookie('ui'));
	setTimeout("callalertstop()", 30000);
}*/
//setTimeout("callalertstop()", 30000);

function callalertstop()
{
	if(readCookie('ui')!='' && readCookie('ui')!=null)
	{		//alert("C:" + readCookie('ui'));
		
		str = "";
		$.post("/active18/common/alerts.php",{'lastappid':jsmenuval},function(data) {	
			//alert(data);			
			if(data != "" && data != "<script>window.location='/login.php?err=0'</script>" && data != "<script>window.location='/home/index.php?msg=1'</script>" && data != "<script>window.location='/'</script>")
			{		
				
				str = data.split("|");
	
				if(str[3]!='undefined' && str[2] != '' && str[5] != '' && str[3] != '')
				{					
					$('#fromnamealerttop').html(str[2]);
					strlink  = '<a href="/mails/openmailstartup/'+str[0]+'" )">'+str[3]+'</a>';				
					$('#messagealerttop').html(strlink);				
					$('#alertstop').slideToggle("slow");
					setTimeout("hidecallalertstop('alertstop')", 10000);
				}
				str = "";
			}
			if(data == "<script>window.location='/'</script>" || data == "<script>window.location='/login.php?err=0'</script>")
			{
				
				window.location="/";
			}
			data ="";
			
		});
	}
	//setTimeout("callalertstop()", 30000);
}
// this code added in alerts.php
//function updateaccess(ins,uia) {
//	
//	$.post("/mails/updatelastaccess.php", {'lastappid': ins,'uia':uia});
//}
function hidecallalertstop(alertname)
{
	$('#'+alertname).hide();
}

function newtopsignin()
{
	
	//$.post('/active18/common/functiongetvalue.php',{'passstr':readCookie('ui')},function(data){
		
//if(data =='Signin')

		if(readCookie('ui') == null || readCookie('ui') == '' || readCookie('ui')== undefined)
		{
			
			strlogin = '<div style="padding:3px 0px 5px 0px;"><a href="#" onclick="addsignin(\'home\',\'\',\'\');" class="w_11"><b>Sign in</b></a> &nbsp;|&nbsp; <a href="/mails/new_reg.php"  class="w_11"><b>Register</b></a></div>';
			$('#topsignin').html(strlogin);	
			strhomelink = '<div style="padding:8px 15px;"><h2>Sign <span class="gL_20">in</span></h2><FORM METHOD=POST name="formlogin" ACTION="/active18/loginverify.php"><div id="disperrhome"></div><div style="width:268px;"><div class="gD_12 PT10"><b>Username:</b></div><div><input type="text" id="f_id" name="" value="" class="txtInput" style="width:250px;" /><input type="hidden" id="f_idmain" name="f_idmain" value="" size="27" class="txtInput4" /><input type="hidden" id="f_pwdmain" name="f_pwdmain" value="" /></div><div class="gD_12 PT10"><b>Password:</b></div><div><input type="Password" id="f_pwd" name="" value="" class="txtInput" onkeypress="{if (event.keyCode==13) loginverify(\'\')}" style="width:250px;" /></div><div class="PT15 PB10"><div class="FL"><a href="#" onclick="loginverify(\'\')"><img src="http://im.in.com/active18/images/home_login.gif" /></a></div><div class="FL PL10 PT10"><a href="#" class="bl_2_12" onclick="forgotin();">Forgot Password?</a> &nbsp;&nbsp; </div><div class="CL"></div></div></div>';
			if (document.getElementById('homesignin') != null)
					$('#homesignin').html(strhomelink);
			displaysignout_top();
				
		}
		else 
		{
			
			var display_name ="";
			var displayshort_name ="";
			display_name = readCookie('dispname');
			if(display_name != '')
			{
				displayshort_name = display_name.split('+');
			}
			strlogin = '<div style="padding:3px 0px 5px 0px;"><b>Welcome '+display_name.replace('+',' ')+'!</b> &nbsp;&nbsp;&nbsp; <a href="/mails/openstartup/settings"  class="w_11">Settings</a> &nbsp;|&nbsp; <a href="javascript:void(0);" class="w_11" onclick="logoutfn()">Sign out</a></div>';
			strhomeadd ='<iframe src="http://www.in.com/active18/addserver/homeadd.html" height="305" width="365" frameborder=0></iframe>';	
			strhomelink = "<div class='loader'></div>";
			if (document.getElementById('homesignin') != null)
					$('#homesignin').html(strhomelink);		
			$('#topsignin').html(strlogin);	
			$.post("/active18/mails/fetchmailcount.php", "", function(data1) {
				$.post("/active18/social/fetchfriendrequest.php", "", function(friendrequest) {						
					//friendrequest = friendrequest.split('~');
					userimage = 'http://im.in.com/active18/images/h_profile_icon.jpg'; 				
//					if(friendrequest[1].length > 0)
//					{
//						userimagearr = friendrequest[1].split('|');
//						userimage = '/active18/social/thumbnail_4/'+userimagearr[1]+'/'+userimagearr[0]+'.gif';						
//					}				
				strhomelink = '<div style="padding:8px 15px;"><h2>welcome <font style="color:#666; font-weight:normal;">'+displayshort_name[0]+'!</font></h2><div style="width:268px;"><div style="height:145px;"><div class="pr_brd_b PT10 PB10"><div class="FL PR5"><img src="http://im.in.com/active18/images/h_mail_icon.gif" /></div><div class="FL"><a href="/mails/mailstartup" class="bl_2_12" >'+data1+'</a></div><div class="CL"></div></div><div class="pr_brd_b PT10 PB10"><div class="FL PR5"><img src="'+userimage+'" /></div><div class="FL gD_12"><a href="/active18/social/userindex.php" class="bl_2_12" >My Profile</a><p class="PT3"><a href="/active18/social/userindex.php" class="bl_3_11" >'+friendrequest+'</a></p></div><div class="CL"></div></div><div class="PT10 PB10"><div class="FL PR5 PT3"><img src="http://im.in.com/active18/images/h_feed_icon.jpg" /></div><div class="FL"><a href="/active18/readnow/myfeeds.php"  class="bl_2_12">My Feeds</a></div><div class="CL"></div></div></div><div align="right" class="PT5"><a href="#" onclick="logoutfn()" class="bl_2_12">Signout</a></div></div></div>';
				
				if (document.getElementById('homesignin') != null)
					$('#homesignin').html(strhomelink);
			
				});
			});
			displaysignin_top();
			
		}
		
	
//	});
	
	
}
function rightads()
{
		if(readCookie('ui') == null || readCookie('ui') == '' || readCookie('ui')== undefined)
		{
			strrightadd ='<iframe src="http://www.in.com/active18/addserver/promo1.html" height="250" width="300" frameborder=0></iframe>';
		}
		else 
		{
			strrightadd ='<a href="/active18/readnow/"><img src="http://im.in.com/active18/images/read01.jpg" /></a>';
		}
		if (document.getElementById('rightadd300') != null)
				$('#rightadd300').html(strrightadd);
			
}
function homeads()
{
		if(readCookie('ui') == null || readCookie('ui') == '' || readCookie('ui')== undefined)
		{
			strhomeadd ='<iframe src="http://www.in.com/active18/addserver/promo2.html" height="305" width="365" frameborder=0></iframe>';
			$('#invitefrndsign').show();
			$('#invitefrndsign1').hide();

		}
		else 
		{
			strhomeadd ='<iframe src="http://www.in.com/active18/addserver/add300.html" height="305" width="365" frameborder=0></iframe>';
			$('#invitefrndsign').hide();
			$('#invitefrndsign1').show();
		}
			if (document.getElementById('homeadd300') != null)
				$('#homeadd300').html(strhomeadd);
			
}
function displaysignout_top()
{
			$('#login2maildiv02').hide();
			$('#tmenu6').hide();
			$('#login2maildiv01').show();
			$('#login2maildiv11').show();
}
function displaysignin_top()
{
			$('#login2maildiv02').show();
			$('#tmenu6').show();
			$('#login2maildiv01').hide();
			$('#login2maildiv11').hide();
}