/**
 * Gets a cookie with the specifed name.
 *
 * @param name the name of the cookie to return.
 * @return the value of the specified cookie.
 * @author Johan Känngård, WM-data, johan.kanngard@wmdata.com
 */
function getCookie(name) {
	var dc=document.cookie;
	var prefix=name+"=";
	var begin=dc.indexOf("; "+prefix);
	if(begin==-1){
		begin=dc.indexOf(prefix);
		if(begin!=0) return false;
	} else begin+=2;  
	var end=document.cookie.indexOf(";",begin);
	if(end==-1) end=dc.length;
	return unescape(dc.substring(begin+prefix.length,end));
}
/**
 * Sets a cookie with the given name and value.
 *
 * @param name the name of the cookie
 * @param value the value of the cookie
 * @param expires (optional) expiration date of the cookie (default: end of current session)
 * @param path (optional) path where the cookie is valid (default: path of calling document)
 * @param domain (optional) domain where the cookie is valid
 *              (default: domain of calling document)
 * @param secure (optional) boolean value indicating if the cookie transmission requires a
 *              secure transmission
 * @author Johan Känngård, WM-data, johan.kanngard@wmdata.com
 */
function setCookie(name,value,expires,path,domain,secure){
	document.cookie=name+"="+escape(value)+
		(expires?"; expires="+expires.toGMTString():"")+
		(path?"; path="+path:"")+
		(domain?"; domain="+domain:"")+
		(secure?"; secure":"");
}
/**
 * Deletes the specified cookie.
 *
 * @param name the name of the cookie
 * @param path (optional) path of the cookie (must be same as path used to create cookie)
 * @param domain (optional) domain of the cookie (must be same as domain used to create cookie)
 * @author Johan Känngård, WM-data, johan.kanngard@wmdata.com
 */
function deleteCookie(name,path,domain){
	if (getCookie(name)) document.cookie=name+"="+
		(path?"; path="+path:"")+
		(domain?"; domain="+domain:"")+
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
/**
 * Replaces values in Strings.
 *
 * @param inStr the String to replace.
 * @param oldStr the String to search for in inStr.
 * @param newStr the value to replace with.
 * @return the replace String.
 * @author Johan Känngård, WM-data, johan.kanngard@wmdata.com
 */
function replace(inStr,oldStr,newStr){
	var outStr="";
	var j=oldStr.length;
	for(var i=0;i<inStr.length;i++) {
		if(inStr.substr(i,j)==oldStr) {
			outStr+=newStr;
			i+=j-1;
		} else outStr+=inStr.substr(i,1);
	}
	return outStr;
}
/**
 * Returns true if the argument is a date.
 *
 * @param inStr the String to test if it is a date.
 * @return true if the specified argument is a date in the form yymmdd,
 * yy-mm-dd, yyyymmdd or yyyy-mm-dd, otherwise false.
 * @author Johan Känngård, WM-data, johan.kanngard@wmdata.com
 */
function isDate(inStr){
	var numStr=replace(replace(inStr,'-',''),' ','');
	if(isNaN(numStr)) return false;
	if(numStr.length!=6&&numStr.length!=8) return false;
	if(numStr.length==6){
		var tmpYear=eval(numStr.substr(0,2));
		var year=(tmpYear>50)?1900+tmpYear:2000+tmpYear;
		var month=numStr.substr(2,2);
		var day=numStr.substr(4,2);
	}
	if(numStr.length==8){
		var year=numStr.substr(0,4);
		var month=numStr.substr(4,2);
		var day=numStr.substr(6,2);
	}
	var tmpDate=new Date(year,month-1,day);
	if(tmpDate.getFullYear()!=year||tmpDate.getMonth()!=month-1||tmpDate.getDate()!=day) return false;
	else return true;
}
/**
 * Launches the specified local application.
 *
 * Note! The security settings in Internet Explorer must be changed according to the following:
 * - The DNS names of the development, test and production server must be put in the
 *  "Trusted Sites" ("Tools", "Internet Options", "Security")
 * - The choice "Initialize and script ActiveX controls not marked as safe" in
 *  "Tools", "Internet Options", "Security", "Trusted Sites" must be set to "Enable".
 *
 * @param the first argument is the stirt directory path. The next is the application path to launch. If multiple
 * appName arguments are supplied each of them are tested if they exist, and launches the first existing one.
 * @author Johan Känngård, WM-data, johan.kanngard@wmdata.com
 */
function launchApp(startDir, appName){
	var ws=new ActiveXObject('WScript.Shell');
	var filesystem=new ActiveXObject('Scripting.FileSystemObject');
	var appName='';
	var found=false;
	for(i=1;i<arguments.length;i++){
		appName=arguments[i];
		if (appName.indexOf('\\')>-1) {
			var currDir=appName;
			if (appName.indexOf('.exe')>-1) currDir=appName.substring(0, currDir.lastIndexOf('.exe')+4);
			if(filesystem.FileExists(currDir)){
				found=true;
				break;
			}
		}
	}
	if(found){
		if((startDir!='undefined')&&(startDir!='')){
			ws.CurrentDirectory=startDir;
		}
		ws.Exec(appName);
	}else{
		alert('Application ['+appName+'] not found');
	}
}
/**
 * Returns the parent path of a specified path.
 *
 * @param path the path to get the parent to.
 * @return the parent of the specified path. If the specified is a root directory (c:\, d:\ etc.),
 * this function returns an empty string, ""
 * @author Johan Känngård, WM-data, johan.kanngard@wmdata.com
 */
function getParentPath(path){
	var index=path.lastIndexOf('\\');
	if(index==-1) return "";
	if(index==path.length-1){
		path=path.substring(0, index);
		index=path.lastIndexOf('\\');
	}
	return (index==-1) ? "" : path.substring(0, index+1);
}
/**
 * Gets the width of the specified image, in pixels.
 *
 * @param path the path to the image.
 * @return the width of the specified image.
 * @author Johan Känngård, WM-data, johan.kanngard@wmdata.com
 */
function getImageWidth(path){
	var image=new Image();
	image.src=path;
	return image.width;
}
/**
 * Gets the height of the specified image, in pixels.
 *
 * @param path the path to the image.
 * @return the height of the specified image.
 * @author Johan Känngård, WM-data, johan.kanngard@wmdata.com
 */
function getImageHeight(path){
	var image=new Image();
	image.src=path;
	return image.height;
}
/**
 * Stripes the specified table element.
 * Uses the CSS classes oddRow and evenRow for formatting.
 *
 * @param table the table DOM element to stripe.
 * @author Johan Känngård, WM-data, johan.kanngard@wmdata.com
 */
function stripe(table) {
	var even=false;
	if(!table) return;
	var tbodies=table.getElementsByTagName('tbody');
	var trs;
	var tds;
	for(var h=0;h<tbodies.length;h++){
		trs=tbodies[h].getElementsByTagName('tr');
		for(var i=0;i<trs.length;i++){
			tds=trs[i].getElementsByTagName('td');
			for(var j=0;j<tds.length;j++){
				var mytd=tds[j];
				mytd.className=mytd.className+(even?' evenRow':' oddRow');
			}
			even=!even;
		}
	}
}
/**
 * Returns the right part of a String. Works like StrRight in LotusScript.
 *
 * @param str the String to search in.
 * @param strStart the String to search for.
 * @return the part in str after strStart
 * @author Johan Känngård, WM-data, johan.kanngard@wmdata.com
 */
function strRight(str,strStart){
	var start = str.indexOf(strStart);
	return (start>=0)?str.substring(start + strStart.length, str.length):'';
}
/**
 * Removes leading and trailing spaces in Strings.
 * Used like this:
 * <code>var s='  this is a test  ';
 * alert(s.trim());</code>
 *
 * @author Johan Känngård, WM-data, johan.kanngard@wmdata.com
 */
String.prototype.trim=function() {
	return this.replace(/^\s+|\s+$/g,"").replace(/\s+/g," ");
}
/**
 * Formats the specified date in the specified format.
 * Allowed format variables:
 * ddd=short day of week
 * yyyy=long year
 * mm=month with 2 digits, starting from 1=January
 * dd=short day of month, starting from 1
 * hh=hours in 24h clock
 * nn=minutes
 * ss=seconds
 *
 * Example:
 * var d=new Date();
 * formatDate(d,'yyyy-mm-dd');
 *	returns '2004-08-17' on august 17th 2004
 *
 * @param d the date to format.
 * @param format the format String to use when formatting the date.
 * @return a String with the date formatted with the specified formatting.
 * @author Johan Känngård, WM-data, johan.kanngard@wmdata.com
 */
function formatDate(d,format){
	var year=d.getYear();
	var month=d.getMonth()+1;
	var day=d.getDate();
	var dw=d.getDay();
	var hrs=d.getHours();
	var min=d.getMinutes(); 
	var sec=d.getSeconds();
	
	if(month<=9) month='0'+month;
	if(day<=9) day='0'+day;
	if(hrs<=9) hrs='0'+hrs;
	if(min<=9) min='0'+min;
	if(sec<=9) sec='0'+sec;
	var dws=['Sön','Mån','Tis','Ons','Tor','Fre','Lör'];
	var s=replace(format,'ddd',dws[d.getDay()]);
	s=replace(s,'yyyy',year);
	s=replace(s,'mm',month);
	s=replace(s,'dd',day);
	s=replace(s,'hh',hrs);
	s=replace(s,'nn',min);
	return replace(s,'ss',sec);;
}
/**
 * Reloads the current location into the specified frame.
 * If the frame doesn't exist, nothing happens.
 *
 * @param frameName the name of the frame to reload the current location in.
 * @author Johan Känngård, WM-data, johan.kanngard@wmdata.com
 */
function loadInFrame(frameName){
	if(window.name==frameName) return;
	var frame=getFrame(frameName,top.frames);
	if(!frame) return;
	frame.location=document.location;
}
/**
 * Gets the Window object of the specified frame.
 *
 * @param frameName the name of the fram you want to get.
 * @param frames an Array containing the starting point to look for
 * the specified frame.
 * @return the named Window frame object or null if it wasn't found.
 * @author Johan Känngård, WM-data, johan.kanngard@wmdata.com
 */
function getFrame(frameName,frames){
	var tmp=null;
	if(!frames) return;
	for(var i=0;i<frames.length;i++){
		if(frames[i].name==frameName) return frames[i];
		tmp=getFrame(frameName,frames[i]);
		if(tmp!=null) return tmp;
	}
	return null;	
}
/**
 * Launches Microsoft Word with a specified template.
 * @param templateToUse is the path and name of the template you want to use.
 * @author Johan Känngård, WM-data, johan.kanngard@wmdata.com
 */
function createDocFromTemplate(templateToUse) {
	var word=new ActiveXObject('Word.Application');
	word.visible=true
	word.Documents.Add(templateToUse)
}
