var IE = document.all?true:false;

function validation_login()
{
	var passing_sc_id="";
	var full_scid_portfolio="";

	var user_login=document.getElementById("wl_login").value;
	var section_val=document.getElementById("section").value;
	var password_login=document.getElementById("wl_login_pass").value;
	//var section=document.getElementById("section_id").value;
	document.getElementById("msg_div").innerHTML = "";
	if(user_login=="")
	{
		alert("Please enter your Username");	
		document.getElementById("wl_login").focus();
		return false;
	}
	if(password_login=="")
	{
		alert("Please enter your Password");
		document.getElementById("wl_login_pass").focus();
		return false;
	}

var old_login_but_div=document.getElementById("login_but_div").innerHTML;
document.getElementById("login_but_div").innerHTML ="<img src='http://img1.moneycontrol.com/images/simple_watchlist/ajax-loader_email.gif' hspace=0 vspace=0 border=0>";

				if(!IE)
					var objXml = new XMLHttpRequest();
				else
					var objXml = new ActiveXObject("Microsoft.XMLHTTP");

					datafile = "/watchlist1/login_check.php?username="+user_login+"&password="+password_login+"&section="+section_val;


				objXml.open("GET", datafile, true);
				objXml.setRequestHeader("Cache-Control","no-cache"); 

				objXml.onreadystatechange=function() 
					{
						if (objXml.readyState==4) 
						{
						if (objXml.status!=404)
							{	
							   if((objXml.responseText).length > 0)
								{
									var response=objXml.responseText;
									if(response=="Login does not exist" || response=="Invalid Password")
									{
									document.getElementById("msg_div").style.display="block";
									document.getElementById("msg_div").innerHTML = objXml.responseText;	
									document.getElementById("login_but_div").innerHTML =old_login_but_div;
									}
									else if(response=='alldone')
									{
										//alert(section_val);
										if(section_val=="cookie_add")
											add_trans('cookie');
										else if(section_val=="watchlist_add")
											add_trans('watchlist');
										else if(section_val=="full_watchlist")
											window.location.href = "/watchlist1/watchlist_fullview.php";
										else
										next_prev('0', section_val);
									}
									
								}
							}
						
						}
					}
				objXml.send(null);


}
///////////////////////////////////////////////////////


function tep()
{
	if(navigator.appName == "Microsoft Internet Explorer")
	{
	 	document.getElementById('tep').className="tep2";
	}
	else
	{
		document.getElementById('tep').className="tep1";
	}
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function P7_autoLayers() { //v1.4 by PVII
 var g,b,k,f,args=P7_autoLayers.arguments;a=parseInt(args[0]);if(isNaN(a))a=0;
 if(!document.p7setc){p7c=new Array();document.p7setc=true;for(var u=0;u<10;u++){
 p7c[u]=new Array();}}for(k=0;k<p7c[a].length;k++){if((g=MM_findObj(p7c[a][k]))!=null){
 b=(document.layers)?g:g.style;b.visibility="hidden";}}for(k=1;k<args.length;k++){
 if((g=MM_findObj(args[k]))!=null){b=(document.layers)?g:g.style;b.visibility="visible";f=false;
 for(var j=0;j<p7c[a].length;j++){if(args[k]==p7c[a][j]) {f=true;}}
 if(!f){p7c[a][p7c[a].length++]=args[k];}}}
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}


function validate_frm()
{
emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
phoneRe = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/

	if (document.frmregistration.username.value=="")
	{
		alert("Enter Username");
		document.frmregistration.username.focus();
		return false;
	}
	if (document.frmregistration.namenotavilable.value != "")
	{
		alert("Username already taken");
		document.frmregistration.username.focus();
		return false;
	}
	if (document.frmregistration.password.value=="")
	{
		alert("Enter password");
		document.frmregistration.password.focus();
		return false;
	}
	if (document.frmregistration.cpwd.value=="")
	{
		alert("Enter confirm password");
		document.frmregistration.cpwd.focus();
		return false;
	}
	if (document.frmregistration.password.value != document.frmregistration.cpwd.value)
	{
		document.getElementById('passnotsame').innerHTML="Password and Confirm password not same";
		document.frmregistration.cpwd.focus();
		return false;
	}
	else
	{
		document.getElementById('passnotsame').innerHTML="";
	}
	if (!emailRe.test(document.frmregistration.email.value))
	{
		alert("Enter Valid Email");
		document.frmregistration.email.focus();
		return false;
	}
	if (document.frmregistration.emailnotavilable.value != "")
	{
		alert("Email already exist");
		document.frmregistration.username.focus();
		return false;
	}


var post_username=document.frmregistration.username.value;
var post_password=document.frmregistration.password.value;
var post_email=document.frmregistration.email.value;

var old_reg_but_div=document.getElementById("reg_but_div").innerHTML;
document.getElementById("reg_but_div").innerHTML ="<img src='http://img1.moneycontrol.com/images/simple_watchlist/ajax-loader_email.gif' hspace=0 vspace=0 border=0>";


				if(!IE)
					var objXml = new XMLHttpRequest();
				else
					var objXml = new ActiveXObject("Microsoft.XMLHTTP");

					datafile = "/watchlist1/register.php?username="+post_username+"&password="+post_password+"&email="+post_email+"&regon=mc";


					objXml.open("GET", datafile, true);
				objXml.setRequestHeader("Cache-Control","no-cache"); 

				objXml.onreadystatechange=function() 
					{
						if (objXml.readyState==4) 
						{
						if (objXml.status!=404)
							{	
							   if((objXml.responseText).length > 0)
								{
								   
									if(objXml.responseText=="1")
									{
										document.getElementById("register_div").innerHTML = "<table cellpadding=0 cellspacing=0 border=0 width=430px><tR><Td height=2px></td></tr><tr><Td height=2px background='http://img1.moneycontrol.com/images/simple_watchlist/new1/effect_line_hori.gif' width=235px ></td></tR><tr><td height=5px></td></tr><tr><td align=left valign=top class=watchlist_refresh><b>Thank you for registering on Moneycontrol.com</b><br><br>Your account <B>"+post_username+"</B> has been created. However, to activate the account, please check your email and click on the link provided in the mail.<BR><BR>Incase you haven’t got the email (check the Junk/Spam folders also), you can still get temporary access (limited to 5 logins) to our site by using the username & password you gave while registering.<BR>Now you can login to create your Watchlist.</td></tr></table>";
									}
									else
									{
										document.getElementById("reg_but_div").innerHTML =old_reg_but_div;
										document.getElementById('available').innerHTML="Error while registration";
									}									
									
									
								}
							}
						
						}
					}
				objXml.send(null);


return false;
		
}


function displayResponse(req) 
	{ 
	document.getElementById('available').innerHTML="";
	document.getElementById('namenotavilable').value="";
	jStr = req.responseText; 
	alert(jStr);
		if(jStr != "")
		{
			alert(jStr);
		}

	}


function checkpasswordsame()
{
	if (document.frmregistration.password.value != document.frmregistration.cpwd.value)
	{
		document.getElementById('passnotsame').innerHTML="Password and Confirm password not same";
		document.frmregistration.cpwd.focus();
		return false;
	}
	else
	{

			document.getElementById('passnotsame').innerHTML="";
	}
}

function checkifavilable(val)
{
	url = "/registration/register/commonusercheck.php?name="+val;
	callAHAH(url, 'get','', displayContent);
}


function displayContent(req) { 

	document.getElementById('available').innerHTML="";
	document.getElementById('namenotavilable').value="";
	jStr = req.responseText; //alert(jStr);
		if(jStr != "")
		{
			if(jStr=='The Login Id is already in use. Please use another')
				jStr="Login Id is already in use";

			document.getElementById('available').innerHTML=jStr;
			document.getElementById('namenotavilable').value=jStr;
			document.getElementById('username').focus();
		}

	}

function checkemail(val)
{
	if(val =="")
	{
		document.getElementById('availableemail').innerHTML="Enter Email";
		return false;
	}
	else
	{
		document.getElementById('availableemail').innerHTML="";
	}
	url = "/registration/register/commonemailcheck.php?email="+val;
	callAHAH(url, 'get','', displayContentemail);
}

function displayContentemail(req) { 
	document.getElementById('availableemail').innerHTML="";
	document.getElementById('emailnotavilable').value="";
	jStr = req.responseText;// alert(jStr);
		if(jStr != "")
		{
			if(jStr=='The entered Email is already in use')
			{
				jStr="Email Id is already in use";
			}
			document.getElementById('availableemail').innerHTML=jStr;
			document.getElementById('emailnotavilable').value=jStr;
			document.getElementById('email').focus();
		}

	}

function callAHAH(url, type, parameters, fname){ 
	try {
		req = new XMLHttpRequest(); /* e.g. Firefox */
	} catch(e) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP"); /* some versions IE */
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP"); /* some versions IE */ }
			catch (E) {
				req = false;
			}
		}
	}
	req.onreadystatechange = function() {
	responseAHAH(fname);
	};
	
	req.open(type,url,true);
    req.setRequestHeader("Content-length", parameters.length);
	if(parameters == '') parameters = null;
	req.send(parameters);
	
}



function responseAHAH(fname) {
	if(req.readyState == 1 ) { //loading
		if(document.getElementById('busyimg') != null){ 
			document.getElementById('busyimg').style.display = "block";
		}
	}
	if(req.readyState == 4) {
		if(document.getElementById('busyimg') != null)
				document.getElementById('busyimg').style.display = "none";
		if(req.status == 200) {
			 fname(req);		
			
		} else {
			alert("Ooops Error! Call 911");
		}
	}
}

//////////////////// LogOut


function logout()
{
document.getElementById('portfolio_full_scid').value="";			
//alert(document.getElementById('portfolio_full_scid').value);
passing_sc_id="";
full_scid_portfolio="";
//alert(passing_sc_id);
//alert(full_scid_portfolio);
	
			if(!IE)
					var objXml = new XMLHttpRequest();
				else
					var objXml = new ActiveXObject("Microsoft.XMLHTTP");

					datafile = "/watchlist1/logout.php";


				objXml.open("GET", datafile, true);
				objXml.setRequestHeader("Cache-Control","no-cache"); 

				objXml.onreadystatechange=function() 
					{
						if (objXml.readyState==4) 
						{
						if (objXml.status!=404)
							{	
							   if((objXml.responseText).length > 0)
								{
									next_prev('0', 'cookie');	
									document.getElementById('portfolio_full_scid').value="";	
									document.getElementById('portfolio_full_cnt').value="";
									document.getElementById('watchlist_full_scid').value="";
									var full_scid_watchlist=0; 
									var full_scid_portfolio=0; 
									var full_scid_portfolio_cnt=0;
									
								}
							}
						
						}
					}
				objXml.send(null);
}
