//---------------------------------------------------------------------------
// General js functions
//---------------------------------------------------------------------------
 
//check if production or test site
function checkProdSite()
{
	//var prod_site = false;
	var ProdSiteList = new Array();
	ProdSiteList[0] = "www.ontariocolleges.ca";
	ProdSiteList[1] = "www.collegesdelontario.ca";
	ProdSiteList[2] = "www.ocas.ca";
	ProdSiteList[3] = "www.saco.ca";

	 for (var i=0;i<ProdSiteList.length;i++){
		 var reg_http = new RegExp(ProdSiteList[i]);
		 if( reg_http.test(window.location.href) ) {
			 //alert(window.location.href);
			 return true;
			 i=ProdSiteList.length;
		 }
	 }	
	 
	 return false;
}


//This function calls the external IntelliResponse
//Open launch in a new window.
function displayIRPage(request,question_id,question_text,isAskUs,lang)
{
	var url = "";
	//var source_params = "&source=4";
	var source_params = "&source=100";
	lang = lang.toLowerCase();

	if (lang=="fr")
	{
		url = "http://demandezlenous.collegesdelontario.ca/fre/index.jsp?";
	}
	else
	{
		url = "http://askus.ontariocolleges.ca/index.jsp?";
	}

	var iwidth=690;
	var iheight=680;
	var itop = 100;
	var ileft = 150;
	var parm = "top="+itop+",left="+ileft+",scrollbars=yes,resizable=yes,directories=no,status=no,toolbar=no,location=no,menubar=no,width="+iwidth +",height="+ iheight;

	if (request!="TopQuestionsRequest"){

		if (isAskUs){
			source_params = "&source=1";
			url += "question="+document.getElementById("questionInput").value;
		}
		else{
			url += "question="+question_text;
		}
		
		if (question_id!=-1){
			url += "&id="+question_id;
		}

		url += "&requestType="+request;
		url += source_params;
	}
	else{
		url += "question=";
		url += "&requestType="+request;
	}

	//alert(url);
	var openWin = window.open(url,"IRWindow",parm);
	//if (openWin) openWin.focus();
}

//Manage Cookie
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
//END Manage Cookie



function readMore(idx) {
	 
	 var elname_moretext = "readmore";
	 if (idx!=null) elname_moretext+=idx;
	 
	 var elname_morelink = "morelink";
	 if (idx!=null) elname_morelink+=idx;
	 
	 var elname_lesslink = "lesslink";
	 if (idx!=null) elname_lesslink+=idx;
	 
	 var ckie_name = "readMoreCkie";
	 if (idx!=null) ckie_name+=idx;
	 
	 var el_moretext = document.getElementById(elname_moretext);
	 var el_morelink = document.getElementById(elname_morelink);
	 var el_lesslink = document.getElementById(elname_lesslink);
	 
	 if ( el_moretext.style.display != "none" ) {
	 //hide more text
		el_moretext.style.display = 'none';
		el_morelink.style.display = '';
		el_lesslink.style.display = 'none';		
		
		eraseCookie(ckie_name);
	 }
	 else {
	 //display more text
		el_moretext.style.display = '';
		el_morelink.style.display = 'none';
		el_lesslink.style.display = '';
		
		createCookie(ckie_name,elname_moretext,365);
	 }
}



//Trim function
function LTrim(str){
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...
      var j=0, i = s.length;
      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
	 j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

function RTrim(str){
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");
   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)..
      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
	 i--;

      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }
   return s;
}

function Trim(str){
   return RTrim(LTrim(str));
}
//END Trim function

function goAskUs(event,lang) {//alert(event.type);
	var openpg = false;
	if (Trim(document.getElementById("questionInput").value).length>0){
	  if (event.type == "click"){//on click
		openpg=true;
	  } else if (event && event.keyCode == 13){//on enter
		openpg=true;
	  }
	}	
	if (openpg)
	{
		displayIRPage('NormalRequest',-1,'',true,lang);
		return false;	
	}
}


//---------------------------------------------------------------------------
// To handle mouse click events
//---------------------------------------------------------------------------

function mouseOver(eid,url)
{
	document.getElementById(eid).src=url;
}
function mouseOut(eid,url)
{
	document.getElementById(eid).src=url;
}


//---------------------------------------------------------------------------
// BROWSERHAWK
//---------------------------------------------------------------------------
var BROWSERHAWK_ON_FLAG = true; //to enable browserHawk test

//---------------------------------------------------------------------------
// BROWSERHAWK custom functions
//---------------------------------------------------------------------------
 
//implement logic for browserHawk check
function retHelpNotAvail(lang)
{
   // In the highly unlikely event that your script cannot load the RET library due to a
   // network failure, then this function will be called when you attempt to show the
   // RET results, instead of the results being displayed. This is not an expected scenario
   // but best practices are to have a backup plan in place just in case...
   var msg = "";
   if (lang==2) {
	   msg = "Your browser does not meet the requirements. ";
	   msg += "For more help type word 'browser' in Ask Us feature on top of our page and make sure that your browser allow cookies. \n";
	   msg += "Please correct these settings and try again. Our detailed help system is momentarily unavailable, try again shortly.";
   } else {
	   msg = "Nous ne supportons pas le type de navigateur utilisé. ";
	   msg += "Pour plus d'information entrer le mot 'navigateur' dans l'option Demandezle-nous en haut de notre page et assurez vous que votre navigateur accepte les témoins (cookies). \n";
	   msg += "Veuillez effectuer les corrections nécessaires et essayez a nouveau. Notre système d'aide détaillé n'est actuellement pas disponible, veuillez re-essayer très prochainement.";	
   }
}

function bhawkCheck(lang)
{//lang=1 for english; lang=2 for french
   // check to see if the browser fails any core requirements
   // set this function to return true if we decide to turn bhawkCheck OFF
   // return true; // enable this line, to turn browser Hawk OFF
  
   if (

	// Check your core requirements here. Note that this does NOT have to match what is your RET help rules.
	// Rather, this should be the core requirements so that if one of more of these are not met, then the
	// user is shown your RET results.

	// This is just an example - you can use whatever values you want. Note that the values in parenthesis
	// are default values to use in the highly unlikely event that a network issue prevents the BH library
	// from loading or the browser property is not able to be tested. In such a case, the default value
	// is returned and the user is allowed to pass. You can change this logic of course to handle that case
	// however you wish.


	bhawk.sessioncookies() == false ||
	bhawk.javascriptenabled() == false

      )
   {
        // show browser help instead of allowing user to continue, since we know they don't meet the site's requirements


        // TODO: Set this to the caption you want displayed at the top of the browser help window
        var caption = "";
        
        if (lang==2)
        	caption = "Vérification de votre navigateur...";
        else
        	caption = "Checking your browser....";

        // TODO - optional: If you set up a CNAME record that maps whatever.yourdomain.com to ret.browserhawk.com (highly recommended) then replace
        // ret.browserhawk.com in the variable defined below with the fully qualified domain name used in your CNAME record (i.e. var bhtgDomain = 'http://browserhelp.mycompany.com'; )
        var bhtgDomain = "http://ret.browserhawk.com";

        // TODO: Set your BHTG account ID here:
        var bhtgAccountID_en = 'ocas';
        var bhtgAccountID_fr = 'ocas-fr';

	try {
		if (lang==2){
			bhawkret.showResults(caption, bhtgDomain + '/bhtg/ret/browsercheck.aspx?acct=' + bhtgAccountID_fr + '&customtest=ocas_fr&customstyle=ocas&TB_iframe=false&height=510&width=580');
		} else
			bhawkret.showResults(caption, bhtgDomain + '/bhtg/ret/browsercheck.aspx?acct=' + bhtgAccountID_en + '&customtest=ocas_en&customstyle=ocas&TB_iframe=false&height=510&width=580');
	 }
	 catch(e) {
	      // If we are here then there was an unexpected error when attempting to show the help. Place code here that you want to
	      // to execute in this situation.  For this demo we call the retHelpNotAvail() function which displays a simple alert to the user
	      // saying that the help system is unavailable and to try again shortly.
	      retHelpNotAvail(lang);
	 }
   
   
        return false; // browser check failed
   }
   else
	return true; // browser check passed
}


