var opt;
var mod;
var speed = 3;

function addLoadListener(fn) {
  if (typeof window.addEventListener != 'undefined') {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')  {
    window.attachEvent('onload', fn);
  }
  else   {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')     {
      window.onload = fn;
    }
    else {
      window.onload = function() {
        oldfn();
        fn();
      }
    }
  }
}

function AdjustImageSizes() {
	var i, w, mw, h, dh, c, images = xGetElementsByTagName('img', document);
	mw = Math.round(xClientWidth() / ratio);
	dh = 0;
	for (i = 0; i < images.length; ++i) {
		s = images[i];
		s.style.visibility = 'visible';
		w = s.width;
		if (w > mw) {
			h = s.height;
			s.height = Math.round(s.height * mw / s.width);
			s.width = mw;
			if (dh < (h - s.height)) {
				dh = h - s.height;
			}
		}
	}
}

function attachEventListener(target, eventType, functionRef, capture) {
  if (typeof target.addEventListener != "undefined") {
    target.addEventListener(eventType, functionRef, capture);
  } else if (typeof target.attachEvent != "undefined")  {
    target.attachEvent("on" + eventType, functionRef);
  } else  {
    eventType = "on" + eventType;
    if (typeof target[eventType] == "function") {
      var oldListener = target[eventType];
      target[eventType] = function()  {
        oldListener();
        return functionRef();
      }
    } else {
      target[eventType] = functionRef;
    }
  }
  return true; 
}

function del()
{
	var str   
	
	var Len = window.opener.document.editform.displayname.value.length 
	str = window.opener.document.editform.displayname.value.substring(0,Len- 1)

	window.opener.document.editform.displayname.value = str
}

function EnableRead(o) {
	var gread = xGetElementById('gread');
	try {
		if ((o.checked == true) && (gread.checked == false)) {
			gread.checked = true;
			gread.value = 'on';
		}
	} catch (e) {
		alert(e);
	}
}

function hhtc(){ 
        var o=document.getElementById('help'); 
        var os=o.style; 
        os.visibility='hidden'; 
        os.display='none'; 
} 

function huc(){
        var o=document.getElementById('unicode');
        o.innerHTML='';
}

function newWindow(url) {
// This is a degenerated form of the logic
	var x;
    var y;
	var width;
	var height;
	var winOpts;
    var scroll;

	width = screen.width / 5 * 4;
	height = screen.height - 100;
	x = (screen.width - width) / 2;
	y = (screen.width - width) / 2;
	scroll = 1
    winOpts = 'channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars='+scroll+',status=0,toolbar=0,screenX=' + y + ',top=' + 0 + ',screenY=' + x + ',left=' + x + ',width=' + width + ',height=' + height;
    window.open(url, '', winOpts);
}

function newWindowAdv(url, width, height, scrollbar) {
    var x = (screen.width / 2) - (width / 2);
    var y = (screen.height / 2) - (height / 2);
    var winOpts;
    var scroll;
    if (scrollbar == null) {
        scroll=0
    } else {
        if (scrollbar == 1) {
            scroll = 1
        } else {
            scroll = 0
        }
    }

    winOpts = 'channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=0,scrollbars='+scroll+',status=0,toolbar=0,screenX=' + y + ',top=' + y + ',screenY=' + x + ',left=' + x + ',width=' + width + ',height=' + height;
    window.open(url, '', winOpts);
}

function nextSlide() {
	SubmitSlide(opt,mod,speed);
}

function resetdate() {
	alert('hello');
	var today;
	today = new Date();
	populate2(today.getDate(),today.getMonth(),today.getFullYear());
}

function SetAction(frm,option) {
	var action;
	var vars;
	var i;
	var ctl;
	var plines;
	var p;
	var scv;
	var sep;
	
	action = frm.action;
	vars=action.split('=');
	frm.action = vars[0]+'='+option;
	ctl = xGetElementById('productlines');
	plines = ctl.value;
	p = plines.split(',')
	sep = '';
	svc = '';
	for (i = 0; i < p.length; i++) {
		ctl = xGetElementById('S'+i);
		svc = svc + sep + ctl.value;
		sep = ',';
	}
	ctl = xGetElementById('services');
	ctl.value = svc;
	return true;
}

function setStr(str)
{
	try {
		window.opener.document.editform.displayname.value+=str		
	} catch (e) {
		alert(e);
	}
}

function shtc(o,t,e) { 
	var po=document.getElementById('help'); 
	var os=po.style; 
	po.innerHTML = t;
	if(os.visibility == '' || os.visibility == 'hidden'){ 
		var par=o,total=0; 
		var fl=0,ft=0,tl=0,tt=0,t=0,l=0; 
		while(par&&par.tagName!='FORM'){ 
			t=par.offsetTop; 
			l=par.offsetLeft; 
			if(t==null||l==null) 
					break; 
			if(par.tagName!='TD'&&par.tagName!='DIV'){ 
					tt+=t; 
			} 
			tl+=l; 
			par=par.parentNode; 
		} 
		os.display='block'; 
		fl=e.pageX?pageXOffset+e.clientX+20:document.body.scrollLeft+e.x+20; 
		ft=e.pageY?pageYOffset+e.clientY:document.body.scrollTop+e.y; 
		os.left=fl; 
		os.top=ft;
		if (arguments.length > 3) 
			os.width=arguments[3];
		os.zIndex=100; 
		os.visibility='visible'; 
	} 
} 

function SetCheck(o) {
	if (o.checked == true)
		o.value = 'on'
	else
		o.value = 'off';
}

function SetCheckPicasa(o) {
	if (o.checked == true)
		o.value = 'picasa'
	else
		o.value = 'no';
}

function startSlideshow() {
  window.setTimeout('nextSlide()', speed * 1000);
}
function SubmitGB(option,modify) {
	document.editform.action = 'gbook.php?comment='+option+'&modify='+modify;
	document.editform.submit();
	return true;
}
function SubmitSlide(option,modify,rate) {
	document.editform.action = 'Page.php?option='+option+'&modify='+modify+'&rate='+rate;
	document.editform.submit();
	return true;
}
function SubmitSwap(option,modify,o1,o2) {
	document.editform.action = 'Page.php?option='+option+'&modify='+modify+'&o1='+o1+'&o2='+o2;
	document.editform.submit();
	return true;	
}
function SubmitEdit(option,modify) {
	var i;
	document.editform.action = 'Page.php?option='+option+'&modify='+modify;
	for (i = 2; i < arguments.length; i+=2)
		document.editform.action = document.editform.action+'&'+arguments[i]+'='+arguments[i+1];
	document.editform.submit();
	return true;
}
function SubmitFile(option,modify) {
	if (modify == 0)
		return SubmitEdit(option,modify);
	if (validateFields() == true) {
		document.editform.action = 'Page.php?option=1&modify='+modify;
		document.editform.submit();
		return true;
	}
}
function suc(uc){
	var o=document.getElementById('unicode');
	o.innerHTML=uc;
}

function validateAuthor() {
	var author;
	if (!(author = xGetElementById('author'))) return true;
	if (author.value == "") {
		var errorSpan = document.createElement('errspan');
		var errorMessage = document.createTextNode('Missing Author');
		errorSpan.appendChild(errorMessage);
		errorSpan.className = 'errorMsg';
		errorSpan.style.position = 'absolute';
		errorSpan.style.left = '160';
		var fieldLabel = author.parentNode.previousSibling.firstChild;
		while (fieldLabel.nodeName.toLowerCase() != "label") {
			fieldLabel = fieldLabel.nextSibling;
		}	
		fieldLabel.appendChild(errorSpan);
		return false;
	}
	return true;	
}
function validateDisplayName() {
	var author;
	if (!(author = xGetElementById('displayname'))) return true;
	if (author.value == '') {
		var errorSpan = document.createElement('errspan');
		var errorMessage = document.createTextNode('Missing Display Name');
		errorSpan.appendChild(errorMessage);
		errorSpan.className = 'errorMsg';
		errorSpan.style.position = 'absolute';
		errorSpan.style.left = '246';
		var fieldLabel = author.parentNode.previousSibling.firstChild;
		while (fieldLabel.nodeName.toLowerCase() != "label") {
			fieldLabel = fieldLabel.nextSibling;
		}	
		fieldLabel.appendChild(errorSpan);
		return false;
	}
	return true;
	
}
function validateDocdate() {
	return true;
}
function validateFields(){
	var p, e;
	var errors = xGetElementsByTagName('errspan');
	while (errors.length > 0) {
		i = errors.length - 1;
		p = errors[i].parentNode;
		p.removeChild(errors[i]);
		errors = xGetElementsByTagName('errspan');
	}
	e = 0;
	if (validateSectionID() == false) e = e + 1;
	if (validateDisplayName() == false) e = e + 1;
	if (validateAuthor() == false) e = e + 1;
	if (validateDocdate() == false) e = e + 1;
	if (validateGread() == false) e = e + 1;
	if (e > 0) {
		var errors = xGetElementsByTagName('errspan');
		return false;
	} else {
		return true;
	}
}
function validateGread() {
	var gread = xGetElementById('gread');
	var gwrite = xGetElementById('gwrite');
	if (!gread.checked &&  gwrite.checked) {
		var errorSpan = document.createElement('errspan');
		var errorMessage = document.createTextNode('Enable group read or disable group write');
		errorSpan.appendChild(errorMessage);
		errorSpan.className = 'errorMsg';
		errorSpan.style.position = 'absolute';
		errorSpan.style.left = '270';
		var fieldLabel = gread.parentNode.previousSibling.firstChild;
		while (fieldLabel.nodeName.toLowerCase() != "label") {
			fieldLabel = fieldLabel.nextSibling;
		}	
		fieldLabel.appendChild(errorSpan);
		return false;
	}
	return true;	
}
function validateSectionID() {
	var author;
	if (!(author = xGetElementById('sectionid'))) return true;
	if (author.value == '') {
		var errorSpan = document.createElement('errspan');
		var errorMessage = document.createTextNode('Missing Section ID');
		errorSpan.appendChild(errorMessage);
		errorSpan.className = 'errorMsg';
		errorSpan.style.position = 'absolute';
		errorSpan.style.left = '214';
		var fieldLabel = author.parentNode.previousSibling.firstChild;
		while (fieldLabel.nodeName.toLowerCase() != "label") {
			fieldLabel = fieldLabel.nextSibling;
		}	
		fieldLabel.appendChild(errorSpan);
		return false;
	}
	return true;
	
}
