function swapImage( oImg , src )
{
	oImg.src = src
}
function testEmail(src) {
  var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
  return regex.test(src);
}

/******************************/
/* Add trim methods to string */

// Removes leading whitespaces
String.prototype.ltrim = function ()
{
	
	var re = /\s*((\S+\s*)*)/;
	return this.replace(re, "$1");
	
}

// Removes ending whitespaces
String.prototype.rtrim = function ()
{	
	var re = /((\s*\S+)*)\s*/;
	return this.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
String.prototype.trim = function ()
{
	return this.ltrim().rtrim();
}


/******************************/


// Proper int parsing function
function parseIntBase10(value)
{
	var regex  = /(^-?\d\d*$)/;
	if (regex.test(value))
		return parseInt(value, 10);
	else
		return NaN;
}


// Perform submit for enter key and cancel for esc key
function KeyCapture(e, idPrefix)
{
	e = e || window.event;
	idPrefix += "_";
	
	// Enter key pressed, make sure we're not validating
	if (e.keyCode == 13 && !g_bValidating)
	{
		document.getElementById(idPrefix + "btnSubmit").click();
		return false;
	}
	else if (e.keyCode == 27)
	{
		document.getElementById(idPrefix + "btnCancel").click();
		return false;
	}
	else
		return true;
}

// Perform submit for enter key and cancel for esc key (Telerik grid form)
function KeyCaptureGridForm(e, submitButtonID, cancelButtonID)
{
	e = e || window.event;
	
	if (e.keyCode == 13)
	{
		document.getElementsByName(submitButtonID)[0].click();
		return false;
	}
	else if (e.keyCode == 27)
	{
		document.getElementsByName(cancelButtonID)[0].click();
		return false;
	}
	else
		return true;
}


// Enter edit mode when row is double clicked
function RowDoubleClick(index)
{
	this.Rows[index].Control.cells[this.Rows[index].Control.cells.length-2].firstChild.click();
}
	
// Validate an input control according to parameters
function ValidateFormField(inputControlID, controlType, minValue, maxValue, otherVar, errorMessage)
{
	var inputControl = document.getElementById(inputControlID);
	
	// Unavailable form field is always valid
	if (inputControl == null || inputControl.disabled || inputControl.parentNode.style.visibility == 'hidden')
	{
		return true;
	}
	
	// Put together valid condition
	var valid = 
		(controlType == 'String' && inputControl.value.trim().length >= 1) ||
		(controlType == 'Integer' && 
			!isNaN( parseIntBase10(inputControl.value) ) &&
			(minValue == null || (minValue != null && parseIntBase10(inputControl.value) >= minValue)) &&
			(maxValue == null || (maxValue != null && parseIntBase10(inputControl.value) <= maxValue))
		) ||
		(controlType == 'List' && inputControl.selectedIndex > 0) ||
		(controlType == 'Email' && testEmail(inputControl.value)) ||
		(controlType == 'Regex' && otherVar.test(inputControl.value)) ||
		(controlType == 'CCard' && ValidateCreditCard(inputControl.value)) ||
		(controlType == 'IsraCard' && ValidateIsraCard(inputControl.value));
		
	// Generate error
	if (!valid)
	{
		alert(errorMessage);
		inputControl.focus();
		
		if (controlType != 'List')
			inputControl.select();
	}
	
	return valid;
}

//====================================================

// Stores any open popups
var g_currentMenu = null;

// Close any open popups
function CloseOpenPopup()
{
	if (g_currentMenu == null)
		return;
	
	g_currentMenu.Close();
	g_currentMenu = null;
}
document.onclick = CloseOpenPopup;

// Class to contain popup-related information
function PopupMenu(elementID)
{
	// Store the element with specified ID
	this.Element = document.getElementById(elementID);
	this.Element.style.display = '';
	
	// Hide the menu
	this.Close = function()
	{
		this.Element.style.display = 'none';
		g_currentMenu = null;
		
		if (typeof(this.OnClose) == 'function')
			this.OnClose(this.Element);
	}
}

// Display the popup menu with specifed ID
function ShowPopupMenu(elementID, e)
{
	e = e || window.event;
	e.cancelBubble = true;
	
	if (g_currentMenu != null)
		g_currentMenu.Close();
	
	g_currentMenu = new PopupMenu(elementID);
	return g_currentMenu;
}

//====================================================


// Credit Card Validation Javascript
// copyright 12th May 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function ValidateCreditCard(s)
{
	// remove non-numerics
	var v = "0123456789";
	var w = "";
	for (i=0; i < s.length; i++)
	{
		x = s.charAt(i);
		if (v.indexOf(x,0) != -1)
			w += x;
	}

	// validate number
	j = w.length / 2;
	if (j < 6.5 || j > 8 || j == 7)
		return false;
	
	k = Math.floor(j);
	m = Math.ceil(j) - k;
	c = 0;
	
	for (i=0; i<k; i++)
	{
		a = w.charAt(i*2+m) * 2;
		c += a > 9 ? Math.floor(a/10 + a%10) : a;
	}
	
	for (i=0; i<k+m; i++)
		c += w.charAt(i*2+1-m) * 1;

	return (c%10 == 0);
}

function ValidateIsraCard(rawInput)
{
	if (!/^[0-9]{8,9}$/.test(rawInput))
		return false;
		
	// Add a leading zero if necessary
	var s = rawInput;
	if (s.length == 8)
		s = '0' + s;
		
	var sum = 0;
	for (var i = 0; i < 9; i++)
	{
		sum += parseIntBase10(s.charAt(8-i)) * (i+1);
	}

	return (sum % 11 == 0);
}


function ReplaceQueryString( url, param, strValue ) 
{
	var preURL = "";
	var postURL = "";
	var newURL = "";

	var iStart = url.indexOf( param + "=" );
	if( iStart > -1)
	{
		var iEnd = url.indexOf( "=" , iStart );
		preURL = url.substring( 0 , iEnd ) + "=" + strValue;

		var iStartRest = url.indexOf( "&" , iStart );
		postURL = "";
		if( iStartRest > -1)
		{
			postURL = url.substring( iStartRest );
		}
	}
	else
	{
		preURL = url;
		
		// Check if any vars are present
		if ( url.indexOf( "?" ) > -1)
		{
			postURL = "&" + param + "=" + strValue;
		}
		else
		{
			postURL = "?" + param + "=" + strValue;
		}
	}

	newURL = preURL+postURL;

	return newURL;
}

/// get window visible height
function GetWindowHeight()
{
        var y = 0;
        if (self.innerHeight)
        {
                y = self.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientHeight)
        {
                y = document.documentElement.clientHeight;
        }
        else if (document.body)
        {
                y = document.body.clientHeight;
        }
        return y;
}