﻿// Common functions

function CheckFieldLimit(field, iLimit, strID){
    if(field.value.length > (iLimit-1)){
        field.value = field.value.substring(0,(iLimit-1));
    }
    document.getElementById(strID).innerHTML = (iLimit - field.value.length - 1) + " characters remaining";
}


// removes the square border that IE
// insists on adding to checkboxes and radio
function removeCheckBoxBorders(){
	var el = document.getElementsByTagName("input");
	for (i=0;i<el.length;i++){
		var type = el[i].getAttribute("type");
		if((type=="checkbox")||(type=="radio")){
			el[i].style.border = "none";
		}
	}
}


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

//trim
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

//gets array of elements depending on class name, node or tag
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

//browser detection
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


function ChangeTASize(strElementID,bAdd){
	el = document.getElementById(strElementID);
	delta = 5;
	if(bAdd){
		if (el.rows + delta < 1){
			el.rows = 1;
		}else{
			el.rows += delta;
		}
	}else{
		if (el.rows - delta < 1){
			el.rows = 1;
		}else{
			el.rows -= delta;
		}
	}
}

function HideShow(strElementID, strLinkID){
	if(document.getElementById(strElementID).style.display == 'none'){
		document.getElementById(strElementID).style.display = '';
		document.getElementById(strLinkID).innerHTML = '(hide)';
	}else{
		document.getElementById(strElementID).style.display = 'none';
		document.getElementById(strLinkID).innerHTML = '(show)';
	}
}


//examples///////////////////////////////////////////////////

//SET:
//perm
//var date = new Date();
//date.setFullYear(2006,1,1);
//SetArrayCookie("imc", "first_sub", "RRRDDD", date);

//READ:
//alert(GetCookie("imc"))

//getting value set by VBScript Response.Cookies("imc")("email")

// JavaScript:alert(document.cookie.split(';').join('\n'))
//////////////////////////////////////////////////////////////

//SetCookie - legacy from common.js
function SetCookie (name, value) {  
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + "=" + escape (value) + 
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
		((path == null) ? "" : ("; path=" + path)) +  
		((domain == null) ? "" : ("; domain=" + domain)) +    
		((secure == true) ? "; secure" : "");
}

function NewSetCookie(name, value, expires, path, domain, secure) { 
	var curCookie = name + "=" + escape(value) + 
					((expires) ? "; expires=" + expires.toGMTString() : "") + 
					((path) ? "; path=" + path : "") + 
					((domain) ? "; domain=" + domain : "") + 
					((secure) ? "; secure" : ""); 
	document.cookie = curCookie; 
}

function GetCookie(Name) {   
	var search = Name + "="   
	if (document.cookie.length > 0) { // if there are any cookies 
	     offset = document.cookie.indexOf(search)       
		 if (offset != -1) { // if cookie exists          
		 offset += search.length          
		 // set index of beginning of value 
		 end = document.cookie.indexOf(";", offset)          
		 // set index of end of cookie value         
		 if (end == -1)             
		 	end = document.cookie.length         
			return unescape(document.cookie.substring(offset, end))      
		}    
	}
	else
	{
	}
	
	return "NAMESPACE_NOT_EXISTS";
}