function form_expandoDel( name, num ) {
	var obTable = document.getElementById(name + '_expandoitem' + num);
	obTable.removeNode(true);
}
function form_expandoAdd( name) {
	var obHidden = document.getElementById(name + '_expandohide');
	var inOrd	 = ++(obHidden.nextnum);
	var inIndex	 = --(obHidden.index);
	var stHTML	 = obHidden.innerHTML;
	var obReg	 = /\[n\]/gi;
	stHTML = stHTML.replace( obReg, inOrd );
	obReg	 = /\[x\]/gi;
	stHTML = stHTML.replace( obReg, '' );
	obReg		= /\[i\]/gi;
	stHTML = stHTML.replace( obReg, inIndex);
	obHidden.previousSibling.insertAdjacentHTML('beforeBegin',stHTML);		
}

var form_itemHandle, form_dialogHandle;

function form_openBrowse( ob, type, dir ) {
	var szType = '', szDir='';
	switch (type) {
		case 'image' :
			szType = 'Type=Image&';
			break;
	}
	if (dir.length>0) {
		szDir = 'ServerPath=' + escape(dir) + '&'
	}
	form_itemHandle = ob;
	form_dialogHandle = window.open(stFCKPath + 'editor/filemanager/browser/default/browser.html?' + szType + szDir + 'Connector=connectors/asp/connector.asp');
	window.onfocus = form_focusDialog;
	window.onunload = form_closeDialog;
}

function form_focusDialog () {
	if (!form_dialogHandle.closed) {
		form_dialogHandle.focus();
	} else {
		window.onfocus = null;
		window.onunload = null;
	}
}

function form_closeDialog () {
	if (!form_dialogHandle.closed) {
		form_dialogHandle.close();
	}
	window.onfocus = null;
	window.onunload = null;
	
}

/* next function is for file selector support and *MUST* be called SetUrl */
function SetUrl( sz ) {
	form_itemHandle.value = sz;
	window.onfocus = null;
	window.onunload = null;
}


function form_checkall( obForm ) {

	var coSpans = obForm.getElementsByTagName('span')
	var bReturn = true;

	for (var i=0;(i<coSpans.length);i++) {

		switch (coSpans[i].getAttribute('type')) {
			case 'stringcheck' :
				if (!form_checkRegExp(coSpans[i],bReturn)) { bReturn = false; }
				break;
			case 'groupcheck' :
				if (!form_checkGroup(coSpans[i])) { bReturn = false; }
				break;
		}
	}
	return bReturn;
}
				
function form_addWarnIcon( obSpan, errmsg, bShowMess ) {
	var warnimage = obSpan.firstChild;
	if (warnimage==null) {
		warnimage =  document.createElement('img');
		warnimage.src= stFormWarnImage;
		warnimage.setAttribute('alt',errmsg);
		warnimage.setAttribute('class','warn');
		warnimage.setAttribute('title',errmsg);
		warnimage.setAttribute('align',bShowMess?'absmiddle':'top');
		obSpan.insertBefore(warnimage,null);
		if (bShowMess) {
			var messtext = document.createTextNode(' ' + errmsg);
			obSpan.appendChild(messtext);
		}
	}
}

function form_removeWarnIcon( obSpan ) {
	var warnimage = obSpan.firstChild;
	if (warnimage!=null) {
		obSpan.removeChild(warnimage);
		var messtext = obSpan.firstChild;
		if (messtext!=null) obSpan.removeChild(messtext);
	}
}

function form_checkRegExp( obSpan, bFocus ) {
	
	var rgCheck = new RegExp(obSpan.getAttribute('regexp'), "gi");
	var inputbox = document.getElementById(obSpan.getAttribute('inputname'));
	var val = inputbox.value; 
	
	if (!rgCheck.test(val)) {
		inputbox.style.backgroundColor='#FFDDDD';
		var errmsg = obSpan.getAttribute('message');
		inputbox.setAttribute('title',errmsg);
		
		form_addWarnIcon( obSpan, errmsg,false )

		if (bFocus) inputbox.focus();
		return false;
	} else {
		inputbox.style.backgroundColor=null;
		inputbox.title=null;
		form_removeWarnIcon( obSpan )
		return true;
	}
}

function form_checkGroup( obSpan ) {

	var nMin = obSpan.getAttribute('min');
	var nMax = obSpan.getAttribute('max');
	var coChecks = document.getElementsByName(obSpan.getAttribute('inputname'));
	var nCurrent = 0;
	
	for (var i=0;i<coChecks.length;i++) {
		if (coChecks[i].checked) { nCurrent++ }
	}
	
	if (nCurrent>nMax||nCurrent<nMin) {
		form_addWarnIcon( obSpan, obSpan.getAttribute('message'), true )
		return false;
	} else {
		form_removeWarnIcon( obSpan )
		return true;
	}
}

function form_popout( url, value, passbackLabel, passbackValue, multiple ) {
	if (url.indexOf('?') > -1 ) {
		url += '&';
	} else {
		url += '?';
	}

	url += 'pbmultiple=' + multiple + '&pblabel=' + passbackLabel + '&pbValue=' + passbackValue + '&popFinalVals=' + document.getElementById(passbackValue).value

	window.open( url, '_blank', 'location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0,width=400,height=300', false);
}


function form_checkdate( name ) {
	var obNull = document.getElementById(name + '_null');
	if (obNull.checked) {
		document.getElementById(name).value = 'NULL';
	} else {
		var obDay = document.getElementById(name + '_day');
		var obMonth = document.getElementById(name + '_month');
		var obYear = document.getElementById(name + '_year');
		var dtThis = new Date(obYear.options(obYear.selectedIndex).value, obMonth.options(obMonth.selectedIndex).value-1, obDay.options(obDay.selectedIndex).value);
		document.getElementById(name).value = dtThis.getFullYear() + '-' + (dtThis.getMonth()+1) + '-' + dtThis.getDate();

		obDay.selectedIndex = dtThis.getDate()-1;
		obMonth.selectedIndex = dtThis.getMonth();
		for (var i=0;i<obYear.options.length;i++) if (obYear.options(i).value==dtThis.getFullYear()) obYear.selectedIndex=i;
	}
}
	

function clearBox(formItem) {
	for(i = formItem.length-1 ; i > -1 ; i--) {
		formItem.options[i] = null;
	}                                                   
}

function addBoxItem(formItem, valText, valValue) {
	newoption = new Option(valText, valValue, false, false);
	formItem.options[formItem.length] = newoption;                                          
}

function sortSelect(obSelect) {
	var arValues = Array()
	for (i=0 ; i<obSelect.length; i++) {
		arValues[i] = Array(obSelect.options[i].text, obSelect.options[i].value)
	}
	arValues.sort(sortSelectAlphabeticaly);
	
	clearBox(obSelect);
	
	for (i=0 ; i<arValues.length ; i++) {
		addBoxItem(obSelect, arValues[i][0], arValues[i][1])
	}
}

function sortSelectAlphabeticaly(a, b){
	if (a[0].toLowerCase() > b[0].toLowerCase()) {
		return 1
	} else if (a[0].toLowerCase() < b[0].toLowerCase()) {
		return -1
	} else {
		return 0;
	}
}

function swap(obj, prop, state1, state2) {
	thing = 'obj.' + prop;
	if (eval(thing+'==state1')) {
		eval(thing+'=state2');
	} else {	
		eval(thing+'=state1')
	}
}

function swap2(obj, prop, state1, state2, obj2, prop2, state21, state22) {
	thing = 'obj.' + prop;
	thing2 = 'obj2.' + prop2;
	if (eval(thing+'==state1')) {
		eval(thing+'=state2');
		eval(thing2+'=state22');
	} else {
		eval(thing+'=state1');
		eval(thing2+'=state21')
	}
}

function toggle( id ) {
	if (typeof(id)=='string') {
		id = document.getElementById(id); 
	};
	swap(id.style, 'display', '', 'none');
	return false;
}

function flip(section,link) {
	obj=document.getElementById(section).style;
	swap(obj, 'display', 'none', '');
	swap(link, 'innerHTML', '+', '-');
	return false;
}

function borderFlip(id) {
	obj=document.getElementById('sect'+id).style;
	img=document.getElementById('sectimg'+id);
	swap2(obj, 'display', 'none', '', img, 'src', '/img/downarrow.gif', '/img/uparrow.gif');
}

function resizeframe() {
	window.frameElement.style.height=document.body.scrollHeight+'px';
}

function noBubble() {
	event.cancelBubble= true;
	return false;
}

function openColourPicker ( obj, val ) {
	window.open('/@@global/form/colourPicker/picker.asp?name=' + obj.name + '&colour=' + encodeURIComponent(val), 'colourPicker', 'width=590,height=430,toolbar=no,status=no,resizable=no,menubar=no,location=no');
}

