﻿/* 
    This file use to maintain common javascript functions 
    Please make sure to put "/" separators to separate each n every function     
    Created by Vins
*/

/////////////////////////Get Client Browsers Inner Width

// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
// you may copy these functions but please keep the copyright notice as well

      function pageHeight() 
      {
         var res = 0;
         if( window.innerHeight != null)
          res=window.innerHeight 
          else
          {
            if( document.documentElement && document.documentElement.clientHeight)
               res = document.documentElement.clientHeight 
            else
            {
               if( document.body != null)
                  res = document.body.clientHeight 
               else
                  res =0;
             }
          }
          //alert("Height:"+res);
          return res;

      }
      
      
      
      
    function pageWidth() 
      {
         var res = 0;
         if( window.innerWidth != null)
          res=window.innerWidth 
          else
          {
            if( document.documentElement && document.documentElement.clientWidth)
               res = document.documentElement.clientWidth 
            else
            {
               if( document.body != null)
                  res = document.body.Width 
               else
                  res =0;
             }
          }
          
          return res;

      }   


   function SetMiddleHeight()
	{
	   if (document.getElementById("divMiddle") == "undefined")
		{
		   setTimeout(SetMiddleHeight(),500);
      }
		else
		{
		   var height = pageHeight();
				  
			if (height > 400)
			{
			   height = height - 206;
				document.getElementById("divMiddle").style.height = height + "px";
         }
      }
			  
   }






    ////////////////////////////////////////////////////////////////////////////////////////////////
    //////////////// Event capturing ///////////////////////////////////////////////////////////////

    //////New delimeters//////////
    //var colDelim = String.fromCharCode(200);
    var rowDelim = String.fromCharCode(201);
    var dataDelim = String.fromCharCode(202);
    //////End - New delimeters//////////

    /* This will build an array for store user events */

    var eventCount=0; // use for keep track of no of events
    var eventArr = new Array();
    
    
    // build event list
    function BuildEventList(sEventValue, sEventName)
    {
        if((document.frm.hdnAnswerEvents!=null)&&(document.frm.hdnAnswerEvents.value!=""))
        {
        
            var exisSteps="";
            var arrSteps= new Array();
            var step = "";
            var arrStepVals = new Array();
            
            var exisSteps = document.frm.hdnAnswerEvents.value;
            document.frm.hdnAnswerEvents.value = "";
            
            arrSteps = exisSteps.split(rowDelim);
            if(arrSteps.length>0)
            {

                for(i=0;i<arrSteps.length;i++)
                {
                    step = arrSteps[i];
                    arrStepVals = step.split(dataDelim);
                    
                    if(arrStepVals.length>0)
                    {
                        eventArr[eventCount] = new Array(4); // add new array to the existing array to store data
                        // add values to the newly added array
                        eventArr[eventCount][0] = arrStepVals[0];//sEventName; // add event Name
                        eventArr[eventCount][1] = arrStepVals[1];//eventCount+1; // add event count 
                        if (arrStepVals[2]!= "")                //Last Commented 24-04-2007 By  KK ****** if(sEventValue!="")
                        {
                            eventArr[eventCount][2] = arrStepVals[2];//sEventValue; // This may null in some cases.
                        }
                        else
                        {
                            eventArr[eventCount][2] = "";//" "; // This may null in some cases.
                        }
                        eventArr[eventCount][3] = ":"; // Delimeter.
                        // change eventCount 
                        eventCount = eventCount + 1;
                    }                        
                }
            }
            
            if ((sEventName!="")&&(sEventValue!="")) //Last Commented 24-04-2007 By KK === only If condition 
            {
                eventArr[eventCount] = new Array(4); // add new array to the existing array to store data
                // add values to the newly added array
                eventArr[eventCount][0] = sEventName; // add event Name
                eventArr[eventCount][1] = eventCount+1; // add event count 
                if(sEventValue!="")
                {
                    eventArr[eventCount][2] = sEventValue; // This may null in some cases.
                }
                else
                {
                    eventArr[eventCount][2] = "";//" "; // This may null in some cases.
                }
                eventArr[eventCount][3] = ":"; // Delimeter.
                // change eventCount 
                eventCount = eventCount + 1;       
            }     
        }
        else
        {
            eventArr[eventCount] = new Array(4); // add new array to the existing array to store data
            // add values to the newly added array
            eventArr[eventCount][0] = sEventName; // add event Name
            eventArr[eventCount][1] = eventCount+1; // add event count 
            if(sEventValue!="")
            {
                eventArr[eventCount][2] = sEventValue; // This may null in some cases.
            }
            else
            {
                eventArr[eventCount][2] = "";//" "; // This may null in some cases.
            }
            eventArr[eventCount][3] = ":"; // Delimeter.
            // change eventCount 
            eventCount = eventCount + 1;
        }
    } 

    
    //////////////// End - Event capturing ///////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////////////////////////////////
    //////////////// Event capturing ////  Changed - delimeters/////////////////////////////////////////////////

    /* This will build an array for store user events */

    var eventCount=0; // use for keep track of no of events
    var eventArr = new Array();
    
    
    // build event list
    function BuildEventListNew(sEventValue, sEventName)
    {
        if((document.frm.hdnAnswerEvents!=null)&&(document.frm.hdnAnswerEvents.value!=""))
        {
            // Existing answers available.
            // add existing values to array and clear hidden field
            
            var exisSteps="";
            var arrSteps= new Array();
            var step = "";
            var arrStepVals = new Array();
            
            var exisSteps = document.frm.hdnAnswerEvents.value;
            document.frm.hdnAnswerEvents.value = "";
            
            arrSteps = exisSteps.split(rowDelim);
            if(arrSteps.length>0)
            {
                for(i=0;i<arrSteps.length;i++)
                {
                    step = arrSteps[i];
                    arrStepVals = step.split(dataDelim);
                    
                    if(arrStepVals.length>0)
                    {
                        // add values to the newly added array
                        eventArr[eventArr.length-1] = new Array(4);
                        eventArr[eventArr.length-1][0] = arrStepVals[0] + dataDelim; // add event Name
                        eventArr[eventArr.length-1][1] = arrStepVals[1]+ dataDelim; // add event count 
                        if(sEventValue!="")
                        {
                            eventArr[eventArr.length-1][2] = arrStepVals[2] + dataDelim; // This may null in some cases.
                        }
                        else
                        {
                            eventArr[eventArr.length-1][2] = "" + dataDelim; // This may null in some cases.
                        }
                        eventArr[eventArr.length-1][3] = rowDelim; // Delimeter.             
                    }
                }
            }
            
            document.frm.hdnAnswerEvents.value="";
        }

        if(eventArr.length>0)
            eventCount = eventArr.length;
    
        eventArr[eventCount] = new Array(4); // add new array to the existing array to store data
        // add values to the newly added array
        eventArr[eventCount][0] = sEventName + dataDelim; // add event Name
        eventArr[eventCount][1] = eventCount+1 + dataDelim; // add event count 
        if(sEventValue!="")
        {
            eventArr[eventCount][2] = sEventValue + dataDelim; // This may null in some cases.
        }
        else
        {
            eventArr[eventCount][2] = "" + dataDelim; // This may null in some cases.
        }
        eventArr[eventCount][3] = rowDelim; // Delimeter.
        // change eventCount 
        eventCount = eventCount + 1;
    } 

    
    //////////////// End - Event capturing (New) ///////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////



    ////////////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////// Add List items to a given ListBox //////////////////////////////////////
    
    /* This will add List items to a given listbox. 
       functions required following parameters to do the operation
            ctrl - Control (ListBox, which need to add items)
            sItemText = Item text which need to show.
   */
    function addToListBox(ctrl,sItemText)
    {
        var iCnt = 0;
        iCnt = document.frm.ListBox1.length;
        
        var opObj = new Option(sItemText,iCnt);  // (text, val)
        ctrl.options[iCnt]=opObj;
        return false;
    }
    /////////////////////// End - Add List items to a given ListBox ////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    
    
    /// validations *****************************************************************************



function ValidateDate(strDate)
{	
	var yearLastExp = new RegExp("^\\s*(\\d{2})([/])(\\d{2})\\2((\\d{4}))\\s*$");
	
	var m = strDate.match(yearLastExp);
	
	if (m == null) {
		return false;
	}

	var day, month, year;

    month = m[1];
	day = m[3];
   
	year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10))
	
	month -= 1;

	var date = new Date(year,month,day);	
	
	return  (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate());

}

/*****************************************************************************/
/****	NAME    : Getdate												  ****/
/****	PURPOSE : To return JavaScript Date object by passing string date ****/
/****			  Note :: Date sting should dd/mm/yyyy                    ****/
/****	INPUT PARAMETERS  : << strDate >>  Date String              	  ****/
/****	AUTHOR			  : << Ajantha Jayasinghe >>  					  ****/
/*****************************************************************************/
function Getdate(strDate)
{
	if (ValidateDate(strDate))
	{
		var yearLastExp = new RegExp("^\\s*(\\d{2})([/])(\\d{2})\\2((\\d{4}))\\s*$");
	
		var m = strDate.match(yearLastExp);
		
		if (m == null) {
			return false;
		}

		var day, month, year;

		month = m[1];
		day = m[3];
		
		
		year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10))
		month -= 1;
		
		var date = new Date(year,month, day );
		
		return date;
	}
	else
	{
		alert("Invalid Date");
		
		return null;
	}
}

/*****************************************************************************/
/****	NAME    : RangeValidate											  ****/
/****	PURPOSE : This function checks whether the entered date is valid  ****/
/****			  and is in European format								  ****/
/****			  Note :: Date sting should dd/mm/yyyy                    ****/
/****	RETURN :: If strFromDate <= strToDate teturn 0                    ****/
/****			  If strFromDate > strToDate teturn 1					  ****/
/****			  If Error teturn -1					                  ****/
/****	INPUT PARAMETERS  : << strFromDate >> From Date text field     	  ****/
/****	INPUT PARAMETERS  : << strToDate >> To Date text field     	      ****/
/****	AUTHOR			  : << Ajantha Jayasinghe >>  					  ****/
/*****************************************************************************/
function RangeValidate(strFromDate, strToDate)
{
	var intStatus;
	
	if (ValidateDate(strFromDate) && ValidateDate(strToDate))
	{		
		if(Getdate(strFromDate) <= Getdate(strToDate))
		{
			intStatus = 0;
		}
		else
		{
			intStatus = 1;
		}
	}
	else
	{
		intStatus = -1;
	}	
	
	return intStatus;
}

 /// End validations *****************************************************************************
 
 function ReplaceStr(strSource, strSearch, strReplace)
    {
        var reg; // = /strSearch/gi;
        reg = new RegExp(strSearch, "gi");
        newstr =strSource.replace(reg, strReplace);
        
        return newstr;
    }
    
 function CheckQuestions(questionDDL)
 {
    if (questionDDL.value.length > 0)
    {
        return true;
    }
    else
    {
        alert("Please select a question.");
        return false;
    }
 
 }
 
 
 
 //Added By KK
 //To Handle the Mutiple Request or Multiple Postback
 // Dec 03 2007
  var B1;
  var B2;
  var B3;
  
  function HandlePostback(b1,b2,b3)
  {
    B1 = b1;
    B2 = b2;
    B3 = b3;
    setTimeout('DisableButtons()',1);
    return true;
  }
         
  function DisableButtons()
  {
    try
    {
        B1.disabled = "disabled"; 
        B2.disabled = "disabled"; 
        B3.disabled = "disabled"; 
        //    
//    document.getElementById("cmdNext").disabled = "disabled"; 
//    document.getElementById("cmdPrev").disabled = "disabled"; 
//    document.getElementById("cmdEndSec").disabled = "disabled"; 
        document.getElementById("cmdRestore").disabled = "disabled"; 
    
    }catch(error)
    {
        //alert("Error occurred:" + error);
    }
  }
 
 function ShowHelp()
 {
   window.showModalDialog("AssessmentHelp.aspx?ID=" + generateGuid(),'User Help','toolbar:no;dialogWidth:800px;dialogHeight:600px;top:200;status:no;scrollbars:yes;resizable:yes');
   return false;
 }
 
   function generateGuid()
   {
      var result, i, j;
      result = '';
      for(j=0; j<32; j++)
      {
         if( j == 8 || j == 12|| j == 16|| j == 20)
            result = result + '-';
         i = Math.floor(Math.random()*16).toString(16).toUpperCase();
         result = result + i;
      }
      return result;
   }
