// CookieName is the name of the cookie, 
// CookieValue is the Value and 
// ExpireTime is the expiretime in minutes from now
function WriteCookie(CookieName, CookieValue, ExpireTime) {
	var ExpireDate = new Date();
  	ExpireDate.setTime(ExpireDate.getTime() + (ExpireTime*60*1000));
	document.cookie = CookieName + "=" + CookieValue + "; expires=" + ExpireDate.toGMTString();
} // WriteCookie

// CookieName is name of cookie, 
// returns the value of the cookie if cookie is set
// if cookie is not found false is returned
function ReadCookie(CookieName) {
	var myString = document.cookie;
	var CookieValue = false;
	var CookieArray = new Array();
	CookieArray = myString.split("; ");
	var tmpArray = new Array();
	for (i=0;i<CookieArray.length;i++) {
		tmpArray = CookieArray[i].split("=");
		if (tmpArray[0]==CookieName) {
			CookieValue=tmpArray[1];
			return CookieValue;
			break;
		}
	}
	return false;
} // ReadCookie

// Returns all parameters and form fields from defined document.
function GetDocumentParameters(document) {
	var lp=new Array(); // Location parameters
	var ParameterPos=String(document.location).indexOf("?");

	if ( ParameterPos>-1 ) {
		var Parameters=String(document.location).substr(ParameterPos+1);
		var ParameterArray=Parameters.split("&");
		for (var Item in ParameterArray) {
			var lpi=ParameterArray[Item].split("="); // Location parameter item.
			if ( lpi.length==2 ) lp[lpi[0]]=unescape(lpi[1]);
		}
	}
	if ( document.forms[0] ) {
		with ( document.forms[0] ) {
			for (var i=0; i<elements.length; i+=1 ) {
				// If not a .net variable.
				if ( elements[i].name.indexOf("__")!=0 ) {
					lp[elements[i].name]=elements[i].value;
				}
			}
		}
	}

	return lp;
} // GetDocumentParameters

function OpenPopup(argUrl, argDialogArgument, argWidth, argHeight){
	if(typeof(argUrl)=='undefined')return false;
	if(typeof(argDialogArgument)=='undefined')argDialogArgument=window;
	if(typeof(argWidth)=='undefined')argWidth="660"; // 740
	if(typeof(argHeight)=='undefined')argHeight="570"; // 550
	return window.showModalDialog(AdminRootUrl + "PopUpFrame.aspx?url="+escape(argUrl), argDialogArgument,"edge:raised; center:yes; status:no; resizable:yes; dialogWidth:"+argWidth+"px; dialogHeight:"+argHeight+"px;");
//old with resize	return window.showModalDialog(AdminRootUrl + "PopUpFrame.aspx?url="+escape(argUrl), argDialogArgument,"edge: raised; center:yes; status:no; dialogWidth:"+argWidth+"px; dialogHeight:"+argHeight+"px; resizable:yes");
//	return window.open(AdminRootUrl + "PopUpFrame.aspx?url="+escape(argUrl));
} // OpenPopup


function OpenPopupWithFeatures(argUrl, argDialogArgument, argFeatures ,argWidth, argHeight){
	if(typeof(argDialogArgument)=='undefined')argDialogArgument=window;
	if(typeof(argUrl)=='undefined')return false;
	if(typeof(argWidth)=='undefined')argWidth="660"; // 740
	if(typeof(argHeight)=='undefined')argHeight="570"; // 550
	return window.showModalDialog(AdminRootUrl + "PopUpFrame.aspx?url="+escape(argUrl), argDialogArgument,argFeatures+"dialogWidth:"+argWidth+"px; dialogHeight:"+argHeight+"px;");
} // OpenPopupWithFeatures


function UnselectableChilds(Obj) {
	for (var x=0; x<Obj.childNodes.length; x++) {
		if (Obj.childNodes[x].unselectable) {
			Obj.childNodes[x].unselectable = "on";
		} else {
			if (Obj.childNodes[x].setAttribute) {
				Obj.childNodes[x].setAttribute("unselectable", "on");
			}
		}
		UnselectableChilds(Obj.childNodes[x]);
	}
} // UnselectableChilds

function EnableButton(Id) {
	if (document.getElementById(Id)) {
		document.getElementById(Id).disabled="";
		document.getElementById(Id).style.filter = "";
		document.getElementById(Id).style.cursor="hand";
	}
} //EnableButton

function DisableButton(Id) {
	if (document.getElementById(Id)) {
		document.getElementById(Id).disabled="disabled";
		document.getElementById(Id).style.filter = "alpha(opacity=50)";
		document.getElementById(Id).style.cursor="default";
	}
} //DisableButton


function getAutoDate(argFormElement) {
	var d = new Date();
	var day = d.getDate() + '';
	var month = d.getMonth() + '';
	var year = d.getYear() + '';
	var rng = argFormElement.createTextRange();
	argFormElement.currentDV = day;

	if (argFormElement.currentDV == month && (event.keyCode == 109 || event.keyCode == 190))  {
		argFormElement.currentDV = year;
		top.window.status = 'year';
	}
	
	if (argFormElement.currentDV == day && (event.keyCode == 109 || event.keyCode == 190))  {
		argFormElement.currentDV = month;
		top.window.status = 'month';
	}
		
	if (!argFormElement.dateCount) {
		argFormElement.dateCount = 0;
	}
	
	if (event.keyCode==8 && argFormElement.dateCount > 0) {
		argFormElement.dateCount--;
	} else {
		argFormElement.dateCount++;
	}

	if (argFormElement.value == argFormElement.currentDV.substr(0, argFormElement.dateCount)) {
		argFormElement.value = d.getDate() + '-' + d.getMonth() + '-' + d.getYear();
	}
	
	rng.moveStart('character', argFormElement.dateCount)
	rng.select()
} // getAutoDate

function ReplaceQueryString(argInput, argKey, argNewValue) {
	var StartChar = (argInput == '') ? '?' : '&';
	var input = argInput.substr(1);
	if (input.indexOf(argKey) < 0) {
		return argInput + StartChar + argKey + '=' + argNewValue
	}
	StartChar = '?';
	var output = '';
	var pairs = input.split('&');
	for (var pair in pairs) {
		if (pair.split('=')[0] == argKey) {
			output += StartChar + argKey + '=' + argNewValue + '&';
		} else {
			output += StartChar + argKey + '=' + argNewValue + '&';
		}
		StartChar = '&';
	}
	return output;
} // ReplaceQueryString


function GetUrlObjectId(url) {
	var returnValue = "";
	var str = new String(url);
	if ((index = str.indexOf('ObjectId=')) != -1) {
		index += 9;
		endindex = str.indexOf("&", index)
		if (endindex >= index) {
			returnValue = str.substring(index, endindex);
		} else {
			returnValue = str.substr(index);
		}
	}
	delete str;
	return returnValue;
} // GetUrlObjectId

function checkBrowserVersion(Browser,Version) {
	if (navigator.appVersion.indexOf(Browser + " " + Version) > 0) {
		return true;
	} else {
		return false;
	}
}

function CreateEvent(eventobject, eventname, eventfunction) {
	if (eventobject) {
		if (typeof(eventobject.attachEvent) != "undefined") {
			eventobject.attachEvent("on"+eventname, eventfunction);
		} else {
			if (typeof(eventobject.addEventListener) != "undefined") {
				eventobject.addEventListener(eventname, eventfunction, false);
			} else {
				alert('Could not attach event');
			}
		}
	}
}
