// 20070601 aathavale pilot: function to extract GET variables from url
function gup(name){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec(window.location.href);
  if( results == null )
    return "";
  else
    return results[1];
}


/**
 * Get cookie values, then check for country and language
 **/
function GetCookie(name) {
    var value = "";
    if ((begin = document.cookie.indexOf(name + "=")) >= 0) {
        if ((end = document.cookie.indexOf(";",begin)) >= 0) {
            value = document.cookie.substring(begin + name.length + 1,end);
        } else {
            value = document.cookie.substr(begin + name.length + 1);
            }
        }
    return(unescape(value));
}


/* 06.16.08 - kwolski  
 * determineLang() - Find the language of a page
 * 
 * This function determines and returns the correct supported language
 * abbreviation for a given page. It first checks to see if a 'lang' 
 * query string attribute has been set and secondly, a 'lang' cookie. If 
 * either of these values are valid, it will return that language, if not
 * it will default to english.
 */
function determineLang(){

/* Get the value of 'lang' query string parameter */
var langqs = gup('im_language'); 
/* Get the value of the lang cookie, if it exists*/ 
var thecook = GetCookie('lang');

var validLangs = "en,es,de,fr,it,ja,ko,zh-cn,zh-tw"; 
	
	/*Found a query string lang attribute*/
	if(langqs != ''){
		
		/* Make sure it is a supported language */ 
		if (validLangs.indexOf(langqs) != -1) {
			return langqs;
			}else{
				return 'en';
			}
	/* No query string attribute, check lang cookie */	
	}else if(thecook != ''){
		/* Make sure it is a supported language */ 
		if (validLangs.indexOf(thecook) != -1) {
			return thecook;
			}else{
				return 'en';
			}	
	
	/* If the two above failed, return the default language
	 * of 'en'.
	 */	
	}else{
		return 'en';
	}
 		
}
