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=no,menubar=no");
	pWin.focus();
}
function newwindow(url, width, height) 
{
	var Secondwin;
	Secondwin = window.open(url,"NewWin2","height=" + height + ",width=" + width + ",location=no,toolbar=no,scrollbars=yes,resizable=yes,menubar=no");
}
function limitLength( control, length )
{
	if( control.value.length > length )
	{
		control.value = control.value.substr( 0, length );
	}
}


function InvokeValidateField(textbox,fieldName,fieldValue,originalFieldValue,errorMessage,url,extraParams){

    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 + "validatefield.ashx?fieldname=" + fieldName + "&fieldvalue=" + fieldValue + extraParams;
    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 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 validateemail(name, 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.ashx?name=" + name;
	oXMLHTTP.open( "POST", sURL, false );

	// Execute the request
	oXMLHTTP.send();
	
	if (oXMLHTTP.responseText == "exist")
	{
		alert("Sorry - the user name " + name + " already exists.");
		document.getElementById('_ctl0_content_companyinfo_username').value = '';
		document.getElementById('_ctl0_content_companyinfo_username').focus();
	}

	document.body.style.cursor='auto';
}
function validateemail1(name, 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.ashx?name=" + name;
	oXMLHTTP.open( "POST", sURL, false );

	// Execute the request
	oXMLHTTP.send();
	
	if (oXMLHTTP.responseText == "exist")
	{
		alert("Sorry - the user name " + name + " already exists.");
		document.getElementById('_ctl0_content_newid').value = '';
		document.getElementById('_ctl0_content_newid').focus();
	}

	document.body.style.cursor='auto';
}

function checkABA(s) 
{
  var i, n, t;

  // First, remove any non-numeric characters.

  t = "";
  for (i = 0; i < s.length; i++) {
    c = parseInt(s.charAt(i), 10);
    if (c >= 0 && c <= 9)
      t = t + c;
  }

  // Check the length, it should be nine digits.

  if (t.length != 9)
    return false;

  // Now run through each digit and calculate the total.

  n = 0;
  for (i = 0; i < t.length; i += 3) {
    n += parseInt(t.charAt(i),     10) * 3
      +  parseInt(t.charAt(i + 1), 10) * 7
      +  parseInt(t.charAt(i + 2), 10);
  }

  // If the resulting sum is an even multiple of ten (but not zero),
  // the aba routing number is good.

  if (n != 0 && n % 10 == 0)
    return true;
  else
    return false;
}

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.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(oElement, newdisplay)
{
	if (newdisplay == null) newdisplay = "";
	if( typeof(oElement) == "string")
		oElement = getElement(oElement);
	if (oElement.style.display == "none")
		oElement.style.display = newdisplay;
	else
		oElement.style.display = "none";
}

function delayElementDisplay(id, newdisplay, milliseconds)
{
	var toggleFuncString = "toggleElementDisplay('" + id + "','" + newdisplay + "')";
	setTimeout( toggleFuncString, milliseconds);
}

function toggleElementOverflow(id, newoverflow)
{
	var oElement = getElement(id);
	if (oElement.style.overflow == 'auto')
		oElement.style.overflow = newoverflow;
	else
		oElement.style.overflow = 'auto';
}

function getImgSrcName( oElement )
{
	if( typeof(oElement) == 'string' )
		oElement = getElement( oElement );
	var imgSrc = oElement.src;
	var imgSrcArray = imgSrc.split("/");
	return imgSrcArray[ imgSrcArray.length - 1 ];
}

function btnImgSwap(thisElement, str)
{
	thisElement.src = thisElement.src.replace(/rollup|rolldown|rollover/ , str);
}

function plusMinusImgSwap(oElement)
{
	if( typeof(oElement) == 'string')
		oElement = getElement(oElement);
	var str = "plus";
	if( oElement.src.search("minus") == -1 )
		str = 'minus';
	oElement.src = oElement.src.replace(/plus|minus/ , str);
	return str;
}

//Sidebar
function sidebarToggle( oElement, hiddenElement )
{
	if (typeof(hiddenElement) == 'string')
		hiddenElement = getElement(hiddenElement);
	if (typeof(oElement) == 'string')
		oElement = getElement(oElement);
	if(getElement("sidebarCell").style.display == "none") 
	{
		oElement.src = oElement.src.replace( /sidebarclosed/, "sidebaropen");
		getElement("sidebarCell").style.display = "";
		hiddenElement.value = "open";
	}
	else
	{
		oElement.src = oElement.src.replace( /sidebaropen/, "sidebarclosed");
		getElement("sidebarCell").style.display = "none";
		hiddenElement.value = "closed";
	}
}
function toggleSidebarPanel( oElement, elementPrefix )
{
	if (typeof(oElement) == 'string')
		oElement = getElement(oElement);
	btnElement = getElement( elementPrefix + 'Btn');
	hiddenElement = getElement( elementPrefix );
	if( oElement.style.display == "none" ) 
	{
		btnElement.src = btnElement.src.replace( /panelclosed/, "panelopen");
		hiddenElement.value = "open";
		oElement.style.display = "";
	} else {
		btnElement.src = btnElement.src.replace( /panelopen/, "panelclosed");
		hiddenElement.value = "closed";
		oElement.style.display = "none";
	}
	return false;
}

//Scroll
function setFreezeDiv()
{
	oElement = getElement("freezingDiv");
	oElement.style.height = "570px";
	changeFreezeDiv();
	window.onresize = function(){changeFreezeDiv();}
}
function changeFreezeDiv()
{
	oElement = getElement("freezingDiv");
	oElement.style.display = "none";
	oElement.style.width = oElement.offsetParent.offsetWidth + "px";
	oElement.style.display = "block";
}
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];
	}
}

/*
**  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();
	}
}

function selectAll(oField)
{
	if (document.selection)// IE Support
	{
		oField.focus();
		var oSel = document.selection.createRange();
		oSel.expand("textedit");
		oSel.select();
	}
	else if (oField.selectionStart || oField.selectionStart == '0')//Firefox support
	{
		oField.selectionStart = 0;
		oField.selectionEnd = oField.value.length;
		oField.focus();
	}
}

function selectCaretToEnd(oField, iCaretPos)
{
	if (document.selection)// IE Support
	{
		oField.focus();
		var oSel = document.selection.createRange();
		oSel.expand("textedit");
		oSel.moveStart('character', iCaretPos);
		oSel.select();
	}
	else if (oField.selectionStart || oField.selectionStart == '0')//Firefox support
	{
		oField.selectionStart = iCaretPos;
		oField.selectionEnd = oField.value.length;
		oField.focus();
	}
}

function selectStartToCaret(oField, iCaretPos)
{
	if (document.selection)// IE Support
	{
		oField.focus();
		var oSel = document.selection.createRange();
		oSel.expand("textedit");
		oSel.moveEnd('character', -(oField.value.length - iCaretPos));
		oSel.select();
	}
	else if (oField.selectionStart || oField.selectionStart == '0')//Firefox support
	{
		oField.selectionStart = 0;
		oField.selectionEnd = iCaretPos;
		oField.focus();
	}
}