/****************************************************************************************************************
 *                                            Learned Solutions  (C) 2002                                       *
 *                                                                                                              *
 *             Utility Functions for learning space content                                                     *
 *   Created 5 Sept 2002 - Kee Fai Foong                                                                        *
 *                                                                                                              *
 *                                                                                                              *
 *                                                                                                              *
 *                                                                                                              *
 *                                                                                                              *
 ****************************************************************************************************************/



/****************************************************************************************************************
 *  Server Constants                                                                                            *
 ****************************************************************************************************************/
var serverName = "http://www.learnedsolutions.com"; // the fully qualified name of the server on the internet/intranet
var serverDomain = "learnedsolutions.com"; // required to save settings accross all load balanced servers
var demoContentServer = "http://eflautest.learnedsolutions.com.au/"; // the external efl server hosting demo-content
var geoLocationServer = "http://webhost.learnedsolutions.com.au/geoLocation/redirect.php"; // the external server hosting the ip to location script



// Shouldn't need to modify below
// global declarations

init();



/****************************************************************************************************************
 *  Initialization routines                                                                                     *
 ****************************************************************************************************************/

function init() {


//insertScript('/javascript/constants.js','');

//insertScript('/portals/1/javascript/errors.js','');
insertScript('/portals/1/javascript/swfobject.js','');
insertScript('/portals/1/javascript/webdev.js','');
//insertScript('/portals/1/javascript/trytalk2.js','');
//insertScript('/portals/1/javascript/trytalk3.js','');
//insertScript('/portals/1/javascript/trytalk4.js','');

}

/****************************************************************************************************************
 *  Function: insertScript                                                                                      *
 *  Description:  Inserts another external .js file.                                                            *
 *  Arg 1 is relative path, arg 2 is init function 2 run "" = none                                              *
 *  Author: Kee Fai Foong                                                                                       *
 *  Date: 26/6/03                                                                                               *
 ****************************************************************************************************************/


function insertScript(filePath, fName) {

//document.write('<script src="' + serverBase + filePath + '" type="text/javascript" ></script>');

document.write('<script src="' + filePath + '" type="text/javascript" >'+fName+'\;</script>');

}


/****************************************************************************************************************
 *  Function: getCookie()                                                                                       *
 *  Description:  returns the value of the specified parameter from the cookie.  Returns empty string if not    *
 *                found                                                                                         *
 *  Author: Kee Fai Foong                                                                                       *
 *  Date: 20/6/03                                                                                               *
 ****************************************************************************************************************/

function getCookie(cookiename) {
// var cookiestring=""+document.cookie;
// var index1=cookiestring.indexOf(cookiename);
// if (index1==-1 || cookiename=="") return ""; 
// var index2=cookiestring.indexOf(';',index1);
// if (index2==-1) index2=cookiestring.length; 
// return unescape(cookiestring.substring(index1+cookiename.length+1,index2));


  var arg = cookiename + "=";

  var alen = arg.length;

  var clen = document.cookie.length;

  var i = 0;

  while (i < clen) {

    var j = i + alen;

    if (document.cookie.substring(i, j) == arg)

      return getCookieVal (j);

    i = document.cookie.indexOf(" ", i) + 1;

    if (i == 0) break; 

  }

  return "";

}

//

// "Internal" function to return the decoded value of a cookie

//

function getCookieVal (offset) {

  var endstr = document.cookie.indexOf (";", offset);

  if (endstr == -1)

    endstr = document.cookie.length;

  return unescape(document.cookie.substring(offset, endstr));

}


/****************************************************************************************************************
 *  Function: setCookie(Str value, Str keyName, Str response_message)                                           *
 *  Description: Function for setting a cookie value.  The value paramater defines the value of the cookie      *
 *  entry.  The keyName parameter defines the associated unique key which is used to retrieve the stored value  *
 *  The response_message specifies the text alert message displayed when the cookie is set.  Specifying a value *
 *  of "" disables this feature.                                                                                *
 *  Author: Kee Fai Foong                                                                                       *
 *  Date: 23/6/03                                                                                               *
 ****************************************************************************************************************/
  function setCookie(value, keyName, response)  {
    
    var expiry = new Date();
    var killdate = new Date(expiry -1);
    document.cookie = "blank= ;expires="+killdate.toGMTString();
    if (expiry.getMonth() > 5) {
      expiry.setFullYear(expiry.getFullYear() +1);
      expiry.setMonth(expiry.getMonth() -5);
      }
    else {
      expiry.setMonth(expiry.getMonth()+6);
    }
//    document.cookie = keyName+"="+value +
//		      "; expires=" + expiry.toGMTString() + 
//                      "; path=/;domain=eflau1.learnedsolutions.com;";

    document.cookie = keyName + "=" + escape (value) +

    "; expires=" + expiry.toGMTString() + "; domain="+serverDomain+"; path=/";



    if (response != "" ) alert(response);

}


// convert path format to internet slashes
function convertPathFormat(str) {
   return str.replace(/\\/g,"/");

}

// convert path format to windows back slashes
function convertPathFormatToLocal(str) {
   return str.replace(/\//g,"\\");

}



/****************************************************************************************************************
 *  Function: getQueryString   				                                                        *
 *  Description: get current page's query string. 							        *
 *  Author: Kee Fai Foong                                                                                       *
 *  Date: 22/1/04                                                                                               *
 ****************************************************************************************************************/
function getQueryString() {

	// get query string
	var documentURL = "" + parent.document.location;
	var index = documentURL.indexOf("?");
	
	if (index != -1)
		return documentURL.substring(index + 1, documentURL.length);
	else return null;

 
}

/****************************************************************************************************************
 *  Function: getParamaterValue    				                                                *
 *  Description: get a particular value from query string.  Pass query string obtained from getQueryString()    *
 *  Author: Kee Fai Foong                                                                                       *
 *  Date: 22/1/04                                                                                               *
 ****************************************************************************************************************/
function getParameterValue(parameterName, queryString)
{
	var retVal = "";
	var parameters = queryString.toUpperCase();
	var index = parameters.indexOf(parameterName.toUpperCase());
	
	if (index != -1)
	{
		var parameterNameLength = parameterName.length;
		
		retVal = queryString.substring(index + parameterNameLength + 1, queryString.length);

		if (retVal.indexOf("&") > 0)
			retVal = retVal.substring(0, retVal.indexOf("&"));
			
		retVal = unescape(retVal);
	}

	return retVal;
}



///////////////////////////////////////////
//
//	This function is to get a selected option from a radio group
//  It returns the value associated witht he selected option, null otherwise
//  
///////////////////////////////////////////
function getRadioGroupValue(radioGroup) {

if (radioGroup == null || radioGroup == undefined) return null;

var numberOfOptions = radioGroup.length;

// if we only have 1 option, we can't use array syntax
if (numberOfOptions == null) {
  if (radioGroup.checked == true) return radioGroup.value;
  else return null;
}

var x = 0;
  for (x = 0 ; x < numberOfOptions ; x++) {
    if (radioGroup[x].checked == true) return radioGroup[x].value;
  }
  
  // if none selected, return error
  return null;

}


///////////////////////////////////////////
//
//  This function is to set a selected option from a radio group
//  It returns true if successful, false otherwise
//  NOTE: If u specify an invalid value to set, the state of previously
//        selected radio boxes will be lost 
//  
///////////////////////////////////////////
function setRadioGroupValue(radioGroup, value) {

var numberOfOptions = radioGroup.length;

// if we only have 1 option, we can't use array syntax
if (numberOfOptions == null) {
  if (radioGroup.value == value) {
    radioGroup.checked = true;
    return true;
  }
  else return false;
}

var x = 0;
  for (x = 0 ; x < numberOfOptions ; x++) {
    if (radioGroup[x].value == value) {
      radioGroup[x].checked = true;
	//alert("Checking radio button at index " + x + ", with value: " + value);
      return true;
    }
    else radioGroup[x].checked = false;
  }
  
  // if none selected, return error
  return false;

}





