var domain = "";
var session_id = get_cookie('session_id');

var institutions = init_array();
var courses = init_array();

get_array('institutions', institutions);
get_array('courses', courses);

function shortlistPage(sitebase) {

        if(document.getElementById('shortlist-heading')) {    
        	document.getElementById('shortlist-heading').innerHTML = 'You have shortlisted ' + (next_entry(courses) -1) + ' Courses and ' + (next_entry(institutions) -1) + ' Institutions';        
		if(get_cookie('user_logged_in') != 0 || next_entry(institutions) != 1){
		
			document.getElementById('tabs-no-content').style.display = 'none';
			document.getElementById('shortlist-empty').style.display = 'none';
			document.getElementById('shortlist-register').style.display = 'none';
			document.getElementById('tabs2').style.display = 'block';
			document.getElementById('shortlist-not-empty').style.display = 'block';
		
		        if(get_cookie('user_logged_in') != 0) {  
                		document.getElementById('register-shortlist').style.display = 'none';
                	}
                	
                	if(next_entry(institutions) == 1 || get_cookie('user_logged_in') == 0) {
                 		document.getElementById('email-shortlist').style.display = 'none';
                	}


			ahah("/webservice/shortlist.php?userid=" + get_cookie('session_id') + "&i=" + get_cookie('institutions') + "&c=" + get_cookie('courses') + "&sitebase=" + sitebase, "shortlist-details");
			document.getElementById('shortlist-details').style.display='block';

		} else {
		
		  	document.getElementById('tabs-no-content').style.display='block';
			document.getElementById('shortlist-empty').style.display='block';
			document.getElementById('shortlist-register').style.display='block';
			document.getElementById('email-shortlist').style.display='none';
		}
        }

}

function updateShortlistBox() {

	document.getElementById('view-institutions-link').innerHTML = (next_entry(institutions) -1) + ' institutions';
	document.getElementById('view-courses-link').innerHTML =  (next_entry(courses) -1) + ' courses';

	if(next_entry(institutions) != 1 || get_cookie("user_logged_in") == 1) {
		document.getElementById('shortlist-box-empty').style.display = 'none';
		document.getElementById('shortlist-box-items').style.display = 'block';
	}

        if(document.getElementById('shortlist-heading')) {    
        	document.getElementById('shortlist-heading').innerHTML = 'You have shortlisted ' + (next_entry(courses) -1) + ' Courses and ' + (next_entry(institutions) -1) + ' Institutions';     
        }

}

function setShortlistLinks(inst_id) {


    if(inst_id) {

	if(document.getElementById('addInst' + inst_id) && document.getElementById('removeInst' + inst_id)) {
	    document.getElementById('addInst' + inst_id).style.display='none';
	    document.getElementById('removeInst' + inst_id).style.display='none';
  	    if(document.getElementById('gotoShortlist' + inst_id)){
  	    	document.getElementById('gotoShortlist' + inst_id).style.display='block';
	    }
	}
	if(document.getElementById('suitability-addInst' + inst_id) && document.getElementById('suitability-removeInst' + inst_id)) {
	    document.getElementById('suitability-addInst' + inst_id).style.display='none';
	    document.getElementById('suitability-removeInst' + inst_id).style.display='none';
  	    if(document.getElementById('suitability-gotoShortlist' + inst_id)) {
  	    	document.getElementById('suitability-gotoShortlist' + inst_id).style.display='block';	    
	    }
	}

    } else {

	for (var i=1; i<next_entry(institutions); i++) {
	  if(document.getElementById('addInst' + institutions[i]) && document.getElementById('removeInst' + institutions[i])) {
	      document.getElementById('addInst' + institutions[i]).style.display='none';
	      document.getElementById('removeInst' + institutions[i]).style.display='block';
	      
	      if(document.getElementById('gotoShortlist' + institutions[i])) {
	      	      document.getElementById('removeInst' + institutions[i]).style.display='none';
	      	      document.getElementById('gotoShortlist' + institutions[i]).style.display='block';
	      }
	      
	  }
	  if(document.getElementById('suitability-addInst' + institutions[i]) && document.getElementById('suitability-removeInst' + institutions[i])) {
	      document.getElementById('suitability-addInst' + institutions[i]).style.display='none';
	      document.getElementById('suitability-removeInst' + institutions[i]).style.display='block';
	      
	      if(document.getElementById('suitability-gotoShortlist' + institutions[i])) {
	      	      document.getElementById('suitability-removeInst' + institutions[i]).style.display='none';
	      	      document.getElementById('suitability-gotoShortlist' + institutions[i]).style.display='block';
	      }	      
	  }

	}

     }

     for (var i=1; i<next_entry(courses); i++) {
          if(document.getElementById('addCourse' + courses[i]) && document.getElementById('removeCourse' + courses[i])) {
              document.getElementById('addCourse' + courses[i]).style.display='none';
              document.getElementById('removeCourse' + courses[i]).style.display='block';
          }
	  if(document.getElementById('suitability-addCourse' + courses[i]) && document.getElementById('suitability-removeCourse' + courses[i]) ) {
	      document.getElementById('suitability-addCourse' + courses[i]).style.display = 'none';
	      document.getElementById('suitability-removeCourse' + courses[i]).style.display = 'block';   
	  }          
     }
}

function addToShortlist(inst_id, course_id, node_id) {
     var notInArray = true;
     for (var i=1; i<next_entry(institutions); i++) {
          if(institutions[i] == inst_id) { notInArray = false; }
     }
     
     if(notInArray) {
          institutions.push(inst_id);
          set_array('institutions', institutions, '');
     }

     if(course_id) {
         courses.push(course_id + '-' + node_id);
         set_array('courses', courses, '');
     }

}

function removeInstitution(inst_id) {
   var newArray = init_array();
   var j=1;
   for (var i=1; i<next_entry(institutions); i++) {
      if(institutions[i] != inst_id) { 
         newArray[j] = institutions[i];
         j++;
      }
   }
   institutions = newArray;
   set_array('institutions', institutions, '');
   sendRequest("setdata.php?method=removeFromShortlist&sessionID=" + session_id + "&institutionID=" + inst_id);
}

function removeCourse(course_id, node_id) {
   var newArray = init_array();
   var j = 1;
   for (var i=1; i<next_entry(courses); i++) {
      if(courses[i] != course_id + '-' + node_id) { 
         newArray[j] = courses[i];
         j++
      }
   }
   courses = newArray;
   set_array('courses', courses, '');
   sendRequest("setdata.php?method=removeFromShortlist&sessionID=" + session_id + "&courseID=" + course_id + "&nodeID=" + node_id);
}

function doAddInstitution(inst_id, inst_name, node_id) {
   if(document.getElementById('soObj')){
   	document.getElementById('soObj').addToShortlist(inst_id, inst_name, node_id);           
   }
   if(document.getElementById('addInst' + inst_id) && document.getElementById('removeInst' + inst_id) ){
	document.getElementById('addInst' + inst_id).style.display = 'none';
	document.getElementById('removeInst' + inst_id).style.display = 'block';
   }
   if(document.getElementById('suitability-addInst' + inst_id) && document.getElementById('suitability-removeInst' + inst_id) ){
   	document.getElementById('suitability-addInst' + inst_id).style.display = 'none';
   	document.getElementById('suitability-removeInst' + inst_id).style.display = 'block';   
   }
   addToShortlist(inst_id);
   sendRequest("setdata.php?method=addToShortlist&sessionID=" + session_id + "&institutionID=" + inst_id);
   updateShortlistBox();
   
   urchinTracker ('/add-institution/' + inst_id); 
}

function doRemoveInstitution(inst_id, node_id) {
   if(document.getElementById('soObj')){
   	document.getElementById('soObj').removeInstitution(inst_id);
   }	 
   if(document.getElementById('removeInst' + inst_id) && document.getElementById('addInst' + inst_id)) { 
   	document.getElementById('removeInst' + inst_id).style.display = 'none';
   	document.getElementById('addInst' + inst_id).style.display = 'block';
   }
   if(document.getElementById('suitability-addInst' + inst_id) && document.getElementById('suitability-removeInst' + inst_id) ){
   	document.getElementById('suitability-addInst' + inst_id).style.display = 'block';
   	document.getElementById('suitability-removeInst' + inst_id).style.display = 'none';   
   }
   removeInstitution(inst_id, node_id);
   updateShortlistBox();
   
   urchinTracker ('/remove-institution/' + inst_id);    
}

function doAddCourse(inst_id, inst_name, node_id, course_id, course_name) {
   if(document.getElementById('soObj')){
	document.getElementById('soObj').addToShortlist(inst_id, inst_name, node_id, course_id, course_name);
   }

   if(document.getElementById('addCourse' + course_id + '-' + node_id)) {
      document.getElementById('addCourse' + course_id + '-' + node_id).style.display = 'none';
   }

   if(document.getElementById('removeCourse' + course_id + '-' + node_id)) {
      document.getElementById('removeCourse' + course_id + '-' + node_id).style.display = 'block';
   }

   if(document.getElementById('suitability-addCourse' + course_id + '-' + node_id) && document.getElementById('suitability-removeCourse' + course_id + '-' + node_id) ) {
       document.getElementById('suitability-addCourse' + course_id + '-' + node_id).style.display = 'none';
       document.getElementById('suitability-removeCourse' + course_id + '-' + node_id).style.display = 'block';   
   }
   
   addToShortlist(inst_id, course_id, node_id);
   sendRequest("setdata.php?method=addToShortlist&sessionID=" + session_id + "&institutionID=" + inst_id + "&courseID=" + course_id + "&nodeID=" + node_id);
   updateShortlistBox();
   setShortlistLinks(inst_id);
   
   urchinTracker ('/add-course/' + inst_id + '/' + course_id); 
}

function doRemoveCourse(course_id, node_id) {
   if(document.getElementById('soObj')){
	document.getElementById('soObj').removeCourse(course_id);
   }
   
   if(document.getElementById('removeCourse' + course_id + '-' + node_id) && document.getElementById('addCourse' + course_id + '-' +node_id)) {
   	document.getElementById('removeCourse' + course_id + '-' + node_id).style.display = 'none';
   	document.getElementById('addCourse' + course_id + '-' +node_id).style.display = 'block';
   }
   
   if(document.getElementById('suitability-addCourse' + course_id + '-' + node_id) && document.getElementById('suitability-removeCourse' + course_id + '-' + node_id) ) {
       document.getElementById('suitability-addCourse' + course_id + '-' + node_id).style.display = 'block';
       document.getElementById('suitability-removeCourse' + course_id + '-' + node_id).style.display = 'none';   
   }   
   
   removeCourse(course_id, node_id);
   updateShortlistBox();
   
   urchinTracker ('/remove-course/' + inst_id + '/' + course_id); 
   
}


function addContacted(inst_id) {
   sendRequest("setdata.php?method=addContacted&sessionID=" + get_cookie('session_id') + "&institutionID=" + inst_id); 
}

function removeContacted(inst_id) {
   sendRequest("setdata.php?method=removeContacted&sessionID=" + get_cookie('session_id') + "&institutionID=" + inst_id); 
}


function registerShortlist() {
   document.getElementById('slider0').value = get_cookie('slider0');
   document.getElementById('slider1').value = get_cookie('slider1');
   document.getElementById('slider2').value = get_cookie('slider2');
   document.getElementById('slider3').value = get_cookie('slider3');
   document.getElementById('slider4').value = get_cookie('slider4');   
   document.getElementById('slider5').value = get_cookie('slider5');
   document.getElementById('institutions').value = get_cookie('institutions');
   document.getElementById('courses').value = get_cookie('courses');
   return true;
}


function trackClickthrough(u, i, c, m, y) {

    var sDate = new Date();
    var tracker = new Image();
    tracker.src = "http://www.newzealandeducated.com/tracker/tracker.php?u=" + u + "&t=clickthrough&i=" + i + "&c=" + c + "&m=" + m + "&y=" + y + "&time=" + sDate.getTime();

}















function sendRequest(url) {
    var sDate = new Date();
    url = domain + "/webservice/" + url + "&time=" + sDate.getTime();
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = function() {requestDone();};
        req.open("GET", url, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() {requestDone();};
            req.open("GET", url, true);
            req.send();
        }
    }
} 

function requestDone() {
   // only if req is "loaded"
   if (req.readyState == 4) {
       // only if "OK"
       if (req.status == 200 || req.status == 304) {
           results = req.responseText;
       } else {
           results = "";
       }
   }
}



function ahah(url,target) {
    var sDate = new Date();
    url = url + "&time=" + sDate.getTime();

    document.getElementById(target).innerHTML = 'loading data...';
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = function() {ahahDone(target);};
        req.open("GET", url, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() {ahahDone(target);};
            req.open("GET", url, true);
            req.send();
        }
    }
} 


function ahahDone(target) {
   // only if req is "loaded"
   if (req.readyState == 4) {
       // only if "OK"
       if (req.status == 200 || req.status == 304) {
           results = req.responseText;
           document.getElementById(target).innerHTML = results;
           execJS(target);
       } else {
           document.getElementById(target).innerHTML="ahah error:\n" +
               req.statusText;
       }
   }
}

var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
var bMoz = (navigator.appName == 'Netscape');
function execJS(target) {
  var node = document.getElementById(target);
  var st = node.getElementsByTagName('SCRIPT');
  var strExec;
  for(var i=0;i<st.length; i++) {     
    if (bSaf) {
      strExec = st[i].innerHTML;
    }
    else if (bOpera) {
      strExec = st[i].text;
    }
    else if (bMoz) {
      strExec = st[i].textContent;
    }
    else {
      strExec = st[i].text;
    }
    try {
      eval(strExec.split("<!--").join("").split("-->").join(""));
    } catch(e) {
      alert(e);
    }
  }
}


function setSearch(w1,w2,w3,w4,w5,w6,w7,w8,w9,w10,w11) {

	document.getElementById('weblog1').value = w1;
	document.getElementById('weblog2').value = w2;
	document.getElementById('weblog3').value = w3;
	document.getElementById('weblog4').value = w4;
	document.getElementById('weblog5').value = w5;
	document.getElementById('weblog6').value = w6;
	document.getElementById('weblog7').value = w7;
	document.getElementById('weblog8').value = w8;
	document.getElementById('weblog9').value = w9;
	document.getElementById('weblog10').value = w10;
	document.getElementById('weblog11').value = w11;
	
}


function MM_openBrWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
}
