/* 
 * Javascripts
 */

/* Global variables */

var IMAGE_PREFIX = '';
var MINIMAL_SPACE_3 = '<span style="font-size: 1pt; width: 3px; height: 3px;">&nbsp;</span>';


/* Common scripts */

function stripNull(s) { 
	if(s==null)
		return '';
	return s;
}

function flatListContains(value, sep, find) {
	var va = value.split(sep);
	for(var i=0;i<va.length;i++) {
		if(va[i] == find)
		return true;
	}
	return false;
}

function flatListAdd(value, sep, newval) {
	if(stripNull(value)=='')
		return newval;
	return value + sep + newval;
}

function flatListRemove(value, sep, remval) {
	var va = value.split(sep);
	var res = '';
	for(var i=0;i<va.length;i++) {
		if(va[i]!=remval)
		res += va[i] + sep;
	}
	if(res!='')
		res = res.substr(0,res.length-1);
	return res;
}

function getWindowFromTarget(target) {
	if(target == '_top')
		return window.top;
	return window.top.frames[target];
}

function windowOpen(url,target) {
	if(target=='_blank') {
		window.open(url);
	} else if(target.substring(0,7)=='_blank/') {
		window.open(url, target.substring(7));
	} else if(target=='_popup') {
		window.open(url, '', 'width=750,height=500,resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=yes,menubar=no');
	} else if(target.substring(0,7)=='_popup/') {
		window.open(url, target.substring(7), 'width=750,height=500,resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=yes,menubar=no');
	} else {
		var w = getWindowFromTarget(target);
		w.location = url;
	}
}

function submit(attachTarget,url,target,part,submitId) { 
	var w,f;
	
	if(attachTarget == 'gimlicrm') {
		f = document.forms['submitForm'];
	} else {
		w = getWindowFromTarget(attachTarget);
		f = w.document.forms['submitForm'];
	}
	
	if(f != null) {
		if(url != '')
			f.action = url;
		if(target != '')
			f.target = target;
		if(part != '')
			f.part.value = part;
		if(submitId != '')
			f.submitid.value = submitId;
		f.submit();
	}
}

/* Control Helper */

function getControlTopWindow(c) {
	if(getControlTopWindowHelper(window.top, c))
		return window.top;
	
	if(window.top.opener != null && window.top.opener != window)
		if(getControlTopWindowHelper(window.top.opener.top, c))
			return window.top.opener.top;
	
	return null;
}

function getControlTopWindowHelper(w,c) {
	var i;
	var f;
	
	if(w.document.forms[c.form.name] != null)
		return true;
	
	for(i=0;i<w.frames.length;i++) {
		if(getControlTopWindowHelper(w.frames[i],c))
			return true;
	}

	return false;
}


function findFormControl(controlName) {
	var c = findFormControlHelper(window,controlName);
	if(c == null && window.opener != null && window.opener != window)
		c = findFormControlHelper(window.opener,controlName);
	return c;
}

function findFormControlHelper(w,controlName) {
	var i;
	var c;
	
	for(i=0;i<w.document.forms.length;i++) {
		c=w.document.forms[i].elements[controlName];
		if(c != null)
			return c;
	}

	for(i=0;i<w.frames.length;i++) {
		c = findFormControlHelper(w.frames[i],controlName);
		if(c != null)
			return c;
	}
	
	return null;
}

/* Expects to be run from the window the control resides in */
function htmlListAdd(c,id,desc) {
	c.options[c.options.length] = new Option(desc,id,true,true);
}

function htmlListRemove(c,index) {
	if(index >= 0)
		c.options[index] = null;
	else if(c.selectedIndex >= 0)
		c.options[c.selectedIndex] = null;
}

var activeWindow = null;
function setInitialFocus(w) {
	activeWindow = w;
	setInitialFocusHelper();
	/* Jumping annoyning
	if(REQUIRES_JAVA=='true')
		window.setTimeout("setInitialFocusHelper()",1000);
		*/
}
function setInitialFocusHelper() {
	var f,t,i;
	// activeWindow.focus();
	f = activeWindow.document.forms['submitForm'];
	if(f != null) {
		for(i=0;i<f.elements.length;i++) {
			t = f.elements[i];
			if(t.type!='hidden' && t.name.indexOf("_description") == -1 ) {
				t.focus();
				break;
			}
		}
	}
}

/* Control scripts */

function textControlAssign(controlName,value) { 

	var c = findFormControl(controlName);
	if(c == null)
		return;
	
	c.value = value;
}

function textControlAppend(controlName,value,separator) { 
	
	var c = findFormControl(controlName);
	if(c == null)
		return;
	
	if(c.value == '')
		c.value = value;
	else if(c.length >= separator.length && c.substr(c.length-separator.length) == separator)
		c.value += value;
	else
		c.value += separator + value;
}

function textControlValue(controlName) { 

	var c = findFormControl(controlName);
	if(c == null)
		return '';
	
	return c.value;
}

function linkControlAssign(controlName,id,desc) { 
	
	var c = findFormControl(controlName);
	var cd = findFormControl(controlName + '_description');
	if(c == null || cd == null)
		return;
	
	c.value = id;
	cd.value = desc;
	
}

function linkControlClear(controlName) { 
	
	var c = findFormControl(controlName);
	var cd = findFormControl(controlName + '_description');
	if(c == null || cd == null)
		return;
	
	c.value = '';
	cd.value = '';
	
}

function multiLinkControlAdd(controlName,id,desc,sep) { 

	var c = findFormControl(controlName);
	var cd = findFormControl(controlName + '_description');
	if(c == null || cd == null)
		return;
	
	if(!flatListContains(c.value, sep, id)) {
		c.value = flatListAdd(c.value, sep, id);
		getControlTopWindow(cd).htmlListAdd(cd, id, desc);
	}

}

function multiLinkControlRemove(controlName,sep) { 
	var c = findFormControl(controlName);
	var cd = findFormControl(controlName + '_description');
	if(c == null || cd == null)
		return;
	
	if(cd.selectedIndex >= 0) {
		var id = cd.options[cd.selectedIndex].value;
		c.value = flatListRemove(c.value, sep, id);
		htmlListRemove(cd, -1);
	}

}

/* Button scripts */
/*
function ButtonMouseDown(w,id) { 
	w.document.getElementById(id).className='clsButtonDown';
}

function ButtonMouseUp(w,id) { 
	w.document.getElementById(id).className='clsButtonOver';
}

function ButtonMouseOver(w,id) { 
	w.document.getElementById(id).className='clsButtonOver';
}

function ButtonMouseOut(w,id) { 
	w.document.getElementById(id).className='clsButton';
}
*/

/* Write elements */

var buttonHtml =
'<table class="clsButton" id="_ID_" cellspacing="0" cellpadding="0"' +
	' onmousedown="this.className=\'clsButtonDown\';void(0);"' +
	' onmouseup="this.className=\'clsButtonOver\';_ID_();void(0);"' +
	' onmouseover="this.className=\'clsButtonOver\';_SHOW_DESC_PART_window.status=window.status;return true;"' +
	' onmouseout="this.className=\'clsButton\';_HIDE_DESC_PART_void(0);">' +
	'<tr height="3">' +
		'<td width="3" class="clsButtonLeftTop">' +
		'</td>' +
		'<td colspan="_IMAGE_LABEL_COUNT_" class="clsButtonTop">' +
			'_MINIMAL_SPACE_3_' +  
		'</td>' +
		'<td width="3" class="clsButtonRightTop">' +
		'</td>' +
	'</tr>' +
	'<tr>' +
		'<td width="3" class="clsButtonLeft">' +
			'_MINIMAL_SPACE_3_' +  
		'</td>' +
		'_IMAGE_PART_' +
		'_LABEL_PART_' +
		'<td width="3" class="clsButtonRight">' +
			'_MINIMAL_SPACE_3_' +  
		'</td>' +
	'</tr>' +
	'<tr height="3">' +
		'<td width="3" class="clsButtonLeftBottom">' +
		'</td>' +
		'<td colspan="_IMAGE_LABEL_COUNT_" class="clsButtonBottom">' +
			'_MINIMAL_SPACE_3_' +  
		'</td>' +
		'<td width="3" class="clsButtonRightBottom">' +
		'</td>' +
	'</tr>' +
'</table>';

function WriteButton(doc,id,img,lbl,desc) {

	var s = buttonHtml;
	var t;
	var i = 2;
	
	if(img != '') {
		t = 
		'<td class="clsButtonImage">' +
		'<img class="clsButtonImage" src="_IMAGE_PREFIX__IMAGE_.gif"></img>' +
		'</td>';
	} else {
		t = '';
		i = 1;
	}
	s = s.replace(/_IMAGE_PART_/g,t);
	

	if(lbl != '') {
		t =
		'<td class="clsButtonLabel">' +
		'_LABEL_' +
		'</td>';
	} else {
		t = '';
		i = 1;
	}
	s = s.replace(/_LABEL_PART_/g,t);
	
	s = s.replace(/_IMAGE_LABEL_COUNT_/g,i);
	
	if(desc != '') {
		s = s.replace(/_SHOW_DESC_PART_/g,'showtip(this,event,\'_DESC_\');');
		s = s.replace(/_HIDE_DESC_PART_/g,'hidetip();');
	} else {
		s = s.replace(/_SHOW_DESC_PART_/g,'');
		s = s.replace(/_HIDE_DESC_PART_/g,'');
	}
	
	s = s.replace(/_IMAGE_PREFIX_/g,IMAGE_PREFIX);
	s = s.replace(/_MINIMAL_SPACE_3_/g,MINIMAL_SPACE_3);
	s = s.replace(/_ID_/g,id);
	s = s.replace(/_IMAGE_/g,img);
	s = s.replace(/_LABEL_/g,lbl);
	s = s.replace(/_DESC_/g,desc);
	
	doc.write(s);
}

function WriteWelcomeButton(doc,id,img,lbl) {

	var s = buttonHtml;
	var t;
	var i = 2;
	
	if(img != '') {
		t = 
		'<td class="clsButtonImage">' +
		'<img class="clsButtonImage" src="_IMAGE_PREFIX__IMAGE_.gif"></img>' +
		'</td>';
	} else {
		t = '';
		i = 1;
	}
	s = s.replace(/_IMAGE_PART_/g,t);
	
	if(lbl != '') {
		t =
		'<td class="clsButtonLabel">' +
		'_LABEL_' +
		'</td>';
	} else {
		t = '';
		i = 1;
	}
	s = s.replace(/_LABEL_PART_/g,t);
	
	s = s.replace(/_IMAGE_LABEL_COUNT_/g,i);
	
	s = s.replace(/_SHOW_DESC_PART_/g,'document.getElementById(\'initialDescription\').className=\'clsWelcomeDescription\';document.getElementById(\'_ID_Description\').className=\'clsWelcomeDescriptionOver\';');
	s = s.replace(/_HIDE_DESC_PART_/g,'document.getElementById(\'_ID_Description\').className=\'clsWelcomeDescription\';');
	
	s = s.replace(/_IMAGE_PREFIX_/g,IMAGE_PREFIX);
	s = s.replace(/_MINIMAL_SPACE_3_/g,MINIMAL_SPACE_3);
	s = s.replace(/_ID_/g,id);
	s = s.replace(/_IMAGE_/g,img);
	s = s.replace(/_LABEL_/g,lbl);
	
	doc.write(s);
}

/* Status bar functions */
    
function tickTimeTextBox(w,id) {

	var tb = w.document.getElementById(id);
	var s = tb.value;
	
	if(s == '')
		return;
	
	var a = timeTextToArray(s);

	a[2]++;

	if(a[2] >= 60) {
		a[2] = 0;
		a[1]++;
	}

	if(a[1] >= 60) {
		a[1] = 0;
		a[0]++;
	}

	tb.value = arrayToTimeText(a);
}

function timeTextToArray(s) {

	//alert(s);

	var a = s.split(':');
	var b = new Array(0,0,0);
	
	if(a.length == 2) {
		b[1] = a[0];
		b[2] = a[1];
	} else if(a.length == 3) {
		b[0] = a[0];
		b[1] = a[1];
		b[2] = a[2];
	}

	//alert('r1: ' + a[0] + ' ' + a[1] + ' ' + a[2] + ' ' + a.length);
	//alert('r2: ' + b[0] + ' ' + b[1] + ' ' + b[2] + ' ' + b.length);
	//alert('r: ' + arrayToTimeText(b) + ' ' + a.length);
	
	return b;
}
    
function arrayToTimeText(a) {
	return a[0] + ':' + zeroPad(a[1]) + ':' + zeroPad(a[2]);
}
    
function zeroPad(theNumber) {
    var ret='0'+theNumber;
    return ret.substr(ret.length-2,2);
}

