//
// select the a value from dropdown list and focus a textbox
//
function FocusTextboxFromDropdown(which, target) {
	if (which.selectedIndex != 0) {
		target.focus();
	}
}

function SelectDropDownByValue(e, val) {
	for (var i=0; i<e.options.length; i++) {
		if (e.options[i].value == val)
			e.options[i].selected = true;
	}
}

function SelectDropDownByText(e, val) {
	for (var i=0; i<e.options.length; i++) {
		if (e.options[i].text == val)
			e.options[i].selected = true;
	}
}

function TickAll(e, target) {
	for (i=0; i<target.length; i++) {
		target[i].checked=e.checked;
	}
}

function ShowHideById(id, flag, n) {
	if (n == null) {
		e = document.getElementById(id);
		e.className = flag;
	} else {
		for (var i=0; i<n; i++) {
			e = document.getElementById(id+i);
			e.className = flag;
		}
	}
}

function LoadInFrame(framePage) {
	framePage = (framePage == null)?'index.html':framePage;
	if (top.location == self.location) { window.location = framePage +'?page='+ window.location.pathname + escape(window.location.search) }
}

function SetPage(value) {
	SetQueryString('page', value);
}

function SetOrderBy(value) {
	SetQueryString('orderBy', value);
}

function SetQueryString(argname, value) {
	var url = window.location.href.toString();
	var query = window.location.search.substring(1);
	var rExp = new RegExp(argname + "=[a-zA-Z0-9]+");
	url = url.substring(0, url.indexOf('?'));
	if (query.search(rExp) != -1)
		query = query.replace(rExp, argname + '=' + value);
	else
		query += ((query.length > 0) ? '&' : '') + argname + '=' + value;
	window.location.href = url +'?'+ query;
}

function GeneratePassword(length)
{	
	if (length == null) length = 6;
	var password = "";
	var possible = ";_0123456789bcdfghjkmnpqrstvwxyzBCDFGHJKMNPQRSTVWXYZ"; 
	var i = 0; 
	
	while (i < length) { 
		var char = possible.charAt(Math.floor(Math.random() * possible.length));
		if (password.indexOf(char) == -1) { 
			password += char;
			i++;
		}	
	}
	return password;	
}

function SetCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + escape(path) : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	
	document.cookie = curCookie;
}

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 null;
	} else
		begin += 2;
	
	var end = document.cookie.indexOf(";", begin);
	
	if (end == -1)
		end = dc.length;
	
	return unescape(dc.substring(begin + prefix.length, end));
}