//------------------------------
// INC_jsTools.js is a library of useful js functions, like opening pop up windows,  etc.
//------------------------------

function jsTools_openWin(winPage, winName, winParams){
	var win = window.open(winPage, winName, winParams);
	win.focus();
}

function jsTools_strTrim(str) {
    var i = 0;    
    var j = str.length - 1;    
                    
    trimstr = "";    
    if (j < 0) return trimstr;    
    
    flagbegin = true;    
    flagend = true;  
      
    while (flagbegin == true) {    
        if (str.charAt(i) == ' ') i++;    
        else flagbegin = false;    
    }
         
    while (flagend == true) {    
        if (str.charAt(j) == ' ') j--;
        else flagend = false;    
    }    

    if (j < i) return trimstr;
    else trimstr = str.substring(i, j + 1);    
                    
    return trimstr;    
}

var l_popup_win;

function jsTools_close_popup()
{
	if (l_popup_win)
	{	
		l_popup_win.close();
		l_popup_win = null;
	}
}

window.onfocus = jsTools_close_popup;

function jsTools_popup(url, width, height)
{
	l_popup_win=window.open(url, "KinteraSphere",
			"menubar=no,width="+width+",height="+height+",scrollbars,resizable");
	l_popup_win.focus();
}

function jsTools_popup2(url, winParams)
{
	l_popup_win=window.open(url, "KinteraSphere", winParams);
	l_popup_win.focus();
}

function jsTools_popup_calendar2(root_path, element, name, type)
{
	var esc;
	if (type == 1)	//input box in a form
		esc = escape(element.value) + "&inp=" + name;
	else if (type == 100)
		esc = escape(element.value) + "&inp=" + name + "&cs=1";
	else
		esc = escape(element.month.value+"/"+element.day.value+"/"+element.year.value) + "&form=" + name;
		
	jsTools_popup(root_path + "/common/asp/calendar_popup.asp?date="+esc, 240, 265);
}

function jsTools_popup_calendar3(root_path, element, name, type, winParams)
{
	var esc;
	if (type == 1)	//input box in a form
		esc = escape(element.value) + "&inp=" + name;
	else if (type == 100)
		esc = escape(element.value) + "&inp=" + name + "&cs=1";
	else
		esc = escape(element.month.value+"/"+element.day.value+"/"+element.year.value) + "&form=" + name;
		
	jsTools_popup2(root_path + "/common/asp/calendar_popup.asp?date="+esc, "width=240,height=265,"+winParams);
	return l_popup_win;
}

function jsTools_popup_calendar(element, name, type)
{
	jsTools_popup_calendar2("../..", element, name, type);
}

function ks_button_chgclr(ctrl, action)
{
	var st = ctrl.style;
	if (st == null) return;		//strange enough, sometimes can't get style object in some browsers
	
	if (action==1)	//down
	{
		st.borderLeftColor = "#001B3D";
		st.borderTopColor = "#001B3D";
		st.borderRightColor = "#365B7F";
		st.borderBottomColor = "#365B7F";
	}
	else if (action==2)	//up
	{
		st.borderLeftColor = "#365B7F";
		st.borderTopColor = "#365B7F";
		st.borderRightColor = "#001B3D";
		st.borderBottomColor = "#001B3D";
	}
	else if (action==3)	//enter
	{
	/*
		st.color = "#4444ff";
	*/
	}
	else if (action==4)	//leave
	{
		st.borderLeftColor = "#365B7F";
		st.borderTopColor = "#365B7F";
		st.borderRightColor = "#001B3D";
		st.borderBottomColor = "#001B3D";

		st.color = "#ffffff";
	}
}

function ks_button_enter() { ks_button_chgclr(window.event.srcElement, 3); }
function ks_button_leave() { ks_button_chgclr(window.event.srcElement, 4); }
function ks_button_down() { ks_button_chgclr(window.event.srcElement, 1); }
function ks_button_up() { ks_button_chgclr(window.event.srcElement, 2); }
function ks_button_disable(btn, b_disabled)
{
	var st = btn.style;
	if (b_disabled)
	{
		btn.disabled = true;
		st.borderLeftColor = "#e0e0e0";
		st.borderTopColor = "#e0e0e0";
		st.borderRightColor = "#606060";
		st.borderBottomColor = "#606060";
		st.backgroundColor = "#c0c0c0";
		st.cursor = "default"
	}
	else
	{
		btn.disabled = false;
		st.borderLeftColor = "#ffb482";
		st.borderTopColor = "#ffb482";
		st.borderRightColor = "#a0522d";
		st.borderBottomColor = "#a0522d";
		st.backgroundColor = "#ff6000";
		st.cursor = "hand"
	}
}

function ks_button_dosubmit(btn, submit_value)
{
	btn.form.submit_type.value = submit_value;
	btn.form.submit();
}
