function SetCookie(cookieName, cookieVal) {
	var exp=new Date();
	exp.setTime(exp.getTime()+(365*24*60*60*1000));
	document.cookie=cookieName+'='+cookieVal+'; path=/; expires='+exp.toGMTString();
}

function GetCookie(cookieName) {
	if(document.cookie) {
		var index=document.cookie.indexOf(cookieName);
		if (index != -1) {
			var countbegin=(document.cookie.indexOf(cookieName+'=', index) + 1);
			var countend=document.cookie.indexOf(';', index);
			if (countend == -1) {
				countend=document.cookie.length;
			}
			return document.cookie.substring(countbegin + cookieName.length, countend);
		}
	}
	return null;
}



function ChangeLang(formId, code){
	form = document.getElementById(formId);
	form.lang.value=code;
	form.submit();
}

function randomNumber (m,n){
  m = parseInt(m);
  n = parseInt(n);
  return Math.floor( Math.random() * (n - m + 1) ) + m;
}


function ChangeImg(id, sessName, sessId){
        im = document.getElementById(id);
		locn = '/' + id + '.im?';
		if (sessName && sessId){
			locn = locn + sessName + '=' + sessId + '&';
		}
		locn = locn + 'nocache=' + randomNumber(1000000000,2000000000);
        im.src = locn;
}

function SetPass(password){
	if (password){
		p = document.getElementById('signup_password');
		cp = document.getElementById('signup_cpassword');
		p.type = 'text';
		cp.type = 'text';
		p.value = password;
		cp.value = password;
	}
}

function CheckAll(element, formName){
	for (i = 0; i < document.forms[formName].elements.length; i++){
		if (document.forms[formName].elements[i].type == "checkbox"){
			document.forms[formName].elements[i].checked	= element.checked;
		}
	}
}

function OpenMessages(url, name, width, height, top, left){
	if (!width) width=500;
	if (!height) height=500;
	if (!top) top=0;
	if (!left) left=0;
	var w=window.open(url, name, "width=" + width + ",height=" + height +",resizable=1,toolbar=0,location=0,status=0,menubar=0,directories=0,scrollbars=yes,top=" + top +",left=" + left);
	w.focus();
}

function OpenWin(url, name, width, height, top, left){
	if (!width) width=500;
	if (!height) height=700;
	if (!top) top=0;
	if (!left) left=0;
	var w=window.open(url, name, "width=" + width + ",height=" + height +",resizable=1,toolbar=0,location=0,status=0,menubar=0,directories=0,scrollbars=yes,top=" + top +",left=" + left);
	w.focus();
}



function ShowSearchTerms(){
	document.getElementById("searchOff").style.display="none";
	document.getElementById("searchOn").style.display="block";
	document.getElementById("searchTerms").style.display="block";
	document.getElementById('searchCol').className = 'searchTermsOn';
}

function HideSearchTerms(){
	document.getElementById("searchOff").style.display="block";
	document.getElementById("searchOn").style.display="none";
	document.getElementById("searchTerms").style.display="none";
	document.getElementById('searchCol').className = 'searchTermsOff';
}


function insertItem (fromName, toName) {
	var fromObj = document.getElementById(fromName);
	var toObj = document.getElementById(toName);
	
	if (fromObj.options.selectedIndex!=-1) {
		var exist;
		index = toObj.length;
		for (i=0; i<index; i++){
			if (toObj.options[i].value == fromObj.options[fromObj.options.selectedIndex].value){
				exist = true;
			}
		}
		if (!exist){
			toObj.options[index] = new Option(fromObj.options[fromObj.options.selectedIndex].text, fromObj.options[fromObj.options.selectedIndex].value); 
		}
	}
}

function eraseItem (toName) {
	var toObj = document.getElementById(toName);
	if (toObj.options.selectedIndex!=-1 && toObj.options.selectedIndex!=0) {
		toObj.options[toObj.options.selectedIndex]=null;
	}
}



function selectItem (toName, outName) {
	var toObj = document.getElementById(toName);
	mn = toObj.length;
	out =  document.getElementById(outName);
	out.value ='';
	for (n=0; n<mn; n++) {
		if (toObj.options[n].value != '-10') {
			out.value += toObj.options[n].value + ',';
		}
	}
	if (out.value != '0') {
		out.value = out.value.substring(0, out.value.length-1);
	}
}

function initCalendar(inputField, triggerField, dateFormat){
	if (!dateFormat) dateFormat = "%Y-%m-%d";
	Calendar.setup(
    	{
      inputField  : 	inputField,         
      ifFormat    :	dateFormat,
      button      : 	triggerField,
      singleClick : 	"true",
      firstDay  : 	1,
      weekNumbers : 	false,
      align : "br"
    }
  );
}
