var ua;
var wPopup;
var wPopupOpened = false;

ua=navigator.userAgent;

function Login(page) {
	document.forms[0].action = page;
	__doPostBack('LOGIN', 'LOGIN');
}

function GetBrowser() {
	var br;
	if (ua.indexOf("MSIE") != -1) {br="IE";}
	if ((ua.indexOf('Mozilla') != -1) && ((ua.indexOf('Spoofer') == -1) && (ua.indexOf('compatible') == -1))) {br="NS";}
	return br;
}

function OpenDialog(h, w, l, t, myurl, targetstring){
	var popProps
	var popup=new Object();
	popup.w=w;
	popup.h=h;
	popup.l=l;
	popup.t=t;
	popup.url=myurl;
	popup.trgt=targetstring;

	wPopup = window.open(popup.url, popup.trgt, "width=" + popup.w + ",height=" + popup.h + ",left=" + popup.l + ",top=" + popup.t + ",resizable=0,status=1,menubar=0,scrollbars=0,toolbar=0,location=0,directories=0");
	wPopupOpened = true;

	if (GetBrowser() != "IE") {
		wPopup.focus();
	}
	return wPopup;
}

function OpenPopup(h, w, l, t, myurl, targetstring){
	var popProps
	var popup=new Object();
	popup.w=w;
	popup.h=h;
	popup.l=l;
	popup.t=t;
	popup.url=myurl;
	popup.trgt=targetstring;

	wPopup = window.open(popup.url, popup.trgt, "width=" + popup.w + ",height=" + popup.h + ",left=" + popup.l + ",top=" + popup.t + ",resizable=1,status=1,menubar=0,scrollbars=1,toolbar=0,location=0,directories=0");
	wPopupOpened = true;

	if (GetBrowser() != "IE") {
		wPopup.focus();
	}
	return wPopup;
}

function IsItemSelected(inputObject) {
	if (inputObject.selectedIndex >= 0){ 
    	if (inputObject.options[inputObject.selectedIndex].value == "" || inputObject.options[inputObject.selectedIndex].value == null){ 
        	return false; 
        } else { 
            return true; 
        } 
    } else { 
    	return false; 
    } 
}

function IsNumber(inputStr){ 
	var vChars = '-.1234567890';
	for (var i = 0; i < inputStr.length; i++) { 
		var oneChar = inputStr.substring(i, i + 1); 
		if (vChars.indexOf(oneChar) < 0) { 
			return false;
		}
	} 
	return true; 
} 

function IsAlphaNumeric(inputStr){ 
	var vChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
	for (var i = 0; i < inputStr.length; i++) { 
		var oneChar = inputStr.substring(i, i + 1); 
		if (vChars.indexOf(oneChar) < 0) { 
			return false; 
		} 
	} 
	return true; 
} 

function IsAlphaNumericExtended(inputStr){ 
	var vChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-@.'
	for (var i = 0; i < inputStr.length; i++) { 
		var oneChar = inputStr.substring(i, i + 1); 
		if (vChars.indexOf(oneChar) < 0) { 
			return false; 
		} 
	} 
	return true; 
} 

function IsWebsafeInput(inputStr){ 
	var vChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890$-_.+!*\'(),:;\"?/[]{}=*&%#@!"> \\\t\f\n\r';
	for (var i = 0; i < inputStr.length; i++) { 
		var oneChar = inputStr.substring(i, i + 1); 
		if (vChars.indexOf(oneChar) < 0) { 
			alert("|" + oneChar + "|");
			return false; 
		} 
	} 
	return true; 
} 

function IsEmpty(inputStr) { 
	var vTemp;
	
	vTemp = inputStr.replace(/ /g, "");
	if (vTemp == "" || vTemp == null) { 
		return true; 
	} 
	
	if (inputStr == "" || inputStr == null) { 
		return true; 
	} else { 
		return false; 
	} 
} 

function GetStrParm(sep,which,inwhat){
    var pos = 0;
    var wstr = 0;
    var i = 0;
    var start = 0;
    var finish = 0;
    for (i = 1 ; i < which ; i++){
    	pos = inwhat.indexOf(sep,pos);
		if (pos < 0 ){     
                return '';     
            	break;
        }     
        pos++;
	}     
    if ( pos >= 0){                            
        //alert(n + '==' + wstr)
        var start = pos                    
                                       
        var finish = inwhat.indexOf(sep,pos)
        if (finish < 0 ) {
			finish = inwhat.length;
		}
        wstr = inwhat.substring(pos,finish);
    }                                   
    //alert(f + '--' + wstr)
    return wstr;
}

function IsValidDateTime(aDateTime) {
	if (IsEmpty(aDateTime)){
		return false;
	}
	pos = aDateTime.indexOf(' ');
	if (eval(pos) >= 0){	
		sDate = aDateTime.substring(0, pos);
		sTime = aDateTime.substring(pos + 1, aDateTime.length);

		if(IsValidDate(sDate)){
			if (IsValidTime(sTime)){
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	} else {
		if(IsValidDate(aDateTime)){
			return true;
		} else {
			return false;
		}
	}
}

function IsValidDate(aDate){
	today = new Date();

	if (IsEmpty(aDate)){
		return false;
	}

	pos1 = aDate.indexOf('/');
	if (eval(pos1) >= 0){	
		sMonth = aDate.substring(0, pos1);
		if (eval(sMonth) > 0 && eval(sMonth) < 13){
			pos2 = aDate.indexOf('/', pos1 + 1);
			if (eval(pos2) >= 0){	
				sDay = aDate.substring(pos1 + 1, pos2);
				sYear = aDate.substring(pos2 + 1, aDate.length);
				if (eval(sDay) > 0 && eval(sDay) < 32){
					if (eval(sYear) >= eval(today.getYear())){
						return true;
					} else {
						return false;
					}
				} else {
					return false;
				}	
			} else {
				return false;
			}
		} else {
			return false;
		}
	} else {
		return false;
	}
}

function IsValidTime(aTime){
	if (IsEmpty(aTime)){
		return false;
	}

	pos1 = aTime.indexOf(':');
	if (eval(pos1) >= 0){	
		sHours = aTime.substring(0, pos1);
		if (eval(sHours) >= 0 && eval(sHours) < 24){
			pos2 = aTime.indexOf(':', pos1 + 1);
			if (eval(pos2) >= 0){	
				sMinutes = aTime.substring(pos1 + 1, pos2);
				// trip the AM/PM off of the end
				sSeconds = aTime.substring(pos2 + 1, aTime.length - 2);
				if (eval(sMinutes) >= 0 && eval(sMinutes) < 60){
					if (eval(sSeconds) >= 0 && eval(sSeconds) < 60){
						return true;
					} else {
						return false;
					}
				} else {
					return false;
				}	
			} else {
				return false;
			}
		} else {
			return false;
		}
	} else {
		return false;
	}
}

function IsValidEmail(as_email) {
	if(as_email.indexOf('@', 0) == -1){
		return false;
	} else {
		return true;
    }
}


function IsValidUrl(astrUrl) {
	if(astrUrl.indexOf('http://', 0) == -1 && astrUrl.indexOf('mailto:', 0) == -1){
		return false;
	} else {
		return true;
    }
}

function IsLeapYear (aYear) {
	if (((aYear % 4)==0) && ((aYear % 100)!=0) || ((aYear % 400)==0)) {
		return true;
	} 
}

function confirmDelete(){
	var msg = "Are you sure you want to delete this item?\r\n\r\nThis can not be undone.\r\n\r\nIn some cases you can not delete this because\r\nthere is attached information.";
	if(!confirm(msg)){	
		window.event.srcElement.name='ABORT'
	}
}

function SetElementVisibility(elementID, visibility){
	if (visibility == true)
	{
		document.getElementById(elementID).style.visibility='visible';
	}
	else
	{
		document.getElementById(elementID).style.visibility='hidden';
	}
}


//Show calendar and get selected date
function GetCalendarDate(objMe, strUrl){
	var strThisElement = document.getElementById(objMe.id);
	var coordinates=new Object();
	
	coordinates = getAnchorWindowPosition(objMe.id)
	
	var strYPos = coordinates.y-100;
	var strXPos = coordinates.x-100;
	
	OpenDialog(180, 240, strXPos, strYPos, strUrl + '?Locale=Set&ctrl='+ strThisElement.id.replace('__navLink','__txtInputField'), 'calWin');
}

// getAnchorPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the page.
function getAnchorPosition(anchorname) {
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	// Logic to find position
 	if (use_gebi && document.all) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=AnchorPosition_getPageOffsetLeft(o);
		y=AnchorPosition_getPageOffsetTop(o);
		}
 	else if (use_css) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { found=1; break; }
			}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
			}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
		}
	else {
		coordinates.x=0; coordinates.y=0; return coordinates;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// getAnchorWindowPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the window
function getAnchorWindowPosition(anchorname) {
	var coordinates=getAnchorPosition(anchorname);
	var x=0;
	var y=0;
	if (document.getElementById) {
		if (isNaN(window.screenX)) {
			x=coordinates.x-document.body.scrollLeft+window.screenLeft;
			y=coordinates.y-document.body.scrollTop+window.screenTop;
			}
		else {
			x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
			y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
			}
		}
	else if (document.all) {
		x=coordinates.x-document.body.scrollLeft+window.screenLeft;
		y=coordinates.y-document.body.scrollTop+window.screenTop;
		}
	else if (document.layers) {
		x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
		y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
	}
function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
	}	
function AnchorPosition_getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
	}
function AnchorPosition_getWindowOffsetTop (el) {
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
	}

//Function for ENTER key press control
var activeElement;
document.onkeypress=function(event) {
	if(event && event.which) {
		var e=event;
		var charCode=e.which;
	} else {
		var e=window.event;
		var charCode=e.keyCode;
	}
	if (charCode == 13) {
		setActiveElement(event);
		var nextButton = findButton();
		setEnterKey(nextButton.name);
	}
}

//This function will trigger the click event for the button that should be clicked when the ENTER key is pressed
function setEnterKey(btnName) 
{
	if (event.keyCode == 13) 
	{
		event.cancelBubble = true;
		event.returnValue = false;
		document.getElementById(btnName).click();
	}
}

//This will get the active element based on the event
function setActiveElement(e)
{
	//var targ;
	if (!e) var e = window.event;
	if (e.target) activeElement = e.target;
	else if (e.srcElement) activeElement = e.srcElement;
	if (activeElement.nodeType == 3) // defeat Safari bug
		activeElement = targ.parentNode;
}

//This function finds the next button following a particular element
function getNextButton(fIndex,eIndex){
	for (x = eIndex+1; x < document.forms[fIndex].elements.length; x++)
	{
		if (document.forms[fIndex].elements[x].type == 'button' || document.forms[fIndex].elements[x].type == 'submit')
			return x;
	}		
}

//This function does the work of using the activeElement to find the next button (calls getNextButton)
function findButton()
{
	var aeIndex = -1;
	var aeForm = -1;
	var subIndex = -1;

	if (activeElement.type != 'textarea')
	{
		if (activeElement.type != 'button' && activeElement.type != 'submit')
		{
			for (i = 0; i < document.forms.length; i++)
				for (j = 0; j < document.forms[i].elements.length; j++)
				{
					if (document.forms[i].elements[j].name == activeElement.name)
					{
						aeIndex = j;
						aeForm = i;
					}
				}
			
			if (aeForm != -1 && aeIndex != -1)
			{
				subIndex = getNextButton(aeForm,aeIndex);			
			}
				
			if (subIndex != -1)
			{
				return document.forms[aeForm].elements[subIndex];
			}				
		}
		else
		{
			return document.getElementById(activeElement.name);
		}
	}	
}

//**********************************************
//Launch a page for configing a panel.
function OpenPermissionConfig(rootPath, pageId, ctrlId)
{
	url = rootPath + '/LMS20/SCM/Permissions.aspx?pid=' + pageId + '&id=' + ctrlId;
	
	//alert("OpenPermissionConfig:  " + url);
	
	window.open(url,'','status=1, menubar=0, scrollbars=1, toolbar=0, location=0, directories=0, width=760px, height=500px, left=50px, top=50px');
	return false;
}
//**********************************************

//**********************************************
//Show a config screen.
function OpenProblemReport(rootPath, pageId, ctrlId)
{
	url = rootPath + '/LMS20/ClientSupport/FormBasic.aspx?pid=' + pageId + '&id=' + ctrlId;
	window.open(url,'','status=1, menubar=0, scrollbars=1, toolbar=0, location=0, directories=0, width=760px, height=500px, left=50px, top=50px');
	return false;
}
//**********************************************

//**********************************************
//Show a config screen.
function OpenPageMetaData(rootPath, pageId, ctrlId)
{
	url = rootPath + '/LMS20/SCM/PageMetaData.aspx?pid=' + pageId + '&id=' + ctrlId;
	window.open(url,'','status=1, menubar=0, scrollbars=1, toolbar=0, location=0, directories=0, width=760px, height=500px, left=50px, top=50px');
	return false;
}
//**********************************************

//**********************************************
//Show a config screen.
function OpenContentEditor(rootPath, pageId, ctrlId)
{
	url = rootPath + '/LMS20/SCM/ContentEditor.aspx?pid=' + pageId + '&id=' + ctrlId;
	window.open(url, '', 'status=1, menubar=0, scrollbars=1, toolbar=0, location=0, directories=0, width=760px, height=500px, left=50px, top=50px')
	return false;
}
//**********************************************

//**********************************************
//Show a config screen.
function ShowConfig(rootPath, pageId, ctrlId, contentTypeId){
	if(window.event.button == 2 || window.event.button == 0)
	{
		
		url = rootPath + '/LMS20/SCM/Permissions.aspx?pid=' + pageId + '&id=' + ctrlId + '&tid=' + contentTypeId + '&url=' + document.location.pathname;

		//alert("ShowConfig:  " + url);

		window.open(url, '', 'status=1, menubar=0, scrollbars=1, toolbar=0, location=0, directories=0, width=760px, height=500px, left=50px, top=50px');
		event.cancelBubble=true;
		return false;
	}
}
//**********************************************


//**********************************************
//Print preview for a page
function PrintPreview(rootPath, print_area)
{
	//Creating new page
	var pp = window.open();
	
	//Adding HTML opening tag with <HEAD> … </HEAD> portion 
	pp.document.writeln('<HTML><HEAD><title>Receipt</title><LINK href="' + rootPath + '/LMS20/Resource/css/en-us/rvmain.css" type="text/css" rel="stylesheet">');
	pp.document.writeln('<LINK href="' + rootPath + '/LMS20/Resource/css/en-us/PrintStyle.css"  type="text/css" rel="stylesheet" media="print"><base target="_self"></HEAD>');
	
	//Adding Body Tag
	pp.document.writeln('<body MS_POSITIONING="GridLayout" bottomMargin="0" leftMargin="0" topMargin="0" rightMargin="0">');
	
	//Adding form Tag
	pp.document.writeln('<form  method="post">');

	//Creating two buttons Print and Close within a table
	pp.document.writeln('<TABLE width=100%><TR><TD></TD></TR><TR><TD align=right><INPUT ID="PRINT" type="button" value="Print" onclick="javascript:location.reload(true);window.print();"><INPUT ID="CLOSE" type="button" value="Close" onclick="window.close();"></TD></TR><TR><TD></TD></TR></TABLE>');

	//Writing print area of the calling page
	pp.document.writeln(document.getElementById(print_area).innerHTML);

	//Ending Tag of </form>, </body> and </HTML>
	pp.document.writeln('</form></body></HTML>');
}
//**********************************************

/*
function populate()
{
     document.write("In Populate js!")
    if (iedom)
    {
        document.write("iedom js!")
        cross_scroller=document.getElementById? document.getElementById("iescroller") : document.all.iescroller
        cross_scroller.style.top=parseInt(scrollerheight)+8+"px"
        cross_scroller.innerHTML=scrollercontent
        actualheight=cross_scroller.offsetHeight
    }
    else if (document.layers)
    {
        document.write("layers js!")
        ns_scroller=document.ns_scroller.document.ns_scroller2
        ns_scroller.top=parseInt(scrollerheight)+8
        ns_scroller.document.write(scrollercontent)
        ns_scroller.document.close()
        actualheight=ns_scroller.document.height
    }
    lefttime=setInterval("scrollscroller()",20)
}
*/

function ShowWait() 
{
var h = 800;
var w = 800;
if ( typeof( window.innerWidth ) == 'number' ) 
{
    //Non-IE
    h = window.innerHeight;
    w = window.innerWidth;
} 
else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
{
    //IE 6+ in 'standards compliant mode'
    h = document.documentElement.clientHeight;
    w = document.documentElement.clientWidth;
}
else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
{
    //IE 4 compatible
    h = document.body.clientHeight;
    w = document.body.clientWidth;
}
document.getElementById("waitbox").src = document.getElementById("waitbox").src; // force reload to fix IE hidden animated gif bug
document.getElementById("waitbox").style.display = "inline"; 
document.getElementById("waitbox").style.top = (h-document.getElementById("waitbox").clientHeight)/2;
document.getElementById("waitbox").style.left = (w-document.getElementById("waitbox").clientWidth)/2;
}
