//
//  jsrsClient.js - javascript remote scripting client include
//  
//  Author:  Brent Ashley [jsrs@megahuge.com]
//
//  make asynchronous remote calls to server without client page refresh
//
//  see license.txt for copyright and license information

/*
see history.txt for full history
2.0  26 Jul 2001 - added POST capability for IE/MOZ
2.2  10 Aug 2003 - added Opera support
2.3(beta)  10 Oct 2003 - added Konqueror support - **needs more testing**
*/

// callback pool needs global scope

function ckeckpassword()
{
if(document.getElementById("user_pass").value!=document.getElementById("conf_pass").value)
{
alert("Both Password is not Same.");
document.getElementById("user_pass").value="";
document.getElementById("conf_pass").value="";
document.getElementById("user_pass").focus();
}
if(document.getElementById("fatname").value=="" && document.getElementById("motname").value=="")
{
alert("Please Enter Parent Name");
}
}


function checkdetail(str)
{
var errmsg = new Array();
 errmsg['cmbDate[]']="Please Select the Date";
 errmsg['fname']="Please Enter the First Name";
 errmsg['lname']="Please Enter the Last Name";
 errmsg['email']="Please Enter the E-Mail Id";
 errmsg['vendor_id']="Please Select the Vendor Name";
 errmsg['regularmeal[]']="Please Select the Regular Meal";
 errmsg['meal[]']="Please Select the Meal Name";
 errmsg['dob']="Please Enter Date of Birth";
 errmsg['user_name']="Please Enter the User Name";
 errmsg['user_pass']="Please Enter the Password";
 errmsg['oldpass']="Please Enter the Old Password";
 errmsg['conf_pass']="Please Enter the Confirm password";
 errmsg['grade']="Please Select the Grade/Section";
 errmsg['stu_name']="Please Select the Student's Name";
 errmsg['pid']="Please Select the Category";
 errmsg['category']="Please Select the Category";
 errmsg['title']="Please Enter the Title";
 errmsg['txtImage']="Please Browse And Select the Image";
 errmsg['image_title']="Please Enter the Image Title";
 errmsg['content_en']="Please Enter the Content";
 errmsg['menu_text']="Please Enter the Page Name";
 errmsg['gradesection']="Please Select the Grade/Section";
 errmsg['subject_id']="Please Select the Subject";
 errmsg['expiary_date']="Please Enter Assignment Due Date";
 errmsg['number']="Please Select No. of Upload Files";
 errmsg['uploadfile[]']="Please Browse the Uploaded Files";
 errmsg['point']="Please Enter the Point";
 
element= str.split(',');
for(i=0 ; i<element.length ; i++)
{
if(document.getElementById(element[i]).value==""){
alert(errmsg[element[i]]);
document.getElementById(element[i]).focus();
return false;
}
}
return true;

}


var stuid=new Array();
var name1= new Array();
var price=new Array();
function addSkills(parameter){
var nameid= document.getElementById("stu_name").value;
var name = nameid.split("#");
	str="<table bgcolor='#f1f1f1' cellspacing='1' width='100%' boder='0'>";
	if(parameter=="Add") {
		document.getElementById("studid").value=parameter;
		stuid[stuid.length]=name[0];
		name1[name1.length]=name[1];
		//year[year.length]=document.getElementById("year").options[document.getElementById("year").selectedIndex].text;
		//price[price.length]=document.getElementById("business").value;
		//document.getElementById("business").value="";
		//document.getElementById("year").selectedIndex=0;
	}
	for(i=0;i<stuid.length;i++) {
		str=str+"<tr border='0'>";
		str=str+"<td bgcolor='#FFFFFF' width='75%' align='left' valign='top' class='verlowtext'>"+name1[i]+"</td>";
		str=str+"<td bgcolor='#FFFFFF' width='25%'align='left' valign='top' class='textbold'><input type='hidden' name='student_id[]' value='"+stuid[i]+"'><a onClick='deleteSkills("+i+")' class='title' onMouseOver=Javascript:this.style.cursor='hand'>Delete</a></td></tr>";
	}
	str=str+"</table>";
	document.getElementById("child").innerHTML=str;
}
//delete emp skills
function deleteSkills(i) {
	stuid.splice(i,1);
	name1.splice(i,1);
	price.splice(i,1);
	str="<table bgcolor='#f1f1f1' cellspacing='1'  width='100%' boder='0'>";
	for(i=0;i<stuid.length;i++) {
		str=str+"<tr>";
		str=str+"<td bgcolor='#FFFFFF' width='75%' align='left' valign='top' class='verlowtext'>"+name1[i]+"</td>";
		str=str+"<td bgcolor='#FFFFFF' width='25%'align='left' valign='top' class='textbold'><input type='hidden' name='student_id[]' value='"+stuid[i]+"'><a onClick='deleteSkills("+i+")' class='title' onMouseOver=Javascript:this.style.cursor='hand'>Delete</a></td></tr>";
	}
	str=str+"</table>";
	document.getElementById("child").innerHTML=str;
}



function createForm(num,previous) {
j = 1+(previous)/1;
//numprevious = ((previous)/1)+1;
//alert(number);
data = "<table width=100%>"; 
inter = "'";
//alert(num+"  --  "+j+"  --  "+previous);
var bgcol="";
if (num < 6 && num > -1) {
for (i=j; i <= num; i++) {
if (i < 6) spaces="      ";
else spaces="    ";
if(i%2==0){bgcol="#F1F1F1";}
else {bgcol="#FFFFFF";}
data = data + "<tr class='more' bgcolor="+bgcol+"><td width='100' align ='right'></td><td width='130' class='title'>File Title</td><td width='150' align ='left'>"+spaces+"<input type='text' size=20 name='filetitle["+i+"]' ></td><td width='25' align='center'><b>:</b></td><td>"+spaces
+ "<input type='File' size=20 name='uploadfile["+i+"]' id='uploadfile[]'></td></tr>";
}
data = data + "</table>";
if (document.layers) {
document.layers.cust.document.write(data);
document.layers.cust.document.close();
}
else {
if (document.all) {
cust.innerHTML = data;
      }
   }
}
else {
window.alert("Please select up to 5 entries.");
   }
}


function roundNumber(rnum) {

	var rlength = 2; // The number of decimal places to round to
	if (rnum > 8191 && rnum < 10485) {
		rnum = rnum-5000;
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
		//return newnumber;
		//newnumber = newnumber+5000;
	} else {
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
		//return newnumber;
	}
	return newnumber;
	
}


function chkusername(str)
{
//country =document.frmeditphoto.pid.value;
//alert(country);
jsrsExecute( "find_user_name.php", showuser, "selectuser" ,str);
}
function showuser(states)
{
if(states=="yes")
{
alert("This User Name Already Exist. Please Change The User Name.");
document.getElementById("user_name").value="";
document.getElementById("user_name").focus();
}

}


function listimage()
{
country =document.frmeditphoto.pid.value;
//alert(country);
jsrsExecute( "getimagelist.php", showlist, "selectimagelist" ,country);
}
function showlist(states)
{
jsrsExecute ("foo.php", callBack, "blankFunc");
cust.innerHTML = states;
}


function listnews()
{
country =document.frmeditnews.pid.value;
//alert(country);
jsrsExecute( "getnewslist.php", shownewslist, "selectnewslist" ,country);
}
function shownewslist(states)
{

jsrsExecute ("foo.php", callBack, "blankFunc");
cust.innerHTML = states;
}

function getteaassignment()
{
subjectid = document.uploadassform.subject_id.value;
gradeid = document.uploadassform.gradesection.value;
teacherid = document.uploadassform.teacher_id.value;
country= subjectid+'#'+gradeid+'#'+teacherid;
//ert(country);
jsrsExecute("admin/getteachassiglist.php", showteachasslist, "selectteachasslist" ,country);



}

function showteachasslist(states)
{
//alert(states);
jsrsExecute ("foo.php", callBack, "blankFunc");
cust.innerHTML = states;
}


function studentlistcombo()
{
country = document.getElementById('grade').value
//alert(country);
jsrsExecute( "getstudentlistcombo.php", showstudentlistcombo, "selectstudentlistcombo" ,country);
}
function showstudentlistcombo(states)
{
//alert(states);
jsrsExecute ("foo.php", callBack, "blankFunc");
//document.addinvoicesfrm.vendor_id.options.length=0;
options = states.split("|");
document.form1.searchstudent.options[0] = new Option("All","0");
	for( var i = 0; i < options.length; i++ )
	if (options[i]!="")
	{
	nameValue=options[i].split("~"); 
	
	document.form1.searchstudent.options[i+1] = new Option(nameValue[1], nameValue[0]); 
	}

}


function studentlist()
{
country = document.getElementById('grade').value
//alert(country);
jsrsExecute( "getstudentlist.php", showstudentlist, "selectstudentlist" ,country);
}
function showstudentlist(states)
{

jsrsExecute ("foo.php", callBack, "blankFunc");
cust.innerHTML = states;
}
function studentlistfront()
{
country = document.getElementById('grade').value
//alert(country);
jsrsExecute( "admin/getstudentlistfront.php", showstudentlist, "selectstudentlist" ,country);
}
function showstudentlist(states)
{

jsrsExecute ("foo.php", callBack, "blankFunc");
cust.innerHTML = states;
}



function LTrim(str){
	if (str==null){return null;}
	for(var i=0;str.charAt(i)==" ";i++);
	return str.substring(i,str.length);
	}
function RTrim(str){
	if (str==null){return null;}
	for(var i=str.length-1;str.charAt(i)==" ";i--);
	return str.substring(0,i+1);
	}
function Trim(str)
{
return LTrim(RTrim(str));
}


function callBack() {}

 



var jsrsContextPoolSize = 0;
var jsrsContextMaxPool = 100;
var jsrsContextPool = new Array();
var jsrsBrowser = jsrsBrowserSniff();
var jsrsPOST = true;
var containerName;

// constructor for context object
function jsrsContextObj( contextID ){
  
  // properties
  this.id = contextID;
  this.busy = true;
  this.callback = null;
  this.container = contextCreateContainer( contextID );
  
  // methods
  this.GET = contextGET;
  this.POST = contextPOST;
  this.getPayload = contextGetPayload;
  this.setVisibility = contextSetVisibility;
}

//  method functions are not privately scoped 
//  because Netscape's debugger chokes on private functions
function contextCreateContainer( containerName ){
  // creates hidden container to receive server data 
  var container;
  switch( jsrsBrowser ) {
    case 'NS':
      container = new Layer(100);
      container.name = containerName;
      container.visibility = 'hidden';
      container.clip.width = 100;
      container.clip.height = 100;
      break;
    
    case 'IE':
      document.body.insertAdjacentHTML( "afterBegin", '<span id="SPAN' + containerName + '"></span>' );
      var span = document.all( "SPAN" + containerName );
      var html = '<iframe name="' + containerName + '" src=""></iframe>';
      span.innerHTML = html;
      span.style.display = 'none';
      container = window.frames[ containerName ];
      break;
      
    case 'MOZ':  
      var span = document.createElement('SPAN');
      span.id = "SPAN" + containerName;
      document.body.appendChild( span );
      var iframe = document.createElement('IFRAME');
      iframe.name = containerName;
      iframe.id = containerName;
      span.appendChild( iframe );
      container = iframe;
      break;

    case 'OPR':  
      var span = document.createElement('SPAN');
      span.id = "SPAN" + containerName;
      document.body.appendChild( span );
      var iframe = document.createElement('IFRAME');
      iframe.name = containerName;
      iframe.id = containerName;
      span.appendChild( iframe );
      container = iframe;
      break;

    case 'KONQ':  
      var span = document.createElement('SPAN');
      span.id = "SPAN" + containerName;
      document.body.appendChild( span );
      var iframe = document.createElement('IFRAME');
      iframe.name = containerName;
      iframe.id = containerName;
      span.appendChild( iframe );
      container = iframe;

      // Needs to be hidden for Konqueror, otherwise it'll appear on the page
      span.style.display = none;
      iframe.style.display = none;
      iframe.style.visibility = hidden;
      iframe.height = 0;
      iframe.width = 0;

      break;
  }
  return container;
}

function contextPOST( rsPage, func, parms ){

  var d = new Date();
  var unique = d.getTime() + '' + Math.floor(1000 * Math.random());
  var doc = (jsrsBrowser == "IE" ) ? this.container.document : this.container.contentDocument;
  doc.open();
  doc.write('<html><body>');
  doc.write('<form name="jsrsForm" method="post" target="" ');
  doc.write(' action="' + rsPage + '?U=' + unique + '">');
  doc.write('<input type="hidden" name="C" value="' + this.id + '">');

  // func and parms are optional
  if (func != null){
  doc.write('<input type="hidden" name="F" value="' + func + '">');

    if (parms != null){
      if (typeof(parms) == "string"){
        // single parameter
        doc.write( '<input type="hidden" name="P0" '
                 + 'value="[' + jsrsEscapeQQ(parms) + ']">');
      } else {
        // assume parms is array of strings
        for( var i=0; i < parms.length; i++ ){
          doc.write( '<input type="hidden" name="P' + i + '" '
                   + 'value="[' + jsrsEscapeQQ(parms[i]) + ']">');
        }
      } // parm type
    } // parms
  } // func

  doc.write('</form></body></html>');
  doc.close();
  doc.forms['jsrsForm'].submit();
}

function contextGET( rsPage, func, parms ){

  // build URL to call
  var URL = rsPage;

  // always send context
  URL += "?C=" + this.id;

  // func and parms are optional
  if (func != null){
    URL += "&F=" + escape(func);

    if (parms != null){
      if (typeof(parms) == "string"){
        // single parameter
        URL += "&P0=[" + escape(parms+'') + "]";
      } else {
        // assume parms is array of strings
        for( var i=0; i < parms.length; i++ ){
          URL += "&P" + i + "=[" + escape(parms[i]+'') + "]";
        }
      } // parm type
    } // parms
  } // func

  // unique string to defeat cache
  var d = new Date();
  URL += "&U=" + d.getTime();
 
  // make the call
  switch( jsrsBrowser ) {
    case 'NS':
      this.container.src = URL;
      break;
    case 'IE':
      this.container.document.location.replace(URL);
      break;
    case 'MOZ':
      this.container.src = '';
      this.container.src = URL; 
      break;
    case 'OPR':
      this.container.src = '';
      this.container.src = URL; 
      break;
    case 'KONQ':
      this.container.src = '';
      this.container.src = URL; 
      break;
  }  
}

function contextGetPayload(){
  switch( jsrsBrowser ) {
    case 'NS':
      return this.container.document.forms['jsrs_Form'].elements['jsrs_Payload'].value;
    case 'IE':
      return this.container.document.forms['jsrs_Form']['jsrs_Payload'].value;
    case 'MOZ':
      return window.frames[this.container.name].document.forms['jsrs_Form']['jsrs_Payload'].value; 
    case 'OPR':
      var textElement = window.frames[this.container.name].document.getElementById("jsrs_Payload");
    case 'KONQ':
      var textElement = window.frames[this.container.name].document.getElementById("jsrs_Payload");
      return textElement.value;
  }  
}

function contextSetVisibility( vis ){
  switch( jsrsBrowser ) {
    case 'NS':
      this.container.visibility = (vis)? 'show' : 'hidden';
      break;
    case 'IE':
      document.all("SPAN" + this.id ).style.display = (vis)? '' : 'none';
      break;
    case 'MOZ':
      document.getElementById("SPAN" + this.id).style.visibility = (vis)? '' : 'hidden';
    case 'OPR':
      document.getElementById("SPAN" + this.id).style.visibility = (vis)? '' : 'hidden';
      this.container.width = (vis)? 250 : 0;
      this.container.height = (vis)? 100 : 0;
      break;
  }  
}

// end of context constructor

function jsrsGetContextID(){
  var contextObj;
  for (var i = 1; i <= jsrsContextPoolSize; i++){
    contextObj = jsrsContextPool[ 'jsrs' + i ];
    if ( !contextObj.busy ){
      contextObj.busy = true;      
      return contextObj.id;
    }
  }
  // if we got here, there are no existing free contexts
  if ( jsrsContextPoolSize <= jsrsContextMaxPool ){
    // create new context
    var contextID = "jsrs" + (jsrsContextPoolSize + 1);
    jsrsContextPool[ contextID ] = new jsrsContextObj( contextID );
    jsrsContextPoolSize++;
    return contextID;
  } else {
    alert( "jsrs Error:  context pool full" );
    return null;
  }
}

function jsrsExecute( rspage, callback, func, parms, visibility ){
  // call a server routine from client code
  //
  // rspage      - href to asp file
  // callback    - function to call on return 
  //               or null if no return needed
  //               (passes returned string to callback)
  // func        - sub or function name  to call
  // parm        - string parameter to function
  //               or array of string parameters if more than one
  // visibility  - optional boolean to make container visible for debugging

  // get context
  var contextObj = jsrsContextPool[ jsrsGetContextID() ];
  contextObj.callback = callback;

  var vis = (visibility == null)? false : visibility;
  contextObj.setVisibility( vis );

  if ( jsrsPOST && ((jsrsBrowser == 'IE') || (jsrsBrowser == 'MOZ'))){
    contextObj.POST( rspage, func, parms );
  } else {
    contextObj.GET( rspage, func, parms );
  }  
  
  return contextObj.id;
}

function jsrsLoaded( contextID ){
  // get context object and invoke callback
  var contextObj = jsrsContextPool[ contextID ];
  if( contextObj.callback != null){
    contextObj.callback( jsrsUnescape( contextObj.getPayload() ), contextID );
  }
  // clean up and return context to pool
  contextObj.callback = null;
  contextObj.busy = false;
}

function jsrsError( contextID, str ){
  alert( unescape(str) );
  jsrsContextPool[ contextID ].busy = false
}

function jsrsEscapeQQ( thing ){
  return thing.replace(/'"'/g, '\\"');
}

function jsrsUnescape( str ){
  // payload has slashes escaped with whacks
  return str.replace( /\\\//g, "/" );
}

function jsrsBrowserSniff(){
  if (document.layers) return "NS";
  if (document.all) {
		// But is it really IE?
		// convert all characters to lowercase to simplify testing
		var agt=navigator.userAgent.toLowerCase();
		var is_opera = (agt.indexOf("opera") != -1);
		var is_konq = (agt.indexOf("konqueror") != -1);
		if(is_opera) {
			return "OPR";
		} else {
			if(is_konq) {
				return "KONQ";
			} else {
				// Really is IE
				return "IE";
			}
		}
  }
  if (document.getElementById) return "MOZ";
  return "OTHER";
}

/////////////////////////////////////////////////
//
// user functions

function jsrsArrayFromString( s, delim ){
  // rebuild an array returned from server as string
  // optional delimiter defaults to ~
  var d = (delim == null)? '~' : delim;
  return s.split(d);
}

function jsrsDebugInfo(){
  // use for debugging by attaching to f1 (works with IE)
  // with onHelp = "return jsrsDebugInfo();" in the body tag
  var doc = window.open().document;
  doc.open;
  doc.write( 'Pool Size: ' + jsrsContextPoolSize + '<br><font face="arial" size="2"><b>' );
  for( var i in jsrsContextPool ){
    var contextObj = jsrsContextPool[i];
    doc.write( '<hr>' + contextObj.id + ' : ' + (contextObj.busy ? 'busy' : 'available') + '<br>');
    doc.write( contextObj.container.document.location.pathname + '<br>');
    doc.write( contextObj.container.document.location.search + '<br>');
    doc.write( '<table border="1"><tr><td>' + contextObj.container.document.body.innerHTML + '</td></tr></table>' );
  }
  doc.write('</table>');
  doc.close();
  return false;
}

