/* Private functions */
var l_popup_win;

function popupWinClosePopup()
{
    try{
	    if (l_popup_win != null && !l_popup_win.closed)
	    {
		    l_popup_win.close();
		    l_popup_win = null;
	    }
	}catch(e){}
}


function jsTools_popup2(url, winParams){
	l_popup_win=window.open(url,"KinteraSphere",winParams);
	l_popup_win.focus();
}
window.onfocus = popupWinClosePopup;

/* Public functions */

function popupVolatileWindow(url, width, height, b_has_top_bars)
{
	popupWinClosePopup();
	//getting permission error... disable for now.
	//l_popup_win = popupStickyWindow(url, width, height, b_has_top_bars);
	return popupStickyWindow(url, width, height, b_has_top_bars);
}

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 if (type == 200)
		esc = escape(element.start_month.value+"/"+element.start_day.value+"/"+element.start_year.value) + "&form=" + name + "&inp=" + type;
	else if (type == 300)
		esc = escape(element.end_month.value+"/"+element.end_day.value+"/"+element.end_year.value) + "&form=" + name + "&inp=" + type;
	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 if(type==400)
		esc = escape(element.value) + "&inp=" + type
	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 popupStickyWindow(url, width, height, b_has_top_bars)
{
	var bar_str = b_has_top_bars ? "yes" : "no";
	var curr_time = new Date();
	w_popup=window.open(url, "KinteraSphere" + curr_time.getHours() + curr_time.getMinutes() + curr_time.getSeconds(),
			"width="+width+",height="+height+",scrollbars,resizable,menubar=" + bar_str + ",toolbar=" + bar_str);
	w_popup.focus();
	return w_popup;
}

function ks_button_chgclr(ctrl, action)
{
	var bright, dark;
	var st = ctrl.style;
	if (st == null) return;		//strange enough, sometimes can't get style object in some browsers
	
	if (ctrl.className == "LRButton")
	{
		bright = "#d0d0d0";
		dark = "#707070";
		
		if (action==1)	//down
		{
			st.borderLeftColor = dark;
			st.borderTopColor = dark;
			st.borderRightColor = bright;
			st.borderBottomColor = bright;
		}
		else if (action==2)	//up
		{
			st.borderLeftColor = bright;
			st.borderTopColor = bright;
			st.borderRightColor = dark;
			st.borderBottomColor = dark;
		}
	}
	else if (ctrl.className == "htmlbutton" || ctrl.className == "htmlbutton_down")
	{
		if (action==1)	//down
			ctrl.className == "htmlbutton_down";
		else if (action==2)	//up
			ctrl.className == "htmlbutton";
	}
}

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;
		// the style is set to HtmlButton class defined in /CommonLib/style/contacts_crm.css
		// this section of code is primarily used by Query Tool
		st.borderLeftColor = "#BFD3E6";
		st.borderTopColor = "#BFD3E6";
		st.borderRightColor = "#003C79";
		st.borderBottomColor = "#003C79";
		st.backgroundColor = "#5998C9";
		st.cursor = "hand";
	}
}

function confirmUnsavedChangesLost()
{
	if(typeof(GetMode)== "undefined")
		return true;
	else if(GetMode() == 1)
		return true;
	else
		return confirm("Any unsaved changes will be lost.\nClick OK to discard unsaved changes and continue.\nClick Cancel to avoid losing unsaved changes and stay in the current page.");
}

function showHideSolutions(solutionID)
{
	var solButton = document.getElementById("SpPageSolutionToggle");
	var solSection = document.getElementById("SpPageSolutionBox");
	if(solButton.value == "Show Solutions")
	{
		solButton.value = "Hide Solutions";
		solSection.style.display = "block";
		document.cookie = solutionID + "_solu_display=block;path=/;";
	}
	else
	{								   
		solButton.value = "Show Solutions";
		solSection.style.display = "none";
		document.cookie = solutionID + "_solu_display=none;path=/;";
	}
}

