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 limitLength( control, length )
{
	if( control.value.length > length )
	{
		control.value = control.value.substr( 0, length );
	}
}

function InvokeValidateField(textbox,fieldName,fieldValue,originalFieldValue,errorMessage,url){

    if(fieldValue == '') return;
    if(fieldValue == originalFieldValue) return;
 
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch (e1){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch (e2){
            xmlhttp = false;
        }
    }
    
    if (!xmlhttp && typeof XMLHttpRequest!='undefined'){     
            xmlhttp = new XMLHttpRequest();
    }
      
    document.body.style.cursor='wait';
    xmlhttp.onreadystatechange= function(){XMLHttpValidateFieldCompleted(originalFieldValue,textbox,errorMessage);};
    var sURL = url + "validate_field.ashx?fieldname=" + fieldName + "&fieldvalue=" + fieldValue;
    xmlhttp.open("GET", sURL, true );
    xmlhttp.send(null);
}

function XMLHttpValidateFieldCompleted(originalFieldValue,textbox,errorMessage){ 
    if (xmlhttp.readyState==4 && xmlhttp.responseText == "exist"){
		alert(errorMessage);
		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;
			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);
			backgroundStyle = backgroundStyle.replace(/YLOC/,'m');
			backgroundStyle = backgroundStyle.replace(/YALIGN/,'center');
		}
		else if( anchor.indexOf('bottom') >= 0 )
		{
			oHelp.style.top = y + thisElement.offsetHeight;
			backgroundStyle = backgroundStyle.replace(/YLOC/,'t');
			backgroundStyle = backgroundStyle.replace(/YALIGN/,'top');
		}
		if( anchor.indexOf('left') >= 0 )
		{
			oHelp.style.left = x - oHelp.offsetWidth;
			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);
			backgroundStyle = backgroundStyle.replace(/XLOC/,'c');
			backgroundStyle = backgroundStyle.replace(/XALIGN/,'center');
		}
		else if( anchor.indexOf('right') >= 0 )
		{
			oHelp.style.left = x + thisElement.offsetWidth;
			backgroundStyle = backgroundStyle.replace(/XLOC/,'l');
			backgroundStyle = backgroundStyle.replace(/XALIGN/,'left');
		}
	}
	else
	{
		oHelp.style.top = y + (thisElement.offsetHeight/2) - (oHelp.offsetHeight/2);
		backgroundStyle = backgroundStyle.replace(/YLOC/,'m');
		backgroundStyle = backgroundStyle.replace(/YALIGN/,'center');
		oHelp.style.left = x + thisElement.offsetWidth;
		backgroundStyle = backgroundStyle.replace(/XLOC/,'l');
		backgroundStyle = backgroundStyle.replace(/XALIGN/,'left');
	}
	oHelp.style.background = backgroundStyle;
}

function attachHelpTexts()
{	
	for(var i = 0; i < document.forms[0].elements.length; i++) {
		if(document.forms[0].elements[i].getAttribute('id')) 
		{
			var element = document.forms[0].elements[i];
			element.onfocus = function (){setFormHelp(this);};
		}
	}
}

function toggleElementDisplay(id, newdisplay)
{
	var oElement = getElement(id);
	if (oElement.style.display == 'none')
		oElement.style.display = newdisplay;
	else
		oElement.style.display = 'none';
}

function topImgSwap(thisElement, str)
{
	thisElement = getElement(thisElement);
	thisElement.src = thisElement.src.replace(/_off|_on/ , str);
}

function btnImgSwap(thisElement, str)
{
	thisElement.src = thisElement.src.replace(/rollup|rolldown|rollover/, str);
}

/*
**  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();
	}
}