function handleException(exception)
{  
  /* In FF exception can be a string if it happens
     when opening the xmlHttpRequest.  Gah! */
  if (typeof exception == 'string')
    exception = new Error(exception)
  
  /* If a xmlhttp request is happening in Mozilla and
     the user navigates to another page, then when
     the first request returns a NS_ERROR_NOT_AVAILABLE
     error will be thrown.  So just ignore it. */ 
  if (exception.name == 'NS_ERROR_NOT_AVAILABLE') return
      
  var fullMessage = ''
  var uri = ''
  var stack = ''
  var line = ''
      
  try
  {                    
    /* Don't use exception.toString since the JS spec
       does not require it to provide the error name or message
       (haven't tested to see if it matters though across browsers) */
    fullMessage = exception.name + ': ' + exception.message
    
    uri = exception.fileName
    stack = exception.stack
    
    // Firefox sometimes blows up here
    line = exception.lineNumber
  }
  catch (e)
  {
  
  }                  

  fullMessage += "\n at " + uri + ": " + line
}


function XmlElement(name,content){
    var xml

    if (!content){
        xml='<' + name + '/>'
    }
    else {
        xml='<'+ name + '>' + content + '</' + name + '>'
    }
    return xml
}


function st_drop(id)
{
    var ac         = loginControl.get();
    var testId      = "";
    var title       = "";
    var tt          = "n";
    
    if(id > 7)
    {
        tt = "v";
    }
    
    if(id > 14)
    {
        tt = "l"
    }
    
    switch(tt)
    {
        case "n":
            testId  = id
            title   = "NRT " + testId;
            break;
        case "v":
            testId  = id;
            title   = "VRT " + (parseInt(testId) - 7);
            break;
        case "l":
            testId  = id;
            title   = "LRT " + (parseInt(testId) - 28);
            break;
    }    
    
    
    var test_title = "Test Player > " + title;
        
    if(ac != "-1")
    {
         url = "/player/Player_Frame.aspx?testid=" + testId + "&ac=" + ac;
    }
    else {
        url = "/player/Player_Frame.aspx?testid=" + testId

    }     
    
    Modalbox.show(url, {title: test_title, width: 990, height: 565});
}


function st(tt,id)
{
    var ac         = loginControl.get();
    var testId      = "";
    var title       = "";
    
    switch(tt)
    {
        case "n":
            testId  = id
            title   = "NRT " + testId;
            break;
        case "v":
            testId  = id;
            title   = "VRT " + (parseInt(testId) - 7);
            break;
        case "l":
            testId  = id;
            title   = "LRT " + (parseInt(testId) - 28);
            break;
    }    
    
    
    var test_title = "Test Player > " + title;
        
    if(ac != "-1")
    {
         url = "/player/Player_Frame.aspx?testid=" + testId + "&ac=" + ac;
    }
    else {
        url = "/player/Player_Frame.aspx?testid=" + testId

    }     
    
    Modalbox.show(url, {title: test_title, width: 990, height: 565});
}

function sh(tt,e)
{
    switch(tt)
    {
        case "n":
            e.src="/images/buttons/nrt_h.png";
            break;
        case "v":
            e.src="/images/buttons/vrt_h.png";
            break;
         case "l":
            e.src="/images/buttons/lrt_h.png";
            break;
    } 
}
function hi(tt,i,e)
{
    switch(tt)
    {
        case "n":
            e.src="/images/buttons/nrt" + i + ".png";
            break;
        case "v":
            e.src="/images/buttons/vrt" + i + ".png";
            break;
       case "l":
            e.src="/images/buttons/lrt" + i + ".png";
            break;
    } 
}

function doTestEvent(i,elm)
{
        var testId =    parseInt(elm.getAttribute("testId"));
        var testType =  elm.getAttribute("testType");
        
        
        
        
        
        switch(i)
        { 
            case 1:
      
                    elm.update("&nbsp;");
                    
                break;
            case 2:
            
                if(testId == 1)
                {
                    elm.update(testType.toUpperCase() + " " + testId + " " + "<span style='color:red'>(Free)</span>");
                }
                else
                {
                    elm.update(testType.toUpperCase() + " " + testId);
                }
                
                break;     
            case 3:
                    if(testType == "nrt")
                    {
                        TakeTest(testId,'null');
                    }
                    else
                    {
                        testId = testId + 7;
                        TakeTest(testId,'null');
                    }
                break;  
        }
}
    

function TakeTestOver()
{

    
    
    var elmList = '#' + 'HOME_PACKAGE_LIST' + ' ' + 'td';

	$$(elmList).each( function(e) 
	
	{
	    if(e.getAttribute("testType"))
	    {
	        if(e.getAttribute("testType") == "lrt")
	        {
	        }
	        else
	        {		
	            e.observe('mouseover', function(event){ doTestEvent(1,this);}); 
	            e.observe('mouseout', function(event){ doTestEvent(2,this);}); 
	            e.observe('click', function(event){ doTestEvent(3,this);}); 
	        }
	    }		    
	});	 
						

}

function ShowFAQ(id)
{

    var hideFaq = "";
    var hideFaqH = "";
    var showFaq = "";
    var showFaqH = "";
    
    
    for(var i = 1 ; i < 9 ; i++)
    {
        hideFaq= "faq_" + i;
        hideFaqH= "faq_h_" + i;
        
        $(hideFaqH).removeClassName("accordion_toggle_active");
        $(hideFaq).hide();
    }
    
    showFaq = "faq_" + id;
    showFaqH = "faq_h_" + id;
    
    $(showFaq).show();
    $(showFaqH).addClassName("accordion_toggle_active");
}

function removeTextNode(n) { 
  var rmnode = new Array();
  var v = "";
  var pos = 0;

  // Get all the #text nodes 
  for(var i = 0 ; i < n.childNodes.length ; i++) {
    if(n.childNodes[i].nodeType == 3) { 
       v = n.childNodes[i].nodeValue.replace(/^\s*|\s*$/g,""); 
       if(v.length == 0) rmnode[pos++] = n.childNodes[i]; 
    } 
    
    if(n.childNodes[i].nodeType == 1) removeTextNode(n.childNodes[i]); 
  } 

  // Remove the text nodes 
  for(i = 0 ; i < rmnode.length ; i++) { 
    try {n.removeChild(rmnode[i]);} 
    catch(e) {} 
  } 
  
  return n;
}

function getTextVersion(XMLnode) {
    var text;
    try {
        //serialization to string DOM Browser
        var serializer = new XMLSerializer();
        text = serializer.serializeToString(XMLnode);
    } catch(e) {
        // serialization of an XML to String (IE only)
        text = XMLnode.xml;
    }
    
}

// returns a named value from the querystring
function querystring(name)   
{
   var tmp = ( location.search.substring(1) );
   var i   = tmp.toUpperCase().indexOf(name.toUpperCase()+"=");

   if ( i >= 0 )
   {
      tmp = tmp.substring( name.length+i+1 );
      i = tmp.indexOf("&");
      return unescape( tmp = tmp.substring( 0, (i>=0) ? i : tmp.length ));
   }

   return("");
}

function doPurchase(){

    if(parseInt($("drpPackageList").value) > 0)
    {
        var url = "/purchase/default.aspx?pid=" + $("drpPackageList").value;
    
        document.location.href=url;
    }
}

function ShowProgress(){

    url = "/loading.htm";
	        
	Modalbox.show(url, {title: 'Enter your login details', width: 305, height: 150});
}

function HideProgress(){
}



var loginControl        = new LoginControl();
var templateControl     = new TemplateControl();
var ratingControl       = new RatingControl();
var packageControl;
var scoreControl;
var testControl;



function TakeTest(t,ac){

    if((t == 0) || (t== ""))
    {
        alert("Please select a test");
    }
    else
    {
        var testId = t
        var title = "";
        var url;
        var displayId = "";
        var ac1 = loginControl.get();
        
        if(parseInt(testId) > 7)
        {
           title = "VRT "; 
        }
        else
        {
            title = "NRT ";
        }
        
        if(parseInt(testId) > 7)
        { 
            displayId = parseInt(testId) -7;
        }
        else
        {
            displayId = testId;
        }
         
        var test_title = "Test Player > " + title + displayId ;
        
        if(ac != "-1"){
             url = "/player/Player_Frame.aspx?testid=" + testId + "&ac=" + ac1;
        }
        else {
            url = "/player/Player_Frame.aspx?testid=" + testId

        }     
        
        Modalbox.show(url, {title: test_title, width: 990, height: 565});
    }
    
}

function SendToFriend()
{
    Modalbox.show('/functions/sendtofriend.aspx', {title: 'Send To Friend', width: 500, height: 340});
}

function IsAuthenticated(p){

    isAuthenticated = false;
    
    if(loginControl.get() != null)
    {
    
        switch(loginControl.get())
        {
            case "":
                    isAuthenticated = false;
                break;
            case "-1":
                    isAuthenticated = false;
                break;
            default:
                    isAuthenticated = true;
                break;
        }
    }
    return isAuthenticated;
}

function PageLoader(p){

    var showProfilePage = false;
    
        if(IsAuthenticated(p) == true)
        {
            showProfilePage = true;
        }  
        
        switch(p)
        {
            case "HOME_PAGE":
                $(p).addClassName("selected");
                ratingControl.get(p); 
                timer2.start();
            break; 
            
            case "RESULT_PAGE":
                var ts          = querystring("ts");
                var sn          = querystring("sn");
                scoreControl    = new ScoreControl(loginControl.get());
                scoreControl.loadScore(ts,sn);
                ratingControl.get(p);
                break;
            case "PROFILE_PAGE":
                 if(showProfilePage)
                 {
                    testControl     = new TestControl(loginControl.get());
                    testControl.get(1);
                 }
                 break;
            case "ASSESSMENTCENTRES_PAGE":
            case "ECONOMICIMPACT_PAGE":
            case "CONSULTATION_PAGE":
            case "PSYCHOMETRICS_PAGE":
            case "HOWWECANHELP_PAGE":
            case "CQI_PAGE":
                $(p).addClassName("selected");
                ratingControl.get(p); 
                break; 
            default:
                break;
        }

}

function getViewPort()
{
//    var docViewPort = new CookieJar({
//        expires:3600, // seconds
//        path: '/'
//    });
//    
//    var dimensions = document.viewport.getDimensions();

//    docViewPort.put(dimensions);
}





