var helpTimeout = null;
var helpToggle = true;
function popup(url, width, height) 
{
	var pWin = window.open(url,"NewWin","height=" + height + ",width=" + width + ",location=no,toolbar=no,scrollbars=yes,resizable=yes,menubar=no");
	pWin.focus();
}
function newwindow(url, width, height) 
{
	var Secondwin = window.open(url,"NewWin2","height=" + height + ",width=" + width + ",location=no,toolbar=no,scrollbars=yes,resizable=yes,menubar=no");
	Secondwin.focus();
}
function limitLength( control, length )
{
	if( control.value.length > length )
	{
		control.value = control.value.substr( 0, length );
	}
}
function validateemail(email, url) 
{
	document.body.style.cursor = 'wait';

	// Create an instance of the XML HTTP Request object
	var oXMLHTTP = new ActiveXObject( "Microsoft.XMLHTTP" );
	
	// Prepare the XMLHTTP object for a HTTP POST to our validation ASP page
	var sURL = url + "validateemail.aspx?email=" + email;
	oXMLHTTP.open( "POST", sURL, false );

	// Execute the request
	oXMLHTTP.send();

	if (oXMLHTTP.responseText == "exist")
	{
		alert("Sorry - the email " + email + " already exists.");
		document.getElementById("_ctl0_content_contactinfo_contactEmail").value = '';
		document.getElementById("_ctl0_content_contactinfo_contactEmail").focus();
	}

	document.body.style.cursor = 'auto';
}
function validate_username(name, url,textbox) {
	document.body.style.cursor='wait';

	var oXMLHTTP = new ActiveXObject( "Microsoft.XMLHTTP" );
	var sURL = url + "validateemail.ashx?name=" + name;
	oXMLHTTP.open( "POST", sURL, false );

	oXMLHTTP.send();
	if (oXMLHTTP.responseText == "exist"){
		alert("Sorry - the user name " + name + " already exists.");
		textbox.value = '';
		textbox.focus();
	}
	document.body.style.cursor='auto';
}

function getTop(o, sBreakAt) 
{
	var iTop = 0;
	if (o.offsetParent) {
		if(sBreakAt && (oParent = getElement(sBreakAt))) 
		{
			while (o.offsetParent && (o.offsetParent != oParent)){
				iTop += o.offsetTop;o = o.offsetParent;
			}
		}
		else
		{	
			while (o.offsetParent){
				iTop += o.offsetTop;o = o.offsetParent;
			}
		}
		
	} else if (o.y) {
		iTop += o.y;
	}
	return iTop;
}

function getLeft(o) 
{
	var iLeft = 0;
	if (o.offsetParent) {
		while (o.offsetParent) {
			iLeft += o.offsetLeft;o = o.offsetParent;
		}
	} else if (o.x) {
		iLeft += o.x;
	}
	return iLeft;
}

function setEvent(oElement, sEventType, fEventHandler) 
{
	if (typeof(oElement) == 'string')
		oElement = getElement(oElement);
	if(window.attachEvent) {
		oElement.attachEvent('on'+sEventType, fEventHandler);
	} else if (window.addEventListener) {
		oElement.addEventListener(sEventType, fEventHandler, false);
	} else {
		
		if(oElement['on'+sEventType]) {		
			oldFunc = oElement['on'+sEventType];
			oElement['on'+sEventType] = function() {
				oldFunc;
				fEventHandler;
			}
		} else {
			oElement['on'+sEventType] = fEventHandler;
		}
	}
}

function getElementsByTagAndClass(a_sTagName, a_sClassName) 
{
	var aReturnElements = [];
	if(document.getElementsByTagName) {
		var aAllTags = document.getElementsByTagName(a_sTagName);
		for(j = 0; j < aAllTags.length; j++) {
			var oCurrentTag = aAllTags[j];
			if(oCurrentTag.className == a_sClassName) {
				aReturnElements[aReturnElements.length] = oCurrentTag;
			}
		}
	}
	return aReturnElements;
}


function getElement(a_sElementId) 
{
	if (typeof(a_sElementId) != "string")
		return a_sElementId;
	var oElement = (document.getElementById) ? document.getElementById(a_sElementId) : (document.all) ? document.all[a_sElementId] : null;
	if(oElement == null) {	
		for(i = 0; i < document.forms.length; i++) {
			var form = document.forms[i];
			if(form.name == a_sElementId) {
				return form;
			} else {
				for(j = 0; j < form.elements.length; j++) {
					element = form.elements[j];
					if(element.name == a_sElementId) {
						return element;
					}
				}
			}
		}
		if(document.images[a_sElementId]) {
			return document.images[a_sElementId];
		} else if(document.anchors[a_sElementId]) {
			return document.anchors[a_sElementId];
		} else if (document.layers) { 
		}
	}
	return oElement;
}

function showFormHelp(sHeader, sInfo)
{
	var oHelp = getElement('formHelp');
	var oHelpHeader = getElement('formHelpHeader');
	var oHelpText = getElement('formHelpInfo');
	
	if(sHeader && sInfo)
	{
		oHelpHeader.innerHTML = sHeader;
		oHelpText.innerHTML = sInfo;
		oHelp.style.display = 'block';
	}
	else
		oHelp.style.display = 'none';
}

function hideFormHelp(thisElement)
{
	var oHelp = getElement('formHelp');
	var oHelpToggle = getElement('formHelpMini');
	
	oHelpToggle.innerHTML = '&nbsp;';
	oHelp.className = 'formHelpOff';
	moveHelp(thisElement, oHelp);
}

function toggleFormHelp(thisElement)
{
	var oHelp = getElement('formHelp');
	var oHelpToggle = getElement('formHelpMini');
	if (oHelp.className == 'formHelpOff')
	{
		oHelp.className = 'formHelpOn';
		oHelpToggle.innerHTML = '&ndash;';
		helpToggle = true;
		moveHelp(thisElement, oHelp);
	}
	else
	{
		hideFormHelp(thisElement);
		helpToggle = false;
	}
	clearTimeout(helpTimeout);
}

function setFormHelp(thisElement)
{
	if (typeof(thisElement) == 'string')
		thisElement = getElement(thisElement);
	var id = thisElement.id || thisElement.name;
	var oHelp = getElement('formHelp');
	var oHelpToggle = getElement('formHelpMini');
	
	oHelpToggle.href = "javascript:toggleFormHelp(\'" + id + "\');";
	if (id.indexOf('Link') != -1 && !helpToggle)
		toggleFormHelp(id);
	showFormHelp(helptips[id + 'Title'], helptips[id + 'Text']);
	if (helpToggle)
	{
		oHelp.className = 'formHelpOn';
		oHelpToggle.innerHTML = '&ndash;';
	}
	moveHelp(thisElement, oHelp);
	clearTimeout(helpTimeout);
	helpTimeout = setTimeout("hideFormHelp(\'" + id + "\')", Math.pow(oHelp.offsetHeight - 15,1.1) * 100 );
}

function moveHelp(thisElement, oHelp)
{
	if (typeof(thisElement) == 'string')
		thisElement = getElement(thisElement);
	if (typeof(oHelp) == 'string')
		oHelp = getElement(oHelp);
	var y = getTop(thisElement);
	var x = getLeft(thisElement);
	var id = thisElement.id || thisElement.name;
	var anchor = helptips[id + 'Anchor'];
	var theme = getElement("_ctl0_content_tooltip_themeName").value;
	var backgroundStyle = "url(" + theme + "/images/bg_help_box_YLOCXLOC.gif) no-repeat YALIGN XALIGN";
	
	if(isNaN(helptips[id + '_OffsetX']) == false)
		x += parseInt(helptips[id + '_OffsetX']);
	if(isNaN(helptips[id + '_OffsetY']) == false)
		y += parseInt(helptips[id + '_OffsetY']);
	if(anchor) {
		if( anchor.indexOf('top') >= 0 )
		{
			oHelp.style.top = y - oHelp.offsetHeight + 'px';
			backgroundStyle = backgroundStyle.replace(/YLOC/,'b');
			backgroundStyle = backgroundStyle.replace(/YALIGN/,'bottom');
		}
		else if( anchor.indexOf('middle') >= 0 )
		{
			oHelp.style.top = y + (thisElement.offsetHeight/2) - (oHelp.offsetHeight/2) + 'px';
			backgroundStyle = backgroundStyle.replace(/YLOC/,'m');
			backgroundStyle = backgroundStyle.replace(/YALIGN/,'center');
		}
		else if( anchor.indexOf('bottom') >= 0 )
		{
			oHelp.style.top = y + thisElement.offsetHeight + 'px';
			backgroundStyle = backgroundStyle.replace(/YLOC/,'t');
			backgroundStyle = backgroundStyle.replace(/YALIGN/,'top');
		}
		if( anchor.indexOf('left') >= 0 )
		{
			oHelp.style.left = x - oHelp.offsetWidth + 'px';
			backgroundStyle = backgroundStyle.replace(/XLOC/,'r');
			backgroundStyle = backgroundStyle.replace(/XALIGN/,'right');
		}
		else if( anchor.indexOf('center') >= 0 )
		{
			oHelp.style.left = x + (thisElement.offsetWidth/2) - (oHelp.offsetWidth/2) + 'px';
			backgroundStyle = backgroundStyle.replace(/XLOC/,'c');
			backgroundStyle = backgroundStyle.replace(/XALIGN/,'center');
		}
		else if( anchor.indexOf('right') >= 0 )
		{
			oHelp.style.left = x + thisElement.offsetWidth + 'px';
			backgroundStyle = backgroundStyle.replace(/XLOC/,'l');
			backgroundStyle = backgroundStyle.replace(/XALIGN/,'left');
		}
	}
	else
	{
		oHelp.style.top = y + (thisElement.offsetHeight/2) - (oHelp.offsetHeight/2) + 'px';
		backgroundStyle = backgroundStyle.replace(/YLOC/,'m');
		backgroundStyle = backgroundStyle.replace(/YALIGN/,'center');
		oHelp.style.left = x + thisElement.offsetWidth + 'px';
		backgroundStyle = backgroundStyle.replace(/XLOC/,'l');
		backgroundStyle = backgroundStyle.replace(/XALIGN/,'left');
	}
	oHelp.style.background = backgroundStyle;
}

function attachHelpTexts()
{
	for(var i = 0; i < document.aspnetForm.elements.length; i++) 
	{
		if(document.aspnetForm.elements[i].getAttribute('id')) 
		{
			var element = document.aspnetForm.elements[i];
			element.onfocus = function (){setFormHelp(this);};
		}
	}
}
function toggleElementDisplay(thisElement)
{
	if (typeof(thisElement) == 'string')
		thisElement = getElement(thisElement);
	if(thisElement.style.display == 'none')
		thisElement.style.display = 'block';
	else
		thisElement.style.display = 'none';
}

function checkElementDisplay()
{
	var argsArray = checkElementDisplay.arguments;
	var isDisplayed = true;
	var errmsg = "";
	for ( var i = 0; i < argsArray.length; i++ )
	{
		var oElement = getElement( argsArray[i] );
		if ( oElement )
			isDisplayed = ( isDisplayed && oElement.style.display != 'none' );
		else
			errmsg += "Developer Error: " +  argsArray[i]+ " is not an object.\n";
	}
	if ( errmsg.length > 0 )
		alert(errmsg + "Please Contact Technical Support.");
	return isDisplayed;
}

/*
**  Returns the caret (cursor) position of the specified text field.
**  Return value range is 0-oField.length.
*/
function getCaretPosition(oField)
{
	var iCaretPos = 0;
	if (document.selection)// IE Support
	{
		// Set focus on the element
		oField.focus();
		// To get cursor position, get empty selection range
		var oSel = document.selection.createRange();
		// Move selection start to 0 position
		oSel.moveStart('character', -oField.value.length);
		// The caret position is selection length
		iCaretPos = oSel.text.length;
	}
	else if (oField.selectionStart || oField.selectionStart == '0')//Firefox support
		iCaretPos = oField.selectionStart;

 return iCaretPos;
}

/*
**  Sets the caret (cursor) position of the specified text field.
**  Valid positions are 0-oField.length.
*/
function setCaretPosition(oField, iCaretPos)
{
	if (document.selection)// IE Support
	{
		oField.focus();
		var oSel = document.selection.createRange();
		oSel.moveStart('character', -oField.value.length);
		oSel.moveEnd('character', -oField.value.length);
		// Move selection start and end to desired position
		oSel.moveStart('character', iCaretPos);
		oSel.moveEnd('character', 0);
		oSel.select();
	}
	else if (oField.selectionStart || oField.selectionStart == '0')//Firefox support
	{
		oField.selectionStart = iCaretPos;
		oField.selectionEnd = iCaretPos;
		oField.focus();
	}
}