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" : "");
}

function getCookie(name) {
	var prefix = name + "="
	var start = document.cookie.indexOf(prefix)

	if (start==-1) {
		return null;
	}

	var end = document.cookie.indexOf(";", start+prefix.length)
	if (end==-1) {
		end=document.cookie.length;
	}

	var value=document.cookie.substring(start+prefix.length, end)
	return unescape(value);
}

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";
  }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function newwin() {

	if (!document.getElementsByTagName) return false;

	var lnks = document.getElementsByTagName('a');

	for (var i=0;i<lnks.length;i++) {

		if (lnks[i].className == 'newwin') {

			lnks[i].onclick = function () { window.open(this.getAttribute('href')); return false; };
			lnks[i].onkeypress = lnks[i].onclick;

		}

	}

    return true;

}

function openmovie() {

	if (!document.getElementsByTagName) return false;

	var lnks = document.getElementsByTagName('a');

	for (var i=0;i<lnks.length;i++) {

		if (lnks[i].className == 'openmovie') {

			lnks[i].onclick = function () { window.open(this.getAttribute('href'),'mywindow','scrollbars=0,width=550,height=487'); return false; };
			lnks[i].onkeypress = lnks[i].onclick;

		}

	}

    return true;

}

//see http://www.dustindiaz.com/getelementsbyclass/ for info
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;
}

addLoadEvent(function() {
  newwin();
  openmovie();
});

// This function is a generic function to create form elements
function createFormElement(element, type, name, id, value, parent) {
    var e = document.createElement(element);
    e.setAttribute("name", name);
    e.setAttribute("type", type);
    e.setAttribute("id", id);
    e.setAttribute("value", value);
    parent.appendChild(e);
}

function highlightFormElements() {
    // add input box highlighting
    addFocusHandlers(document.getElementsByTagName("input"));
    addFocusHandlers(document.getElementsByTagName("textarea"));
}

function addFocusHandlers(elements) {
    for (i=0; i < elements.length; i++) {
        if (elements[i].type != "button" && elements[i].type != "submit" &&
            elements[i].type != "reset" && elements[i].type != "checkbox") {
            elements[i].onfocus=function() {this.className='focus';this.select()};
            elements[i].onclick=function() {this.select()};
            elements[i].onblur=function() {this.className=''};
        }
    }
}

function current_page_name() {

    var pathname = location.pathname;
    var parts = pathname.split('/');
    var filename = parts[parts.length-1];
    var parts = filename.split('.');
    var filename = parts[0];
    return filename;

}

function confirmDelete(obj) {   
    var msg = "Are you sure you want to delete this " + obj + "?";
    ans = confirm(msg);
    if (ans) {
        return true;
    } else {
        return false;
    }
}

function numberOnly(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
		    if (charCode == 8) {
        return true;
    }
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        status = "This field accepts numbers only.";
        return false;
    }
    status = "";
    return true;
}

function decimalOnly(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode == 46 || charCode == 8) {
        return true;
    }
    if (charCode > 31 && (charCode < 48 || charCode > 57) ) {
        status = "This field accepts numbers only.";
        return false;
    }
    status = "";
    return true;
}

function fixWindowVertical(link) {
	var myWindow=Wicket.Window.get();
	if(myWindow) {
		var top = Position.get(link).top-6;
		myWindow.window.style.top = top + "px";
		/*Position.set(populate, 10+Position.get(link).left, Position.get(link).top-6);*/
	}
	return false;
} 

