﻿
// function parameters are: field - the string field, count - the field for remaining characters number and max - the maximum number of characters 
function countLeft(fieldControlId, countControlId, max) 
{
	var fieldControl = document.getElementById(fieldControlId);
	var countControl = document.getElementById(countControlId);
	// if the length of the string in the input field is greater than the max value, trim it 
	// else calculate the remaining characters
	if (fieldControl.value.length > max)
	{
	    fieldControl.value = fieldControl.value.substring(0, max);
	    if (navigator.appName == "Microsoft Internet Explorer")
			countControl.innerText = max - fieldControl.value.length;
		else
			countControl.textContent = max - fieldControl.value.length;
	}
	else
	{
		if (navigator.appName == "Microsoft Internet Explorer")
			countControl.innerText = max - fieldControl.value.length;
		else
			countControl.textContent = max - fieldControl.value.length;
	}
}

function subMenuDisplay(divId, isHover)
{
	document.getElementById(divId).style.display = isHover ? "" : "none";
}

function validatePasswordLength(sender, e)
{
	e.IsValid = e.Value.length >= 6 && e.Value.length < 50;
}

function validateImageType(sender, e)
{
	if (e.Value == "")
	{
		e.IsValid = false;
		return;
	}
	
	var filePostfixes = new Array(".jpg", ".jpeg", ".jpe", ".gif", ".png");
	e.IsValid = false;
	
	for (var i = 0; i < filePostfixes.length; i++)
	{
		if (e.Value.indexOf(filePostfixes[i]) > 0)
		{
			e.IsValid = true;
			break;
		}
	}
}

function openWindow(url, width, height)
{
    window.open(url, "termsWin", "width=" + width + ",height=" + height + ",toolbar=0,resizable=0,scrollbars=1");
}

function openModalWindow(url, width, height)
{
	var retVal;
	if (navigator.appName.indexOf("Explorer") != -1 && !window.opera) 
	{
		retVal = showModalDialog(url, "Window", "dialogHeight:" + height + "px; dialogWidth:" + width + "px"); // dialogLeft:" + vLeft + "px;dialogTop:" + vTop + "px");
	} 
	else 
	{
		retVal = window.open(url, "Window", "modal=1, height=" + height + "px, width=" + width + "px"); // left=" + vLeft + "px, top=" + vTop + "px");
		retVal.focus();
	}
	alert(retVal);
	if (retVal == "true")
		location.reload(true);
}

function closeWindow(update)
{
	window.returnValue = update; 
	window.close(); 
}

function isEnterKeyPressed(e)
{
	var key;
	if(window.event)
		key = window.event.keyCode; //IE
	else
		key = e.which; //firefox
		
	return key == 13;
}

function setSearchFunction(e)
{
	if (isEnterKeyPressed(e))
		__doPostBack(btnSearchId, '');
}

function setLoginButtonFocus(e)
{
	var btnLogin = document.getElementById(btnLoginId);
	
	if (isEnterKeyPressed(e))
		btnLogin.click();
}

function showField(id, show)
{
    document.getElementById(id).style.display = show ? "" : "none";
}