﻿
/**
Description: The Class would encapsulate all ajax operations and dynamically creating of drop down Lists
author: Zubair Masoodi
Dated: 21/10/2008
*/
function ExamDatesLoader(outerContainerID,rootURL)
{
   
   this._outerContainer = document.getElementById(outerContainerID);
   this._rootURL  = rootURL + '/tests/' ;


}

//var to hold an instance of the object which is to be populated
ExamDatesLoader.prototype._outerContainer = undefined;

//where to load the data from
ExamDatesLoader.prototype._pageURL = "";

//where to load the data from
ExamDatesLoader.prototype._actionURL = "Info/TestActions_Ajax.aspx?";

//var to hold an instance of the XMLHTTPRequest object
ExamDatesLoader.prototype._request = undefined;

//var to hold an instance input exam string
ExamDatesLoader.prototype._exams = undefined;

// Var for holding the exam string
ExamDatesLoader.prototype._examsList = undefined;

// Variable to hold the current year
ExamDatesLoader.prototype._currentYear = undefined;

// Variable to hold all added exam of the user
ExamDatesLoader.prototype._userExams = undefined;

// Variable to hold the current year
ExamDatesLoader.prototype._requestId = undefined;

ExamDatesLoader.prototype._rootURL = "";

//_pageURLs array holds the list of months
ExamDatesLoader.prototype._months = [
       "January","February","March","April","May","June","July","August","September","October","November","December"
 ];



/**
*	Tells the class to load its data and render the results.
*/

ExamDatesLoader.prototype.load = function(_strExams,currentYear)
{
    
    if(this._outerContainer == null)
        this._outerContainer = document.getElementById("divExamDates");
     
    this._outerContainer.innerHTML = "";
    this._currentYear = currentYear;
    this._examsList = _strExams;
    this._exams = _strExams.split(",");
        for (var i=0; i < this._exams.length;++i){
            this.gener(i);
        }
   this._outerContainer.innerHTML= this._outerContainer.innerHTML;
   
   
}
ExamDatesLoader.prototype._getexams = function()
{
    var param="Action=getExams"
    this._requestId = "1";
    this._pageURL =  this._actionURL + param;
    this.send();
     
}

ExamDatesLoader.prototype.appendtable = function(i)
{
    
	var div = document.createElement("div");
	var tbl = div.appendChild(document.createElement("table"));
	var tr = tbl.appendChild(document.createElement("tr"));
	
	

	var tdExamTitle = tr.appendChild(document.createElement("td"));
	tdExamTitle.width = "100px;";
	var txtExamNode = document.createTextNode(this._exams[i]);
	    tdExamTitle.appendChild(txtExamNode);
	 
	var tdmonthList = tr.appendChild(document.createElement("td"));
	
	var tdyearList = tr.appendChild(document.createElement("td"));
	
	this._outerContainer.appendChild(div);
	var oSelect = document.createElement("select");
	
    oSelect.name = "dd_month_" + this._exams[i] ;
    oSelect.id = "dd_month_" + this._exams[i] ;
   
   
	var oOption = null;
	var t0 = null;

    for (var k=0; k <this._months.length;++k){

       oOption = document.createElement("option");
	    t0 = document.createTextNode(this._months[k]);
	    oOption.setAttribute("value", k+1);
	    oOption.appendChild(t0);
	    oSelect.appendChild(oOption);
    }
   
    tdmonthList.appendChild(oSelect);
    
    var oSelectyear = document.createElement("select");
    oSelectyear.name = "dd_year_" + this._exams[i] ;
    oSelectyear.id = "dd_year_" + this._exams[i] ;
    
    var year =  this._currentYear;
    for (var j = 0; j < 10 ; ++j){

        oOption = document.createElement("option");
	    t0 = document.createTextNode(year);
	    oOption.setAttribute("value", year);
	    oOption.appendChild(t0);
	    oSelectyear.appendChild(oOption);
	    year =  parseInt(year) + 1;
    }
    
    tdyearList.appendChild(oSelectyear);
    
   
}
ExamDatesLoader.prototype.gener = function(i)
{
   
    
	var outerdiv = document.createElement("div");
	outerdiv.setAttribute("class","outerdiv_dyn");
	
	var titlediv = outerdiv.appendChild(document.createElement("div"));
	titlediv.setAttribute("class","titlediv_dyn");
    
	var monthdiv = outerdiv.appendChild(document.createElement("div"));
	monthdiv.setAttribute("class","monyear_dyn");
	
	
	var yeardiv = outerdiv.appendChild(document.createElement("div"));
	yeardiv.setAttribute("class","monyear_dyn");
	
	
	
	this._outerContainer.appendChild(outerdiv);
	
	var oTitle = document.createTextNode(this._exams[i]);
	
	titlediv.appendChild(oTitle);
	
	//alert(1);
	 
	
	
	
	var oSelect = document.createElement("select");
	
    oSelect.name = "dd_month_" + this._exams[i] ;
    oSelect.id = "dd_month_" + this._exams[i] ;
   
   
	var oOption = null;
	var t0 = null;

    for (var k=0; k <this._months.length;++k){

       oOption = document.createElement("option");
	    t0 = document.createTextNode(this._months[k]);
	    oOption.setAttribute("value", k+1);
	    oOption.appendChild(t0);
	    oSelect.appendChild(oOption);
    }
   
    monthdiv.appendChild(oSelect);
    
    var oSelectyear = document.createElement("select");
    oSelectyear.name = "dd_year_" + this._exams[i] ;
    oSelectyear.id = "dd_year_" + this._exams[i] ;
    
    var year =  this._currentYear;
    //alert(year);
    for (var j = 0; j < 10 ; ++j){

        oOption = document.createElement("option");
	    t0 = document.createTextNode(year);
	    oOption.setAttribute("value", year);
	    oOption.appendChild(t0);
	    oSelectyear.appendChild(oOption);
	    year =  parseInt(year) + 1;
    }
     yeardiv.appendChild(oSelectyear);
    
    
    this._outerContainer.appendChild(outerdiv);
    
    //document.getElementById("txtHTML").value = this._outerContainer.innerHTML;
   
}

	function test()
	{
	    alert(ml);
	}
	

ExamDatesLoader.prototype._addDates = function()
{
 
 
   var strmonth= "dd_month_";
   var stryear= "dd_year_";
   var param="";
   var objMonth;
   var objYear;
   for (var i=0; i < this._exams.length;i++){
   
      if(document.getElementById(strmonth + this._exams[i])!= null ) {
            objMonth = document.getElementById(strmonth + this._exams[i]);
            objYear = document.getElementById(stryear + this._exams[i]);
            param += this._exams[i] + "~" + objMonth.value + "~" + objYear.value + "|"; 
        }
         
        }
   this._requestId = "1";
   this._pageURL =  this._actionURL + "Action=addExam&exam=" + param.substring(0,param.length-1);
   this._hideDiv();
   this.send();
  
}
ExamDatesLoader.prototype._add = function(_strExams)
{
  _strExams = _strExams.replace(/,/g, "|");
   this._requestId = "1";
   this._pageURL =  this._actionURL + "Action=addExam&exam=" + _strExams;
   this.send();
 
    
}
ExamDatesLoader.prototype._hideDiv = function()
{
  $find("ModalPopup_AddExamDates").hide();     
}

ExamDatesLoader.prototype.send = function()
{
	  
   this._request = this._getXMLHTTPRequest();
	var _this = this;
	this._request.onreadystatechange = function(){_this._onData()};
	this._request.open("GET", this._generateDataUrl(), true);
    this._request.send(null);
	
}

/*
*	Tells the class to act whenever any error is generated
*/
ExamDatesLoader.prototype.onError = function(status,statusText)
{
       alert("Error in Ajax Response");
}
/**
*	Tells the class to load its data and render the results.
*/



//renders the entire widget
ExamDatesLoader.prototype._render = function(_responseXML)
{
	  
	  if(this._requestId == "1")
	    this._bindExams(_responseXML);
	  
	
}

//renders the entire widget
ExamDatesLoader.prototype._bindExams = function(_responseXML)
{
	 
	
	var xmlDoc = _responseXML;
	if(xmlDoc.getElementsByTagName("Exams")[0].childNodes[0]!=null)
	{
	    var _strUserExams= xmlDoc.getElementsByTagName("Exams")[0].childNodes[0].data;
    	
      
        if(_strUserExams != undefined && _strUserExams != ""){
            this._userExams = _strUserExams.split(",");
            
            var _html =   '<div id="cld" style="float: left; width:320px; ">';
                for(var k=0; k < this._userExams.length; k++)
                 {
                    var a = this._rootURL + this._userExams[k];
                
                   _html += "<a href='"+a+"' id=\"" + k + "\"  class=\"tag_search2blue\">" + this._userExams[k] + "</a> &nbsp;&nbsp ";
                  }
                           
	         _html += '</div>';
        	
	       
	         document.getElementById("spexams").innerHTML = _html;
    	   
    	     
	    } 
	}

  
	
}
//Gets the Url on which the request would be made

ExamDatesLoader.prototype._generateDataUrl = function()
{
	return this._pageURL;
}
//callback for when the data is loaded from the server
ExamDatesLoader.prototype._onData = function()
{

	if(this._request.readyState == 4)
	{
			
		if(this._request.status == "200")
		{
					
			this._render(this._request.responseXML);

		}
		else
		{
			//check if an error callback handler has been defined
			if(this.onError != undefined)
			{
				//pass an object to the callback handler with info
				//about the error
				
				//this.onError({status:this._request.status,
						//statusText:this._request.statusText});
			}
		}

		//clean up
		delete this._request;
	}
}


//returns an XMLHTTPRequest instance (based on browser)
ExamDatesLoader.prototype._getXMLHTTPRequest = function()
{
	var xmlHttp;
	try
	{
		xmlHttp = new ActiveXObject("Msxml2.XMLHttp");
	}
	catch(e)
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
		}
		catch(e2)
		{
		}
	}

	if(xmlHttp == undefined && (typeof XMLHttpRequest != 'undefined'))
	{
		xmlHttp = new XMLHttpRequest();
	}

	return xmlHttp;
}


function txtChange(id)
{
    alert(document.getElementById(id).value);
}