var contextRoot = "/orangut";

function login(backURL)
{
	$("#divLoginPopup").show();
	$("#loginPopupLoadingMsg").show();
	$("#loginPopupContent").hide();
	
	var loginParams = new Object();
	loginParams.backURL = backURL;
	$.post(contextRoot + "/faces/ui/login/Login.jsp" , loginParams, function(value)
	{
		displayIdContent("#loginPopupContent", value);
		$("#loginPopupLoadingMsg").hide();
		$("#loginPopupContent").show();
		$("#username").focus();
	});

	return false;
}

function showAjaxPopup(url, params, contentDiv, popupDiv)
{
	$.post(url, params, function(value)
	{
		displayIdContent(contentDiv, value);
		$(popupDiv).show();
	});

	return false;
}

function showRegisterPopup()
{
	var loginParams = "";
	$.post(contextRoot + "/faces/ui/register/Register.jsp" , loginParams, function(value)
	{
		displayIdContent("#popupContent", value);
		$("#commonPopup").show();
	});

	return false;
}

function closePopup()
{
	$("#divLoginPopup").hide();
}

function displayIdContent(id, value)
{
	if(navigator.userAgent.indexOf('MultiSafari') == -1)
	{
		$(id).html(value);
	}
	else
	{
		$(id).html("<div>" + value + "</div>");
	}
}

function displayYesNoPopup()
{
	var loginParams = "";
	$.post(contextRoot + "/faces/ui/utils/YesNoPopup.jsp" , loginParams, function(value)
	{
		displayIdContent("#popupContent", value);
		$("#commonPopup").show();
	});

	return false;
}

function isNumeric(val)
{
	var validChars = '0123456789.';

	for(var i = 0; i < val.length; i++)
	{
		if(validChars.indexOf(val.charAt(i)) == -1)
			return false;
	}

	return true;
}

function isPositiveInteger(val)
{
	var validChars = '0123456789';

	for(var i = 0; i < val.length; i++)
	{
		if(validChars.indexOf(val.charAt(i)) == -1)
			return false;
	}

	return true;
}

function getMinLenMsg(minLen)
{
	return ("Short! Enter at least " + minLen + " letters");
}

function getInvalidEmailMsg()
{
	return ("Please enter a valid email");
}

function resetRequiredTextbox(id)
{
	var tbValue = $(id).attr('value');
	var tbAltText = $(id).attr('alt');
	
	if(tbValue == tbAltText)
		$(id).attr('value', '');
}

function resetMinLengthTextbox(id, minLen)
{
	var minLenMsg = getMinLenMsg(minLen);
	var tbValue = $(id).attr("value");
	var tbAltText = $(id).attr("alt");
	
	if(tbValue == tbAltText || tbValue == minLenMsg)
		$(id).attr("value", "");
}

function resetEmailTextbox(id)
{
	var invalidEmailMsg = getInvalidEmailMsg();
	var tbValue = $(id).attr('value');
	var tbAltText = $(id).attr('alt');
	
	if(tbValue == tbAltText || tbValue == invalidEmailMsg)
		$(id).attr('value', '');
}

function resetPositiveIntegerTextbox(id)
{
	if($(id).hasClass('errorTextBox'))
		$(id).attr('value', '');
}

function validateRequiredTextbox(id)
{
	var tbValue = $(id).attr('value');
	var tbAltText = $(id).attr('alt');
	
	if(tbValue == '' || tbValue == tbAltText)
	{
		$(id).addClass('errorTextBox');
		$(id).attr('value', tbAltText);
		return false;
	}
	else
	{
		$(id).removeClass('errorTextBox');
		return true;
	}
}

function validateMinLengthTextbox(id, minLen)
{
	var minLenMsg = getMinLenMsg(minLen);
	var tbValue = $(id).attr('value');
	var tbAltText = $(id).attr('alt');
	
	if(tbValue == '' || (tbValue == tbAltText) || (tbValue == minLenMsg))
	{
		$(id).addClass('errorTextBox');
		
		if(tbValue == minLenMsg)
			$(id).attr('value', minLenMsg);
		else
			$(id).attr('value', tbAltText);
		
		return false;
	}
	else
	{
		if(tbValue.length < minLen)
		{
			$(id).addClass('errorTextBox');
			$(id).attr('value', minLenMsg);
			return false;
		}
		else
		{
			$(id).removeClass('errorTextBox');
			return true;
		}
	}
}

function validateEmailTextbox(id)
{
	var emailRegex = /^[a-z0-9._-]+@([a-z0-9.-]+\.)+[a-z0-9.-]{2,4}$/;
	var invalidEmailMsg = getInvalidEmailMsg();
	var enteredEmail = $(id).attr('value').toLowerCase();
	var tbAltText = $(id).attr('alt');
	
	if(enteredEmail == '' || enteredEmail == tbAltText || enteredEmail == invalidEmailMsg)
	{
		$(id).addClass('errorTextBox');
		
		if(enteredEmail == invalidEmailMsg)
			$(id).attr('value', invalidEmailMsg);
		else
			$(id).attr('value', tbAltText);
		
		return false;
	}
	else
	{
		if(emailRegex.test(enteredEmail) == false)
		{
			$(id).addClass('errorTextBox');
			$(id).attr('value', invalidEmailMsg);
			return false;
		}
		else
		{
			$(id).removeClass('errorTextBox');
			return true;
		}
	}
}

function validateFileUploadBox(id)
{
	var filePath = $(id).attr('value');
	
	if(filePath == null || filePath == '')
	{
		$(id).addClass('errorTextBox');
		return false;
	}
	else
	{
		$(id).removeClass('errorTextBox');
		return true;
	}
}

function validatePasswordBox(id, minLen, lblId)
{
	var minLenMsg = getMinLenMsg(minLen);
	var tbValue = $(id).attr("value");
	var tbAltText = $(id).attr("alt");
	
	if(tbValue == "" || (tbValue == tbAltText) || (tbValue == minLenMsg))
	{
		$(id).addClass("errorTextBox");
		
		if(tbValue == minLenMsg)
			$(lblId).text(minLenMsg);
		else
			$(lblId).text(tbAltText);
		
		return false;
	}
	else
	{
		if(tbValue.length < minLen)
		{
			$(id).addClass("errorTextBox");
			$(lblId).text(minLenMsg);
			return false;
		}
		else
		{
			$(id).removeClass("errorTextBox");
			$(lblId).text("");
			return true;
		}
	}
}

function validateDropDownList(id, descIndex)
{
	if($(id).attr("selectedIndex") == parseInt(descIndex))
	{
		$(id).addClass('errorTextBox');
		return false;
	}
	else
	{
		$(id).removeClass('errorTextBox');
		return true;
	}
}

function validatePositiveIntegerTextbox(id, errMsgId)
{
	var tbValue = $(id).attr('value');
	
	if(tbValue == '' || isPositiveInteger(tbValue) == false)
	{
		$(id).addClass('errorTextBox');
		$(errMsgId).show();
		return false;
	}
	else
	{
		$(id).removeClass('errorTextBox');
		$(errMsgId).hide();
		return true;
	}
}

$("#btnResendActivationMail").ready(function()
{
	$("#btnResendActivationMail").click(function()
	{
		$("#lblResendingActivationEmail").text("Please wait we re-send your account activation email...");
	});
});


function getQueryVariable(variable)
{
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	
	for (var i=0;i<vars.length;i++)
	{
		var pair = vars[i].split("=");
		if (pair[0] == variable)
			return pair[1];
	}
	
	return null;
}

function copyTextboxValueToLabel(tbId, lblId)
{
	$(lblId).text($(tbId).val());
}

function initYesNoDialog(divId, buttonId)
{
	$(divId).dialog(
		{
			bgiframe: true,
			resizable: false,
			modal: true,
			autoOpen: false,
			width: 400,
			overlay:
			{
				backgroundColor: '#000000',
				opacity: 0.4
			},
			
			buttons:
			{
				'Yes': function()
					{
						$(buttonId).click();
						$(this).dialog('destroy');
					},
				
				'No': function()
					{
						$(this).dialog('destroy');
					}
			}
		}
	);
};

function showYesNoDialog(divId, buttonId)
{
	initYesNoDialog(divId, buttonId);
	
	if($(divId).dialog('isOpen') == false)
	{
		$(divId).dialog('open');
		return false;
	}
	
	else
		if($(divId).dialog('isOpen') == true)
			return true;
		else
			return false;
}

function initMessageBox(divId)
{
	$(divId).dialog(
		{
			bgiframe: true,
			resizable: false,
			modal: true,
			autoOpen: false,
			width: 450,
			overlay:
			{
				backgroundColor: '#000000',
				opacity: 0.4
			},
			
			buttons:
			{
				'Close': function()
					{
						$(this).dialog('destroy');
					}
			}
		}
	);
};

function showMessageBox(divId)
{
	initMessageBox(divId);
	
	if($(divId).dialog('isOpen') == false)
	{
		$(divId).dialog('open');
		return false;
	}
	
	else
		if($(divId).dialog('isOpen') == true)
			return true;
		else
			return false;
}

function getMonthShortName(monthIndex)
{
	var months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	return months[monthIndex];
}

function toggleDivOnCheckbox(cbId, divId)
{
	if($(cbId).attr("checked") == true)
		$(divId).show();
	else
		$(divId).hide();
}

function extractSuffixedId(div, prefix)
{
	var divId = $(div).attr("id");
	return divId.substring(prefix.length, divId.length);
}

function encodeHtmlForAjax(htmlString)
{
	return htmlString.replace(/%/g, "%25").replace(/&/g, "%26").replace(/\+/g, "%2B");
}

/* Use JS to hide the JS-disability message box. */
$(document).ready(function(){$("#divNoJsSupport").hide();});