// JavaScript Document
function callTimers(obj)
{
	var o=obj;
	this.callSlide=function(){o.slidenav();}
	this.callMove=function(){o.move();}
}

temp=new Array();
function slidecontent (obj, slidewidth, interval, motiondist, slidesno, int_bw_slides, ref)
{
	this.slide = 0;
	this.object=obj;
	this.slidew =slidewidth;
	var me =this;
	this.gap=interval;
	this.step=motiondist
	this.sn=slidesno;
	this.int_bw_slides=int_bw_slides;
	var objid=ref;
	
	
	this.move=function()
	{
		if(this.sn -1 == this.slide)
		{
			this.slide=0;this.object.style.left="0px";temp[objid]=new callTimers(me);setTimeout(function(){temp[objid].callMove()},this.int_bw_slides);//alert('x1');
		}
		else
		{	
			new_value = this.slide * this.slidew;
			var l = this.object.style.left;var tv=l;
			l = parseInt(l.substr(0, this.object.style.left.indexOf('p')));//alert('new value: '+new_value+' pos: '+ l);
			if ( new_value > Math.abs(l)){
				this.object.style.left = (l - this.step) +"px";
				if(typeof temp[objid]== 'undefined')temp[objid]=new callTimers(me);
				setTimeout(function(){temp[objid].callMove()}, this.gap);//alert('x2');
			}
			else
			{
				this.object.style.left="-"+new_value+'px';this.slide+=1;
				if(typeof temp[objid]== 'undefined')temp[objid]=new callTimers(me);
				setTimeout(function(){temp[objid].callMove()},this.int_bw_slides);//alert('x3');
			}
		}
	}
}

function slide ()
{
	//it picks p from html page
	this.w=142;
	var h=5;
	var c='#930';
	var hand='';
	var interval=1;
	this.temp_pos=p;
	this.np=0;
	this.d='';
	var own=this;
	var tim='';
	
	this.create=function()
	{
		var ds=document.createElement('div');ds.style.position='relative';ds.style.backgroundColor=c;ds.style.width=this.w+'px';ds.style.height=h+'px';ds.style.left=(this.temp_pos*this.w)+'px';hand=document.getElementById('scroll').appendChild(ds);
	}
	
	this.intro=function(o,k)
	{
		clearTimeout(tim);var obj=o;var i=k;obj.np=i*obj.w;var pp=hand.style.left;pp = parseInt(pp.substr(0, hand.style.left.indexOf('p')));if(obj.np> pp){obj.d='r';}else if(i<obj.temp_pos){obj.d='l';}obj.temp_pos=i;obj.slidenav();
	}
	
	this.outro=function(o)
	{
		clearTimeout(tim);var obj=o;obj.np=p*obj.w;if(obj.temp_pos>p){obj.d='l';}else{obj.d='r';}obj.temp_pos=p;obj.slidenav();
	}
	
	this.slidenav=function (n)
	{
		
		//alert(this.d+'---------'+pp);
		if(this.d == 'r'){
			var pp=hand.style.left;pp = parseInt(pp.substr(0, hand.style.left.indexOf('p')));
			var x=parseInt(pp+8);
			hand.style.left=x+'px';
			if(this.np>pp){callon=new callTimers(own);tim=setTimeout("callon.callSlide()", interval);}else{hand.style.left=this.np+'px';}
		}
		else
		{
			var pp=hand.style.left;pp = parseInt(pp.substr(0, hand.style.left.indexOf('p')));
			var x=parseInt(pp-8);
			hand.style.left=x+'px';
			//alert(this.np+'-----'+pp);
			if( this.np<pp){callon=new callTimers(own);tim=setTimeout("callon.callSlide()", interval);}else{hand.style.left=this.np+'px';}
		}
	}
}

function resize(t, maxrow) 
{
	var con=trim(t.value);a = con.split('\n');
	b=1;
	for (var x=0;x < a.length; x++) 
	{ 
		if (a[x].length >= t.cols) b+= Math.floor(a[x].length/t.cols);
	}
	b+= a.length;
	if(maxrow > b) t.rows=b-1;
}

function FitToContent(text, maxHeight, minHeight) {
	//var text = id && id.style ? id : document.getElementById(id);
	//if (!text) return;

	var adjustedHeight = text.clientHeight;

	if (!maxHeight || maxHeight > adjustedHeight) {
		adjustedHeight = Math.max(text.scrollHeight, adjustedHeight);
		if (maxHeight)
			adjustedHeight = Math.min(maxHeight, adjustedHeight);
		if (adjustedHeight > text.clientHeight)
			text.style.height = adjustedHeight + "px";
	}

	if (!minHeight || minHeight < adjustedHeight) {
		adjustedHeight = Math.max(text.clientHeight, text.scrollHeight);
		if (minHeight)
			adjustedHeight = Math.max(minHeight, text.scrollHeight);
		if (adjustedHeight < text.clientHeight)
			text.style.height = adjustedHeight + "px";
	}
}

function trim(s)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}

function commentObj()
{
	var text, sender, insert_table, postid, obj, parobj;
	var norm, hid, b;
	
	function request(){if(window.XMLHttpRequest){return new XMLHttpRequest();}else if(window.ActiveXObject){return new ActiveXObject("Microsoft.XMLHttp");}else{document.getElementById("status").innerHTML = 'Your browser does not support this chat facility.' + 	' Consider upgrading your browser.';}}
	var send8765 = request();
	
	this.submitComment916=function(o, person, tbl, parentid, insertwhere, t1, t2, btn)
	{
		text=o.value;obj=o;sender=person;insert_table=tbl;postid=parentid;parobj=insertwhere;norm=t2;hid=t1;b=btn;
		obj.disabled="true";b.disabled="true";
		if(send8765.readyState == 4 || send8765.readyState == 0)
		{
			send8765.open("POST", 'submit_comment.php', true);
			send8765.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			send8765.onreadystatechange = handleComment916;
			var param = 'body=' + text;
			param += '&person=' + sender;
			param += '&table=' + insert_table;
			param += '&postid=' + postid;
			send8765.send(param);
		}
	}
	
	function handleComment916()
	{
		var xmldoc = send8765.responseXML;
		if(xmldoc != null)
		{
			var msg = xmldoc.getElementsByTagName('parcel');
			var dn = msg[0].getElementsByTagName('done');
			var d = dn[0].firstChild.nodeValue;
			var nnode = msg[0].getElementsByTagName('n');
			var n = nnode[0].firstChild.nodeValue;
			var pnode = msg[0].getElementsByTagName('p');
			var pic = pnode[0].firstChild.nodeValue;
			var unode = msg[0].getElementsByTagName('id');
			var senderid = unode[0].firstChild.nodeValue;
			if(d==1)
			{
				  var l=document.createElement('li');l.setAttribute('class', 'comments');l.setAttribute('className', 'comments');
				  var imgli=document.createElement('li');imgli.setAttribute('class', 'c-imgs');imgli.setAttribute('className', 'c-imgs');
				  var conli=document.createElement('li');conli.setAttribute('class', 'c-content');conli.setAttribute('className','c-content');
				  var u=document.createElement('ul');u.setAttribute('id', 'dd-ul');u.setAttribute('id','dd-ul');
				  
				  var img=new Image;img.src=pic;img.width=60;img.height=60;img.hspace=9;img.align='left';imgli.appendChild(img);conli.appendChild(document.createTextNode(text));conli.appendChild(document.createElement('br'));var bol=document.createElement('b');
				  var sl=document.createElement('a');sl.setAttribute('href','viewprofile.php?id='+senderid);
				  var nn=document.createTextNode(n);sl.appendChild(nn);var bynode=document.createTextNode('By: ');
				  bol.appendChild(bynode);bol.appendChild(sl);
				  conli.appendChild(bol);conli.appendChild(document.createTextNode(' | Date: Today'));
				  u.appendChild(imgli);u.appendChild(conli);l.appendChild(u);
				  if(norm=='undefined' || norm==null)hid.style.display=document.all ? "":"";
				  parobj.parentNode.insertBefore(l, parobj);
				  //parobj.appendChild(l);
				  obj.value='';
				  obj.disabled='false';obj.removeAttribute('disabled');
				  b.disabled='false';b.removeAttribute('disabled');
			}
			else
			{
				obj.disabled='false';obj.removeAttribute('disabled');
				  b.disabled='false';b.removeAttribute('disabled');
			}
		}
		else
		{
			//obj.disabled=false;obj.removeAttribute('disabled');
			//	  b.disabled=false;b.removeAttribute('disabled');	
		}
		
	}
}

function transfer(from, to)
{
	var from_frame = from;var to_input = to;
	this.transferText = function ()
	{
		var content = frames[from_frame].document.body.innerHTML;
		content = content.replace(/<br><a/gi,"<a");	content = content.replace(/<p><\/p>/gi,"<br>");
		var last = content.lastIndexOf('<br>');	if(last != -1){if(content.length-last == 4){content=content.substr(0,last);}}
		document.getElementById(to_input).value = content;
	}
}

function createrich(fw, fh, hb, housing)
{
	
	var f = document.createElement('iframe');f.setAttribute('id', hb+'richedit');f.setAttribute('name', hb+'richedit');
	f.setAttribute('width', fw);f.setAttribute('height', fh);f.frameBorder= 0;f.setAttribute('class', 'rich_frame');
	f.setAttribute('className', 'rich_frame');
	if(navigator.appName=='Microsoft Internet Explorer'){f.document.focus();}else{f.focus();}
	var f_handle = document.getElementById(housing).appendChild(f);
	this.iframe_id = hb + 'richedit';this.hidden_id = hb;
	
	f.contentWindow.document.designMode="on";
	//f.contentWindow.document.designMode="On";
	//f.contentWindow.document.body.contentEditable = "True";
	f.contentWindow.document.open();
	f.contentWindow.document.write('<head><style type="text/css">body{ font-family:verdana; font-size:12px;margin-left: 0px;margin-top: 0px;}p{margin: 0px;}</style></head>');
	f.contentWindow.document.close();
	
	callFunction = new transfer(hb+'richedit', hb);	setInterval("callFunction.transferText()", 1000);	
}

function apply()
{
	var inp=document.getElementById('app-form').getElementsByTagName('input');var con=1;
	for(var i=0;i<inp.length;i++){if(inp[i].type=='text'){if(trim(inp[i].value)==''){con=0;}}}
	if(con==1){document.getElementById('aform').submit();}
}

function newUploadBox()
{
	var f=document.getElementById('form-main');var hvalue=document.getElementById('question_no').value;newvalue=parseInt(hvalue)+1;document.getElementById('question_no').value=newvalue;
	var uls=document.getElementById('ul-main1').innerHTML; var newul=document.createElement('ul');newul.innerHTML=uls; var uls2=f.insertBefore(newul, document.getElementById('submitul'));uls2.id='ul-main'+newvalue;var umu=uls2.getElementsByTagName('*'); var umul=umu.length; 
	for(var i=0;i<umul;i++)	{ var w=umu[i].nodeName;if(w=='textarea' || w=='TEXTAREA'){ var x='main_text_'+newvalue; umu[i].name=x;umu[i].id=x;}if(w=='select' || w=='SELECT'){ umu[i].name='answer'+newvalue;umu[i].id='answer'+newvalue;}if(w=='input' || w=='INPUT'){ var n=umu[i].name; var npos=n.lastIndexOf('_'); if(npos != -1){npos+=1;umu[i].name=n.substr(0, npos)+newvalue;umu[i].id=n.substr(0, npos)+newvalue}}}
}

function errorul(oldnode, txt)
{
	var prevobj=document.getElementById('robinho8549754');if(prevobj == null || typeof prevobj == 'undefined'){createerror(oldnode, txt);
	} else {oldnode.removeChild(prevobj.parentNode); createerror(oldnode, txt);}
}

function createerror(oldnode, txt)
{
	var newul=document.createElement('ul');var newli=document.createElement('li');newli.setAttribute('class','red');newli.setAttribute('className','red');newli.setAttribute('id', 'robinho8549754');newli.appendChild(document.createTextNode(txt));newul.appendChild(newli); var uls2=oldnode.insertBefore(newul, document.getElementById('submitul'));	
}

function uploadTest()
{
	var f=document.getElementById('form-main');var hvalue=document.getElementById('question_no').value;var correct=1;var content=0;
	for(var i=1;i<=hvalue;i++)
	{	
		var con=trim(document.getElementById('main_text_'+i).value);
		if(con!=''){content=1; for(var j=1;j<5;j++){if(trim(document.getElementById('option_'+j+'_'+i).value)==''){correct=0;errorul(f, 'Ensure that all question have four options');return;}}
		if(document.getElementById('answer'+i).value=='- Select Right Option -'){correct=0;errorul(f, 'Ensure you select the right option for each question');return;}}
	}if(document.getElementById('testclass').value=='- Select Class -'){correct=0;errorul(f, 'Select A Class To Upload Questions');return;}if(content ==1){f.submit();}else{errorul(f, 'You have not typed any question');}
}

function runload()
{
	var anch=document.getElementById('nav').getElementsByTagName('a');var obj=new slide();obj.create();
	var j=anch.length; for (var i=0;i<j;i++) {anch[i].o=obj;anch[i].i=i;anch[i].onmouseover=function(){ this.o.intro(this.o,this.i);}; anch[i].onmouseout=function(){this.o.outro(this.o);};}
	
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(runload);

