var loc1,loc2;
var log1,log2;
var def="http://nirmaan2010.com/main/";
var urlL= "http://nirmaan2010.com/main/logged.php?";
function reload()
{
	loc1=window.location.href;
	check();
	if(loc1!=loc2)
		frameload();
		loc2=loc1;
		setTimeout("reload()",1000);
}
function check()
{
	id=getCookie('id');


	if (id!=null && id!="")
	{
		var fullurlL = urlL + 'opt=logged';
		log1=1;
		
	}
	else
	{
		var fullurlL = urlL + 'opt=unlogged';
		log1=0;
	}
	if(log1!=log2){
		
		document.getElementById("loginBlock").innerHTML="loading....";
		 if (window.XMLHttpRequest) {
			httpL = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			httpL = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		log2=log1;
		httpL.open("GET", fullurlL, true);
		httpL.send(null);
		httpL.onreadystatechange = statechange_logchange;		
	}
	setTimeout("check()",2000);
}

function statechange_logchange() {
	if (httpL.readyState) {	
		var htmlL = httpL.responseText;
		if(htmlL.indexOf("Not Found")!=-1){
			check();log2++;}
		document.getElementById("loginBlock").innerHTML = htmlL;
		$(".signin").click(function(e) {
			e.preventDefault();
			$("fieldset#signin_menu").toggle();
			$(".signin").toggleClass("menu-open");
			$(".username").focus();
		});
		$("fieldset#signin_menu").mouseup(function() {
			return false
		});
		$(document).mouseup(function(e) {
			if($(e.target).parent("a.signin").length==0) {
				$(".signin").removeClass("menu-open");
				$("fieldset#signin_menu").hide();
			}
		});
	}
	else{
		document.getElementById("loginBlock").innerHTML ="Loading......";}
	
}

function logout()
{
	showDiv();
	 var d = new Date();
    	document.cookie = "username=;expires=" + d.toGMTString() + ";" + ";";
	document.cookie = "id=;expires=" + d.toGMTString() + ";" + ";";
	document.location=def+"#Home.html"
	hideDiv();
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}
function frameload() {
	loc=window.location.href;

	if(loc.indexOf('#')!=-1)
	{
		frameloc = loc.substring(loc.indexOf("#")+1, loc.length);
		if(frameloc=="")document.location=def+"#Home.html";
		else{
		showDiv();
		document.getElementById("divbody").innerHTML = ' Fetching data...';
		document.getElementById("divbody").style.height=window.screen.availHeight-250;
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}

		if (req != undefined) { 
			var timestamp = fetch_unix_timestamp(); 
			req.onreadystatechange = function() {ahahDone(frameloc);};
		if(frameloc.indexOf("?")==-1)
    				req.open("GET", "pages_440005/"+frameloc+"?timestamp=" + timestamp, true);
		else
			req.open("GET", "pages_440005/"+frameloc+"&timestamp=" + timestamp, true);
    				req.send("");
  			}
		}
	}
	else{
                
		document.location=def+"#Home.html";
	}
	return false;
}

 

function ahahDone(url) {
	if (req.readyState == 4) { // only if req is "loaded"
	    if (req.status == 200) { // only if "OK"
		document.getElementById("divbody").innerHTML = req.responseText;
		var urlname=url.substring(0,url.indexOf("."));
		document.title=".:: NIRMAAN 2010::. "+urlname;
		document.getElementById("divbody").style.height="auto";
  
		if(document.getElementById("divbody").clientHeight<window.screen.availHeight-250)
			document.getElementById("divbody").style.height=window.screen.availHeight-250;


		var sref=document.createElement('script');
		sref.setAttribute("type","text/javascript");
		sref.setAttribute("src", "scripts/"+urlname+".js");

      		if (typeof sref!="undefined")
			document.getElementsByTagName("head")[0].appendChild(sref);

		var cref=document.createElement("link");
		cref.setAttribute("rel", "stylesheet");
		cref.setAttribute("type", "text/css");
		cref.setAttribute("href", "stylesheet/"+urlname+".css");
       
		if (typeof cref!="undefined")
			document.getElementsByTagName("head")[0].appendChild(cref);
	      } else {
		document.location=def+"#Home.html";
	      }
		hideDiv();
	   }  
}



function hideDiv() { 
	document.getElementById('loading').style.visibility = 'hidden'; 
}

function showDiv() { 
	if (document.getElementById) { // DOM3 = IE5, NS6 
		document.getElementById('loading').style.visibility = 'visible'; 
	} 
	else { 
		if (document.layers) { // Netscape 4 
			document.loading.visibility = 'visible';
		} 
		else { // IE 4 
			document.all.loading.style.visibility = 'visible'; 
		} 
	} 
}







var menu=function(){
	var t=5,z=50,s=3,a;
	function dd(n){this.n=n; this.h=[]; this.c=[]}
	dd.prototype.init=function(p,c){
		a=c; var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.st('+i+',true)');
			h.onmouseout=new Function(this.n+'.st('+i+')');
		}
	}
	dd.prototype.st=function(x,f){
		var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
		clearInterval(c.t); c.style.overflow='hidden';
		if(f){
			p.className+=' '+a;
			if(!c.mh){c.style.display='block'; c.style.height=''; c.mh=c.offsetHeight; c.style.height=0}
			if(c.mh==c.offsetHeight){c.style.overflow='visible'}
			else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
		}else{p.className=p.className.replace(a,''); c.t=setInterval(function(){sl(c,-1)},t)}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter=''; c.style.opacity=1; c.style.overflow='visible'}
			clearInterval(c.t); return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
		c.style.opacity=o; c.style.filter='alpha(opacity='+(o*100)+')';
		c.style.height=h+(d*f)+'px'
	}
	return{dd:dd}
}();





function checkUser()
{
document.getElementById("loginStatus").innerHTML = "Signing in.... Please Wait.....";
	if (window.XMLHttpRequest)
		http = new XMLHttpRequest();
	else if (window.ActiveXObject)
		http = new ActiveXObject("Microsoft.XMLHTTP");
	usr = document.getElementById('username').value;
	pars = document.getElementById('password').value;	
	if(usr=="" || pars=="" ){
		document.getElementById("loginStatus").innerHTML = "Username and Password cannot be blank";
		document.getElementById('username').focus();
	}else { 
		// Timestamp for preventing IE caching the GET request
		
		var timestamp = fetch_unix_timestamp(); 
		var fullurl = 'http://nirmaan2010.com/main/pages_440005/checkLogin.php?do=check&username=' + encodeURIComponent(usr) + '&password='+encodeURIComponent(pars)+'&timestamp=' + timestamp;
		http.open("GET", fullurl, true);
		http.send(null);
		http.onreadystatechange = statechange;
	}

}
fetch_unix_timestamp = function() {
			return parseInt(new Date().getTime().toString().substring(0, 10));
		}
function statechange() {
	if (http.readyState == 4) {
		var html = http.responseText;
		if(html.indexOf("Successfull")==0){
			var id=html.substring(html.indexOf("=")+1);
			usr = document.getElementById('username').value;
			setCookie("id",id,1);
			document.location="http://nirmaan2010.com/main/#UserDetails.php"
		}else						document.getElementById('loginStatus').innerHTML =html;
	}
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}