/**
 * @author kcw
 */

// function to add style elements at the time the head loads

var CSSRules = function() {
var headElement = document.getElementsByTagName("head")[0],
 styleElement = document.createElement("style");
styleElement.type = "text/css";
headElement.appendChild(styleElement);

// memoize the browser-dependent add function
var add = function() {
 // IE doesn't allow you to append text nodes to <style> elements
 if (styleElement.styleSheet) {
return function(selector, rule) {
 if (styleElement.styleSheet.cssText == '') {
styleElement.styleSheet.cssText = '';
 }

 styleElement.styleSheet.cssText += selector + " { " + rule + " }";
}

 } else {
return function(selector, rule) {
 styleElement.appendChild(document.createTextNode(selector + " { " + rule + " }"));
}
}
}();

return {
 add : add
}

}();

// Function to get the file name from the url

function returnDocument() {
    var file_name = document.location.href;
    var end = (file_name.indexOf("?") == -1) ? file_name.length : file_name.indexOf("?");
    return file_name.substring(file_name.lastIndexOf("/")+1, end);
}

function returnParameterFromDocumentString() {
    var file_name = document.location.href;
    var end = file_name.length;
    return file_name.substring(file_name.lastIndexOf("?")+1, end);	
}

function returnSubFolder() {
    var file_name = document.location.href;
    var end = file_name.lastIndexOf("/");
    var subFolderName =  file_name.substring(file_name.indexOf("/",20)+1, end);
   if (subFolderName != '') {
    return file_name.substring(file_name.indexOf("/",20)+1, end);
    }else{
    return '';
    }
}


// Function to read cookie

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 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 to erase cookie to log user out
function eraseCookie(name) {
	createCookie(name,"",-1);
}

// Functions for show/hide topnav menu
var IE = (navigator.appName == "Microsoft Internet Explorer") ? 1 : 0;
function ToggleSelect(visState) {
  if (!IE) return;
  for (i=0; i < document.all.tags('SELECT').length; i++){
    var obj = document.all.tags('SELECT')[i];
    if (!obj || !obj.offsetParent) continue;
    obj.style.visibility = visState;
  }
}


function showmenu(element) {
  ToggleSelect("hidden");
  document.getElementById(element).style.visibility="visible"
}
function hidemenu(element) {
  ToggleSelect("visible");
  document.getElementById(element).style.visibility="hidden"
}

// Function to parse out url parameter
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];
}


// Function to find the partner_id parameter and put in logo
function detectPartner_id(){
    // first, see if the partner id cookie has been dropped and if so, return the value

if (readCookie('partneridcookie')) {  
    return readCookie('partneridcookie');
}else{
   // then if not, check to see if there's a parameter in the url and drop partner_id cookie for next time 
   var file_name = document.location.href;
 
    var end = file_name.length;
   if (file_name.lastIndexOf("partner_id=") != -1){
        var param_value = gup( 'partner_id' );
       // var param_value = file_name.substring(file_name.lastIndexOf("partner_id=")+11, end);
       createCookie ('partneridcookie', param_value, 1); 
        return param_value;
     }	

}
}

// Function for image popup window
var newwindow = '';
function popitup(url,width,height) {
    newwindow=window.open(url,'htmlname','width='+width+',height='+height+',resizable=1');
}