///////////////////////////////////////////////////////////////////
// Verify user login fields
function UserLogin(){
	if(document.getElementById('txtUserName').value=='' | document.getElementById('txtPassword').value==''){
		alert('Please enter a username and password to login.');
		return false;
	}
	else{
		var msie = ((navigator.appVersion.indexOf("MSIE")!= -1)&&!window.opera)? true : false; 
		if (msie | navigator.appName=='Netscape'){
			return true;
		} 
		else {
			alert('This site only supports Internet Explore and Netscape compatible browsers.')
			return false;
		}
			

		
	}
}


function openhelpfile(ncontext){
window.open("/diyhelp/frames.asp?helpon=" + ncontext,'Sample','toolbar=no,width=800,height=400,left=200,top=200, status=no,scrollbars=auto,resizable=yes')
}


///////////////////////////////////////////////////////////////////
//Open user edit window//

function openUserEdit(gridName,CellID,button){
	var row = igtbl_getRowById(CellID);
	var userName = row.getCellFromKey('User_UserName').getValue();
	var mGroupID = document.getElementById('hMenuGroupID').value;
	document.location = 'admin_user_edit.aspx?username=' + userName + '&menugroupid=' + mGroupID;
}

///////////////////////////////////////////////////////////////////
//Edit user save//

function editUserSubmit(){
	if(document.getElementById('txtPassword').value==''){
		alert('A user password is required.');
		return false;
	}
	
	if(document.getElementById('txtRPassword').value==''){
		alert('A you must re-enter the user password.');
		return false;
	}
	
	if(document.getElementById('txtPassword').value != document.getElementById('txtRPassword').value){
		alert('The passwords do not match, please re-enter the user password.');
		document.getElementById('txtRPassword').value='';
		return false;
	}
	
	if(document.getElementById('txtUserName').value==''){
		alert('A username is required');
		return false;
	}
	return true;
}

///////////////////////////////////////////////////////////////////
//Edit user//

function adminEditUser(){
	var oUltraWebGrid1=igtbl_getGridById("UWGUsers");
	for(var rowId in oUltraWebGrid1.SelectedRows){
		if(igtbl_getRowById(rowId)){
			 var row = igtbl_getRowById(rowId);
			 var userName = row.getCellFromKey('User_UserName').getValue();
			 var mGroupID = document.getElementById('hMenuGroupID').value;
			 document.location = 'admin_user_edit.aspx?username=' + userName + '&menugroupid=' + mGroupID;
		}
	}
}	

///////////////////////////////////////////////////////////////////
//Open Static Reports//	

function staticROpen(repName){
	window.open(repName,'','width=920, height=680, scrollbars=yes, menubar=yes,status=yes, resizable=yes')
}	

///////////////////////////////////////////////////////////////////
//Open Excel Reports//	

function excelROpen(reportID){
	var dStart = document.getElementById('Filters1_WebDateStart').value;
	var dEnd = document.getElementById('Filters1_WebDateEnd').value;
	var eID =document.getElementById('encryptaccountid').value;
	document.getElementById('reportFrame').src = 'generateExcel.aspx?bDate=' + dStart + '&eDate=' + dEnd + '&reportid=' + reportID + '&accountid='+ eID
	return false;
}

///////////////////////////////////////////////////////////////////
//Check Excel for Returned Data//

function excelCheckData(){
	if(document.getElementById('hData').value==0){
	alert('There was no data returned for the dates specified, please refine your search.')
		}
	}	

///////////////////////////////////////////////////////////////////
//Section Menu action//

function menuAction(menu){
	window.open(menu,'','');
}

///////////////////////////////////////////////////////////////////
//Section Menu action//

function articleRateWindow(){
	window.open('ars.aspx','','height=768, width=1024, scrollbars=yes, menubar=no, status=no, resizable=yes, toolbars=no');
}


///////////////////////////////////////////////////////////////////
//Verifies java applet is installed//

function checkJavaPlugin()
{
var applet = parent.frames["appFrame"].document.BannerDisplay;
if(!applet)
{
if(confirm('Your machine does not have the Java Plugin installed , would you like to download it now?')){
window.open('http://www.java.com/en/download/download_the_latest.jsp')
}
}
else
{
window.open('ars.aspx?accountid=' + currAccount,'','height=700, width=1024, scrollbars=yes, menubar=no, status=no, resizable=yes, toolbars=no');
}
return;
}


///////////////////////////////////////////////////////////////////
//ARS gets article body and loads applet xml//

function getArticleBody(gridName,CellID,button){
var vfARBody
var vfArticleBody
var vARSApplet

try {
	vfARBody=parent.frames["ARSBody"].frames['fARBody'];
	vARSApplet=parent.frames['ARSApplet'];
	var row = igtbl_getRowById(CellID);
	var keyField = document.Form1.hKeyField.value;
	var articleID = row.getCellFromKey(keyField).getValue();
	var kdURL = document.Form1.baseURL.value;
	var accountID = document.Form1.hAccountID.value;
	document.Form1.hItemID.value=articleID
	vARSApplet.loadXML(kdURL + '/TestXSD.aspx?accountid=' + accountID,kdURL + '/appletxml.aspx?articleid=' + articleID );
	vARSApplet.objectID = articleID;
	vfARBody.document.Form1.hAccountID.value=accountID;
	vfARBody.document.Form1.hItemID.value=articleID;
	vfARBody.document.Form1.submit();
	}	
catch (E){
	alert('The body page is not loaded, please try again.')
	} 	
}

///////////////////////////////////////////////////////////////////
//ARS gets article body and loads applet xml//

function getArticleBodyNew(entryID){
var vfARBody
var vfArticleBody
var vARSApplet
	vfARBody=parent.frames['fArticleBody'].frames['fARBody']//parent.parent.frames["ARSBody"].frames['fARBody'];
	vARSApplet=parent.parent.frames['ARSApplet'];
	if (checkAppletChange(vARSApplet)){
		if(confirm('By loading another article, you will loose your changes, would you like continue?')!=true)
		return
	}	 	
	var kdURL = parent.document.Form1.baseURL.value;
	var accountID = parent.document.Form1.hAccountID.value;
	var sessionID = parent.document.Form1.hSessionID.value;
	parent.document.Form1.hItemID.value=entryID
	vARSApplet.loadXML(kdURL + '/TestXSD.aspx?accountid=' + accountID + "&sessionid=" + sessionID,kdURL + '/appletxml.aspx?articleid=' + entryID );
	vARSApplet.objectID = entryID;
	vfARBody.document.Form1.hAccountID.value=accountID;
	vfARBody.document.Form1.hItemID.value=entryID;
	vfARBody.document.Form1.hSessionID.value=sessionID;
	vfARBody.document.Form1.submit();
}

///////////////////////////////////////////////////////////////////
//Checks Applet to see if entry has changed//

function checkAppletChange(Appletframe){
//alert(Appletframe)
//alert(parent.parent.frames['ARSApplet'].getIsDocumentChanged())
 return Appletframe.getIsDocumentChanged()
}

///////////////////////////////////////////////////////////////////
//ARS toolbar actions//
var bsaving=false
function articleAction(toolBar,Item,Event){
	var vfARSApplet=parent.frames['ARSApplet'].frames['xmlSave']
	toolBarAction = Item.Key
	SessionID = document.Form1.hSessionID.value;
	AccountID = document.Form1.hAccountID.value;
	ItemID = parent.frames['ARSApplet'].objectID
	UserID = document.Form1.hUserID.value;
	Base = document.Form1.baseURL.value + '/TestXSD.aspx?accountid=' + AccountID + '&sessionid=' + SessionID
	var vARSApplet=parent.frames['ARSApplet']
	switch (toolBarAction){
		case 'NEW':
		if (checkAppletChange(vARSApplet)){
			if(confirm('By loading another article, you will loose your changes, would you like continue?')!=true)
			return
		}	 	
		document.getElementById('hItemID').value=0;
		parent.frames['ARSApplet'].objectID=0
		parent.frames['ARSApplet'].loadXML(Base,null);
		//parent.frames['ARSBody'].newBody('');
		frames['fArticleBody'].newBody('');
		vfARSApplet.document.getElementById('articlebody').value="";
		break
		case 'COPY':
		if(parent.frames['ARSApplet'].xmlValidation()==true){
		if(confirm('This action will save this Record as a new Record  , would you like continue?')){	
			frames["iEntryList"].document.Form1.hSave.value="Save Failed"
			try {
				document.getElementById('hItemID').value=0;
				parent.frames['ARSApplet'].objectID=0;
				vfARSApplet.getxml();
				//vARSApplet.setDocumentChanged(false);
			//	alert('Save Complete');
				} 
			catch (E) 
			{
			vfARSApplet.location=document.Form1.baseURL.value + '/savexml.aspx'
			frames["iEntryList"].document.Form1.submit()
			}
		}
		}
		else
		{
			alert("You must fill out your record's information completely test.")
		}
		break
		case 'SAVE':
			
		if(parent.frames['ARSApplet'].xmlValidation()==true){
			frames["iEntryList"].document.Form1.hSave.value="Save Failed"
			try {
				vfARSApplet.getxml()
				} 
			catch (E) 
			{
			vfARSApplet.location=document.Form1.baseURL.value + '/savexml.aspx'
			frames["iEntryList"].document.Form1.submit()
		
			}
		}
		else
		{
			alert("You must fill out your record's information completely test.")
		}
		
		break
		case 'CLOSE':
		if (checkAppletChange(vARSApplet)){
			if(confirm('By closing this Window, you will loose your changes, would you like continue?')!=true)
			return
		}	 	
//		if(confirm('Are you sure you want to close DIY Rating System?')){
			parent.window.close();
		break
	}
}

///////////////////////////////////////////////////////////////////
//Resets article filter//

function resetARFilter(){
	var entryFrame=frames["iEntryList"].document.Form1
	document.getElementById('Article_filter1_DDLStatus').selectedIndex=0;
	document.getElementById('Article_filter1_ddlFSource').selectedIndex=0;
	var dateChooserStart = igdrp_getComboById('Articlexfilter1xWDTStart'); 
	var dateChooserEnd = igdrp_getComboById('Articlexfilter1xWDTEnd'); 
	dateChooserStart.setText('');
	dateChooserStart.setValue('');
	dateChooserEnd.setText('');
	dateChooserEnd.setValue('');
	entryFrame.hStatus.value=''
	entryFrame.hSource.value=''
	entryFrame.hStart.value=''
	entryFrame.hEnd.value=''	
	entryFrame.submit();
}

///////////////////////////////////////////////////////////////////
//Sets article filter//

function setARFilter(){
	var entryFrame=frames["iEntryList"].document.Form1
	var status=document.getElementById('Article_filter1_DDLStatus').value;
	var source=document.getElementById('Article_filter1_ddlFSource').value;
	var startDate=igdrp_getComboById('Articlexfilter1xWDTStart').getText();
	var endDate=igdrp_getComboById('Articlexfilter1xWDTEnd').getText();
	if(source=='- All sources -')
		source=''
	
	entryFrame.hStatus.value=status
	entryFrame.hSource.value=source
	entryFrame.hStart.value=startDate
	entryFrame.hEnd.value=endDate	
	entryFrame.submit();
	//alert(frames["iEntryList"].document.Form1.hStatus)
	//alert(status)
	//alert(source)
	//alert(startDate)
	//alert(endDate)
}

///////////////////////////////////////////////////////////////////
//sets article filter dates//

function filterDate(){
	var startDate =igdrp_getComboById('WDTStart');
	var endDate = igdrp_getComboById('WDTEnd');
	document.getElementById('hSDate').value = startDate.getText();
	document.getElementById('hEDate').value = endDate.getText();
	var dateChooser = igdrp_getComboById('WDTStart'); 
	alert(dateChooser)
}

///////////////////////////////////////////////////////////////////
//changes field id for maintenance list//

function setFieldID(fieldID){
	var grid = wgGetGridById('WGLists');
	document.getElementById('hFieldID').value = fieldID;
	//grid.RefreshAll();
	
	
}

///////////////////////////////////////////////////////////////////
//submits grid update when enter key is hit//

function DoEditKeyDown(gridId)
{
  if (event.keyCode == 13)
  { 
    // cancel the event as it'll be handled manually
    event.returnValue = false;
		
    var grid = wgGetGridById(gridId);
		
    // get currently active edit cell
    var ac = grid.GetActiveEditCell();
		
    if (ac.rowElement.type == "Record")
    {if(confirm('Are you sure you want to update this row?')){
      // get the table object
      var tbl = grid.Tables[ac.tblName];
									
      wgExitEdit(grid, 0); // exit and update the row if dirty
						
      // find out if the table of current row is a parent
      var isParent = (tbl.HasChildTables().length > 0)? true : false;
						
      //SelectNext(ac, isParent);
						
      // you can try to remove this line if you receive a javascript error
      return false;
    }
    }
   }
				
  return true;
}


///////////////////////////////////////////////////////////////////
//includes all fields for custom grid//

function includeAllFields(){
	var checkAll = document.getElementById("hCheckAll");
	var check
	if(checkAll.value==0){
		checkAll.value=1
		check=true
		document.getElementById("btnInclude").value="Deselect All Fields"
	}
	else
	{	
		checkAll.value=0
		check=false
		document.getElementById("btnInclude").value="Select All Fields"
	}
	var grid = wgGetGridById("WGData");
	var table=grid.Tables["CustomFields"];
	var tableElement=table.gettbTBElement();
	if(tableElement.rows.length){
		for (var i=0; i<tableElement.rows.length; i++) {
			var row = tableElement.rows[i];
			if (row.type == "Record") {
				var cell = wgGetCellByName(row, "Include"); 
				if(!cell.childNodes[0].disabled){
					cell.childNodes[0].checked = check;
			
				 }
			}
		}
	}
}


///////////////////////////////////////////////////////////////////
//gets custom grid query//

function getData(){
var hID=frames["iGrid"].document.Form1.hFID;
	hID.value=""
var gridData = wgGetGridById("WGData");
var gridResults = wgGetGridById("WGResults")
var table=gridData.Tables["CustomFields"];
var tableElement=table.gettbTBElement();
	if(tableElement.rows.length){
	for (var i=0; i<tableElement.rows.length; i++) {
		var row = tableElement.rows[i];
		if (row.type == "Record") {
			var cell = wgGetCellByName(row, "Include"); 
			var iField = wgGetCellByName(row, "fieldID"); 
			if(cell.childNodes[0].checked == true){
				if(hID.value.length>0){
					hID.value=hID.value + "," + iField.innerText
				}
				else{
					hID.value=hID.value + iField.innerText
				}
			}
		}
	}
	
}

//gridResults.RefreshAll();
frames["iGrid"].document.Form1.submit()
}


///////////////////////////////////////////////////////////////////
//custom data edit//

function customDataEdit(){
	var grid=wgGetGridById('WGResults');
	var selObj = grid.getSelectedObject();
	var row=selObj.getRowElement();
	var itemID=row.keyValue;
	var w=window.open("../maintenance/maintenance_applet.aspx?itemid=" + itemID,"","width="+maxW+",height="+maxH+",left="+adjX+",top="+adjY+",No,resizable");
			zNew=null;
	//window.open('../maintenance/maintenance_applet.aspx?itemid=' + itemID,'','')
}

	var maxW,maxH,fromX,fromY,toX,toY,adjX,adjY,zBox,zStep=0,zLink,zNew;
	function customDataWindow() {
	evt=event
	zlink=null
	maxw=900
	maxh=530
	tox=0
	toy=0
	if (arguments.length > 2) zNew=1;
	scrollH=(window.pageYOffset!=null)?window.pageYOffset:document.body.scrollTop;
	maxW=maxw?maxw:window.innerWidth?innerWidth:document.body.clientWidth;
	maxH=maxh?maxh:window.innerHeight?innerHeight:document.body.clientHeight;
	toX=tox?tox:0;
	toY=(toy?toy:0)+scrollH;
	fromX=evt.pageX?evt.pageX:evt.clientX;
	fromY=(evt.pageY?evt.pageY:evt.clientY)+(document.all?scrollH:0);
	adjX=toX+evt.screenX-fromX;
	adjY=toY+evt.screenY-fromY;
	if (document.createElement && document.body.appendChild && !zBox) {
		zBox=document.createElement("div");
		zBox.style.position="absolute";
		document.body.appendChild(zBox);
	} else if (document.all && !zBox) {
		document.all[document.all.length-1].outerHTML+="<div id=zBoxDiv style=position:absolute></div>";
		zBox=document.all.zBoxDiv;
	} else if (document.layers && !zBox) {
		zBox=new Layer(maxW);
		zBox.style=zBox;
	}
	zLink=zlink;
	doLaunch();
}

function doLaunch() {
var grid=wgGetGridById('WGResults');
var selObj = grid.getSelectedObject();
var row=selObj.getRowElement();
var itemID=row.keyValue;
var currAccount=document.getElementById("hCurrAccount").value;
zStep+=1;
zPct=(10-zStep)/10
	if (document.layers) {
		zBox.moveTo(toX+zPct*(fromX-toX),toY+zPct*(fromY-toY));
		zBox.document.open();
		zBox.document.write("<table width="+maxW*(1-zPct)+" height="+maxH*(1-zPct)+" border=2 cellspacing=0><tr><td></td></tr></table>");
		zBox.document.close();
		} else {
		zBox.style.border="2px solid #999999";
		zBox.style.left=toX+zPct*(fromX-toX);
		zBox.style.top=toY+zPct*(fromY-toY);
		zBox.style.width=maxW*(1-zPct);
		zBox.style.height=maxH*(1-zPct);
	}
	zBox.style.visibility="visible";
	if (zStep < 10) setTimeout("doLaunch("+fromX+","+fromY+","+toX+","+toY+")",30);
	else {
		zBox.style.visibility="hidden";
		zStep=0;
		var w=window.open("maintenance_applet_frame.aspx?itemid=" + itemID + "&accountid=" + currAccount,"","width="+maxW+",height="+maxH+",left="+adjX+",top="+adjY+",No,resizable");
			zNew=null;
	}
}


	function customViewWindow(pfilename) {
//	evt=event
//	zlink=null
	maxW=900
	maxH=530
	tox=0
	toy=0
		var w=window.open(pfilename,"","width=900,height=530,resizable=yes, toolbar=yes")
			zNew=null;
		return w		
}
function customScriptWindow(pfilename) {
//	evt=event
//	zlink=null
	maxW=900
	maxH=530
	tox=0
	toy=0
		var w=window.open(pfilename,"ScriptTest","width=900,height=530,scrollbars=yes, menubar=no, resizable=yes");
			zNew=null;
		return w		
}
function customOutputWindow(pfilename) {
//	evt=event
//	zlink=null
	maxW=900
	maxH=530
	tox=0
	toy=0
		var w=window.open(pfilename,"ScriptTest","width=900,height=530,scrollbars=yes, menubar=yes, resizable=yes");
			zNew=null;
		return w		
}

	function customViewSmallWindow(pfilename) {
	maxW=400
	maxH=300
	tox=0
	toy=0
		var w=window.open(pfilename,"","width=400,height=300");
			zNew=null;
		return w		


}
	function customViewSmallWindowold(pfilename) {
	evt=event
	zlink=null
	maxw=400
	maxh=300
	tox=0
	toy=0
	if (arguments.length > 2) zNew=1;
	scrollH=(window.pageYOffset!=null)?window.pageYOffset:document.body.scrollTop;
	maxW=maxw?maxw:window.innerWidth?innerWidth:document.body.clientWidth;
	maxH=maxh?maxh:window.innerHeight?innerHeight:document.body.clientHeight;
	toX=tox?tox:0;
	toY=(toy?toy:0)+scrollH;
	fromX=evt.pageX?evt.pageX:evt.clientX;
	fromY=(evt.pageY?evt.pageY:evt.clientY)+(document.all?scrollH:0);
	adjX=toX+evt.screenX-fromX;
	adjY=toY+evt.screenY-fromY;
	if (document.createElement && document.body.appendChild && !zBox) {
		zBox=document.createElement("div");
		zBox.style.position="absolute";
		document.body.appendChild(zBox);
	} else if (document.all && !zBox) {
		document.all[document.all.length-1].outerHTML+="<div id=zBoxDiv style=position:absolute></div>";
		zBox=document.all.zBoxDiv;
	} else if (document.layers && !zBox) {
		zBox=new Layer(maxW);
		zBox.style=zBox;
	}
	zLink=zlink;
	doLaunchView();
		var w=window.open(pfilename,"","width="+maxW+",height="+maxH+",left="+adjX+",top="+adjY+",scrollbars=yes, menubar=no,status=yes, resizable=yes");
			zNew=null;
		return w		
}

function doLaunchView() {
zStep+=1;
zPct=(10-zStep)/10
	if (document.layers) {
		zBox.moveTo(toX+zPct*(fromX-toX),toY+zPct*(fromY-toY));
		zBox.document.open();
		zBox.document.write("<table width="+maxW*(1-zPct)+" height="+maxH*(1-zPct)+" border=2 cellspacing=0><tr><td></td></tr></table>");
		zBox.document.close();
		} else {
		zBox.style.border="2px solid #999999";
		zBox.style.left=toX+zPct*(fromX-toX);
		zBox.style.top=toY+zPct*(fromY-toY);
		zBox.style.width=maxW*(1-zPct);
		zBox.style.height=maxH*(1-zPct);
	}
	zBox.style.visibility="visible";
	if (zStep < 10) setTimeout("doLaunchView("+fromX+","+fromY+","+toX+","+toY+")",30);
	else {
		zBox.style.visibility="hidden";
		zStep=0;
			
	}
}

