var isLogin=0 ;
var content;

function loads(left,top,width){
	if (!left) left = "25%"
	if (!top) top = "30%"
	if (!width) width="50%"
	return load = 	'<div style ="position:relative; left:'+left+'; top:'+top+'; border:1px solid #cdcdcd; width: '+width+'; margin: 0px; padding: 0px ">'+
					'<div>'+
					'<table cellpadding="5" cellspacing="5">'+
					'<tr valign="top">'+
					'<td><img src="images/loading.gif" width="30"> &nbsp;</td>'+
					'<td>MIKA: Loading<br/><b> Please wait...</b></td>'+
					'</tr>'+
					'</table>'+
					'</div>'+
					'</div>';
}

/* load page */
function loadPage(id, uri,opt,load,sendData) {
	content = document.getElementById(id);	
    var widgets = dijit.findWidgets(content);
	widgets.forEach(function(w){
		w.destroyRecursive();
	});
	
	if (!load)
		content.innerHTML = loads('42%','200px','168px');
	else
		content.innerHTML=load;
	
	if (opt==1){
		isLogin=1;
	}
    
	return dojo.xhrPost( {
		url : uri,
		handleAs : "text",
		load : displayPage,
		content : sendData
	});
	
	//alert(sendData);
}

/* display page */
function displayPage(data){	
	/*dijit.registry.forEach(function(w) {
		//w.destroyRecursive();
		alert(w.parentNode);
	});*/
	
	

	content.style.display = "block";
	if (data.indexOf("Failed") != -1){		
		content.innerHTML=data;	
	}
	else{		
		content.innerHTML=data;
	}	
	
	dojo.parser.parse(content);

	if (isLogin==1){
		document.getElementById("txtuser").focus();
		isLogin=0;
	}  
}

/*
function loadsend(){
    dojo.require("dijit.form.Button");
    dojo.require("dijit.form.TextBox");
    dojo.require("dijit.form.CheckBox");

    var button = dijit.byId("submitButton");
    
    dojo.connect(button, "onClick", function(event) {
        //Stop the submit event since we want to control form submission.
        event.preventDefault();
        event.stopPropagation();

        //The parameters to pass to xhrPost, the form, how to handle it, and the callbacks.
        //Note that there isn't a url passed.  xhrPost will extract the url to call from the form's
        //'action' attribute.  You could also leave off the action attribute and set the url of the xhrPost object
        //either should work.
        var xhrArgs = {
            form: dojo.byId("frm"),
            handleAs: "text",
            load: function(data) {
                dojo.byId("response").innerHTML = "Form posted.";
            },
            error: function(error) {
                //We'll 404 in the demo, but that's okay.  We don't have a 'postIt' service on the
                //docs server.
                dojo.byId("response").innerHTML = "Form posted.";
            }
        }
        //Call the asynchronous xhrPost
        dojo.byId("response").innerHTML = "Form being sent..."
        var deferred = dojo.xhrPost(xhrArgs);
    });

}

function loadPage2(id, uri,opt,load) {
    content = document.getElementById(id);

    if (!load)
        content.innerHTML = loads('42%','200px','168px');
    else
        content.innerHTML=load;
    if (opt==1){
        login=1;
    }

    dojo.xhrPost( {
        form : dojo.ById("frm"),
        url : uri,
        handleAs : "text",
        load : displayPage2
    });
}

// display page 
function displayPage2(data){
    content.style.display = "block";
    if (data.indexOf("Failed") != -1)
        content.innerHTML=data;
    else content.innerHTML=data;
    loadsend()
    
}

*/


function ah (id,uri){
  alert("Execute of node ");
  var xhrArgs = {
    url: uri ,
    handleAs: "text",
    load: function(data) {
      dojo.byId(id).innerHTML = data;
    //window.location = data;
    },
    error: function(error) {
      dojo.byId(id).innerHTML = "<p>Error in loading...</p>";
    }
  }
  dojo.byId(id).innerHTML = "<p>Loading...</p>";
  dojo.xhrPost(xhrArgs);
}

function alerts(field)
{
  for (i = 0; i < field.length; i++)
    field[i].checked = false ;
}

function show(id,uri) {
  Id = document.getElementById(id);
  if(Id.style.display == "none"){
    Id.style.display = "";
    var r = Math.floor(Math.random()*10000000000);
    var f = uri+r
    loadPage(id, f,0,loads());
  }
  else {
//        Id.style.display = "none";
}
}

/* content page */
var content;

/* loading display */



/*
 testing
 */

dojo.require("dijit.form.Button");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.form.CheckBox");

function sendForm() {
  var button = dijit.byId("submitButton");

  dojo.connect(button, "onClick", function(event) {
    //Stop the submit event since we want to control form submission.
    event.preventDefault();
    event.stopPropagation();

    //The parameters to pass to xhrPost, the form, how to handle it, and the callbacks.
    //Note that there isn't a url passed.  xhrPost will extract the url to call from the form's
    //'action' attribute.  You could also leave off the action attribute and set the url of the xhrPost object
    //either should work.
    var xhrArgs = {
      form: dojo.byId("myform"),
      handleAs: "text",
      load: function(data) {
        dojo.byId("response").innerHTML = "Form posted.";
      },
      error: function(error) {
        //We'll 404 in the demo, but that's okay.  We don't have a 'postIt' service on the
        //docs server.
        dojo.byId("response").innerHTML = "Form posted.";
      }
    }
    //Call the asynchronous xhrPost
    dojo.byId("response").innerHTML = "Form being sent..."
    var deferred = dojo.xhrPost(xhrArgs);
  });
}

/*
         *end testing
         */






function sendPage(id, uri) {
/*content = document.getElementById(id);
    content.innerHTML = loading();
    dojo.xhrPost( {
        url : uri,
        handleAs : "text",
        load : displayPage
    });
    */
}




/*
var OutputDiv;
function GetEmployeeList(URL) {
    OutputDiv = document.getElementById("EmployeeList");
    OutputDiv.innerHTML = "<h2>Loading...</h2>";
    dojo.xhrPost( {
        url : URL,
        handleAs : "text",
        load : Display
    });
}

function Display(DATA) {
    OutputDiv.style.display = "block";
    if (DATA.indexOf("Failed") != -1) {
        OutputDiv.className = "Warning";
        OutputDiv.innerHTML = DATA;
    } else {
        OutputDiv.innerHTML = DATA;
    }
}

window.onload = function() {
    //	GetEmployeeList("/index/show");
	
    }


function logouts(){
    //    loadPage('content', '/index/logout');
    //    x=document.getElementById("toolbar");
    //    if (x)
    //        alert(x.id);
    alert("testing")
}
*/
totsks = 0;
function t(chk,data){
  //    if (chk.checked)
  //        totsks +=parseInt(data,10);
  //    else
  //        totsks -=parseInt(data,10);
  //    document.getElementById("totsks").innerHTML=totsks
  sks = parseInt(document.getElementById("totsks").innerHTML,10);
  if (chk.checked)
    sks +=parseInt(data,10);
  else
    sks -=parseInt(data,10);
  document.getElementById("totsks").innerHTML=sks
}

    


function clear(){
  totsks=0;
  document.getElementById("totsks").innerHTML=0
}

function showa(id)
{
  id = document.getElementById(id);
  if(id.style.display == "")
  {
    id.style.display = "none";
  }
  else
  {
    id.style.display = "";
  }
  //    alert("a")
  //    var x = 'saran.php?'+Math.random(); loadPage('100', x)
  document.getElementById(id).innerHTML="testing";

}
function setcontainer(w,h,color,id)
{
  div = document.getElementById(id).style;
  div.position="absolute";
  div.width = w;
  div.height = h;
  div.margin = "0px";
  div.right = "5px";
  div.bottom = "55px";
  div.backgroundColor = color;
  div.borderStyle = "solid";
  div.borderWidth = "thin";
  div.borderColor = "#003399";
}



// post form

function postForm(id) {
  //The parameters to pass to xhrPost, the message, and the url to send it to
  //Also, how to handle the return and callbacks.
  var xhrArgs = {
    url: "dojopost.php",
    postData: "Some random text",
    handleAs: "text",
    load: function(data) {
      dojo.byId(id).innerHTML = "Message posted.";
    },
    error: function(error) {
      //We'll 404 in the demo, but that's okay.  We don't have a 'postIt' service on the
      //docs server.
      dojo.byId(id).innerHTML = "Message posted error.";
    }
  }
  dojo.byId(id).innerHTML = "Message being sent..."
  //Call the asynchronous xhrPost
  var deferred = dojo.xhrPost(xhrArgs);
}

function showKRS(nim){
  //alert(nim)
  //alert('./wali/isikrs.php?id=0'+nim)
  //loadPage('setujuisi', './wali/setujuisi.php?id=0'+nim)

  loadPage('setujuisi', './wali/isikrs.php?id=0'+nim)
}

