﻿var WaitingTimerID = null
var WaitingWindowRunning = false
var WaitSecs
var Waitdelay = 1000
var CurrentCell=null
var CurrentEditCell=null
var Step="";
var CheckpointTypeID=0;
var userIsLive=0;
var checkpointPrePrice=-1;
var LastClickBtn =null;


function RefreshSelectbox(fromobj,toObj,t,k) {

	sendRequest("GetSelectboxInfo.aspx?type=" + t,RefreshView,k,"RefreshSelectbox");
}
function selectcp() {
	var y
	y= document.getElementById("CheckpointSele"); 
	var x = new Array();
	x = y.value.split("`");
	location.href="reports.aspx?k=" + x[0] + "," + x[1];
	//location.href="creports.aspx?b=";
}

function toggleDisplay(e, table1, table2){
element1 = document.getElementById(table1).style;
element2 = document.getElementById(table2).style;
 if (element1.display=='none') {
 		element1.display='block';
		element2.display='none';

		if (e == '2'){document.getElementById('textlink2').innerHTML="By Location";}
		else {document.getElementById('textlink').innerHTML="By Date";}
	}
 else {
  		element1.display='none';
		element2.display='block';

		if (e == '0') {document.getElementById('textlink').innerHTML="By Location";}
		else if(e == '2')  {document.getElementById('textlink2').innerHTML="By Category";}
		else {document.getElementById('textlink').innerHTML="By Category";}
	}
}
function swapArrowDirection(id, up){
	var image = document.getElementById('img' + id.substring(5));
	var len = image.src.length;
	if (image.src.substr(len-10,10)=='nArrow.gif' & up == 1){
		image.src='/Images/UpArrow.gif'}
	else {image.src='/Images/DownArrow.gif'}
}
function showHide(id, table){
   var table = document.getElementById(table);  
   var rows = table.getElementsByTagName("tr");  
   j=0;
   exp=0;
   for(i = 0; i < rows.length; i++){          
 //manipulate rows
 	if (rows[i].id=='sub'){
	
	 	if (exp==0){rows[i].style.display='none';}
	  	else {

	 		if(j % 2 == 0){
       			rows[i].className = "DTTableRow";
     		}
			else{
       			rows[i].className = "DTTableRowAlt";
     		} 
			if (rows[i].style.display=='none'){
				rows[i].style.display='block';
				j++
			}
			else{
				rows[i].style.display='none';
			}  
		}
	}
	else if (rows[i].id==id){
	swapArrowDirection(rows[i].id,1)
	exp=1;
	  	rows[i].style.display='block';
	 	if(j % 2 == 0){
       		rows[i].className = "DTTableRow";
     	}else{
       		rows[i].className = "DTTableRowAlt";
     	}   
	 	j++
	}
	else if (rows[i].id=='ttl'){
	  	rows[i].style.display='block';
	}
	else{
	swapArrowDirection(rows[i].id,0)
	exp=0;
		rows[i].style.display='block';
	 	if(j % 2 == 0){
       		rows[i].className = "DTTableRow";
     	}else{
       		rows[i].className = "DTTableRowAlt";
     	}   
	 		j++
	}
   }
}
function alternate(id){
 if(document.getElementsByTagName){  
   var table = document.getElementById(id);  
   var rows = table.getElementsByTagName("tr");  
   for(i = 0; i < rows.length; i++){          
 //manipulate rows
     if(i % 2 == 0){
       rows[i].className = "DTTableRow";
     }else{
       rows[i].className = "DTTableRowAlt";
     }      
   }
 }
}

function cqueryreport()
{
var pstr ;
var w;
var h;

	w=1000;
	h=600;
	y= document.getElementById("CheckpointSele"); 
	y1= document.getElementById("Period"); 
	y2= document.getElementById("locationSelec"); 
	y4= document.getElementById("RType"); 
	f= document.getElementById("Form1"); 
	
	//y3= f.getElementsByTagName("input"); 

	//for (z=0;z<y3.length;z++){  
	//		 if (y3[z].type=="radio") {
	//				if (y3[z].checked) {
	//					x=y3[z].value;
	//				}
	//		
	//		}
	//}
						
	if(y.value=="") {
		alert("Please select one checkpoint.")
	}
	else {
		var random_num = (Math.round((Math.random()*10000)+1))


		if(y4.value==0){
			pstr = "?CheckpointSele=" + y.value + "&Period=" + y1.value + "&locationSelec=" + y2.value  +"&EChk=0&rnd=" + random_num
			testwindow= window.open ("ReportQuery.aspx" + pstr, "LogReport" + random_num, "location=0,status=0,scrollbars=0,resizable=yes,width=" + w + ",height=" + h);
			}
		else if(y4.value==1){
			pstr = "?CheckpointSele=" + y.value + "&Period=" + y1.value + "&locationSelec=" + y2.value  +"&EChk=1&rnd=" + random_num
			testwindow= window.open ("ReportQuery.aspx" + pstr, "LogReport" + random_num, "location=0,status=0,scrollbars=0,resizable=yes,width=" + w + ",height=" + h);
					}
		else if(y4.value==2){
			pstr = "?CheckpointSele=" + y.value + "&Period=" + y1.value + "&locationSelec=" + y2.value  +"&rnd=" + random_num
			testwindow= window.open ("ReportChart.aspx" + pstr, "ChartReport", "location=0,status=0,scrollbars=yes,resizable=yes,width=" + 710 + ",height=" + 600);
		}
				else if(y4.value==3){
			pstr = "?CheckpointSele=" + y.value + "&Period=" + y1.value + "&locationSelec=" + y2.value  +"&rnd=" + random_num
			testwindow= window.open ("OPSumm.aspx" + pstr, "OverallPerf" + random_num, "location=0,status=0,scrollbars=yes,resizable=yes,width=" + 710 + ",height=" + 600);
				}
							else if(y4.value==4){
			pstr = "?CheckpointSele=" + y.value + "&Period=" + y1.value + "&locationSelec=" + y2.value  +"&rnd=" + random_num
			testwindow= window.open ("PBreakdown.aspx" + pstr, "PerfBD" + random_num, "location=0,status=0,scrollbars=yes,resizable=yes,width=" + 710 + ",height=" + 600);
		}
		
		var iMyWidth;
	var iMyHeight;
	//half the screen width minus half the new window width (plus 5 pixel borders).
	iMyWidth = (window.screen.width/2) - ((w/2) + 10);
	//half the screen height minus half the new window height (plus title and status bars).
	iMyHeight = (window.screen.height/2) - ((h/2) + 50);
	
	
		testwindow.moveTo(iMyWidth,iMyHeight);
		if (window.focus) {testwindow.focus()}
	}
}



function InitializeWaitTimer()
{
    // Set the length of the timer, in seconds
    WaitSecs = 30
    StopWaitClock()
    StartWaitTimer()
}

function StopWaitClock()
{
    if(WaitingWindowRunning){
    clearTimeout(WaitingTimerID)
	messageObj.close();	
	}
    WaitingWindowRunning = false
}

function StartWaitTimer()
{
    if (WaitSecs==0)
    {
        StopWaitClock()
		displayMessage('/includes/RequestTimeOut.html',350,150)

    }
    else
    {
        self.status = WaitSecs
        WaitSecs = WaitSecs - 1
        WaitingWindowRunning = true
        WaitingTimerID = self.setTimeout("StartWaitTimer()", Waitdelay)
    }
}



function ShowIcon (n) {
	var mySplitResult = n.src.split("_");
	if (mySplitResult.length==1) {
		var myNewString = mySplitResult[0].replace(".gif", "");
		n.src=myNewString + "_over.gif"
		
	}
	
}
function RestoreIcon (n) {
	var mySplitResult = n.src.split("_");

	if (mySplitResult.length==2) {
		n.src=mySplitResult[0] + ".gif"
	}
	
}





function MaxMinMe(n) { 

  x= GetMyParent("TABLE",n); //n.parentNode.parentNode.parentNode.parentNode;
  //alert(x.tagName) 

  for (j=0;j<x.childNodes.length;j++)
    {y=x.childNodes[j];
     //  alert(y.tagName) ;
     if (y.tagName=='TBODY') {
         if (y.innerHTML=="") {
       //    alert(y.tagName) 
             z=GetMyParent("TABLE",y);
             GetViewBody(z.getAttribute("listname"),GetKeychain(z),z);

            }
        if (y.style.display=='none')
             {y.style.display=''; }
         else
             {y.style.display='none'; 
			 }
      } }
 	  if (n.className=='HeadingMaxBtn') {
		  n.className='HeadingMinBtn'
	  }
	  else {
		  n.className='HeadingMaxBtn'
	  }
}

function ShowTestEmailSMS(thiscell, n) {
 var poststr
 
 var myEmailFormat=1
 x=GetMyParent("TBODY",thiscell).getElementsByTagName("select");
  for (j=0;j<x.length;j++) {
    if (x[j].name=="MessageFormatID") {
        myEmailFormat=x[j].value;
    }
  
  }
 
 x=GetMyParent("TD",thiscell);
 y=x.getElementsByTagName("input");
 if  (y.length==2) {
 var random_num = (Math.round((Math.random()*10000)+1))
  poststr="?m=" + n + "&to=" + y[0].value +"&r=" + random_num + "&f=" + myEmailFormat
  displayMessage('/members/TestEmailSMs.aspx' + poststr,300,150);
 }
 
 
}


function DeleteMe(n){
 x=GetMyParent("TR",n);
 CurrentCell=GetMyParent("TABLE",x);
	for (j=0;j<x.childNodes.length;j++)
	 {  y=x.childNodes[j]
	  if (y.getAttribute("d")=='1'){
		  //alert('includes/DeleteMsg.aspx?t=' +  GetMyType(y) + '&' + GetKeychain(y));
		  var random_num = (Math.round((Math.random()*10000)+1))

		  var poststr='?type=' +  GetMyType(y) + '&keys=' + GetKeychain(y) + '&rn=' + random_num
		  displayMessage('/includes/DeleteMsg.aspx' + poststr,300,150);
		  return false
	  
		  }
	}
}

function ModifyTopLevelControls(){
	y=document.getElementsByTagName("thead"); 
	var count=0;
	for (j=0;j<y.length;j++){  
		if (y[j].innerHTML.indexOf("E(this)")>0) {
			// check for the display status
			 x=GetMyParent("TABLE",y[j]) // x=n.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
			 x=GetMyParent("TR",x)
                if (x) { // from checkpoint page
			        y1=x.getElementsByTagName("td"); 
			        for (j=0;j<y1.length;j++)
			         {
			          if (y1[j].getAttribute("d")=='1' && y1[j].style.display=='')
				        {
					        count++
				        }
        			 
			        }
			        
			        
		        }
		        else { // from status page
		            y1=y[j].getElementsByTagName("td"); 
			        for (j=0;j<y1.length;j++)
			         {
			          if (y1[j].innerHTML.indexOf("E(this)")>0 && y1[j].style.display=='')
				        {
					        count++
				        }
        			 
			        }
		            
		        }
		        
		        	  




			
    		
		}
    }
	//alert(count);
	if (count==1) {
		for (j=0;j<y.length;j++){  
			if (y[j].innerHTML.indexOf("E(this)")>0){
				x=y[j].getElementsByTagName("a"); 
						for (z=0;z<x.length;z++){  
						
							if (x[z].innerHTML.indexOf("Edit")>0){
								x[z].style.display='';
							}
							if (x[z].innerHTML.indexOf("View")>0){
								x[z].style.display='';
							}
							if (x[z].innerHTML.indexOf("Basic")>0){
								x[z].style.display='';
							}
							if (x[z].innerHTML.indexOf("Advance")>0){
								x[z].style.display='';
							}
							
						}
				x=y[j].getElementsByTagName("tr");		
				if (x[0].className=="DTHeadingObjectViewInactive") {
					x[0].className="DTHeadingObjectView";
					}
			}
		}
	}
	
	// found more than 2 control rows hidden controls on top level
	if (count>1) {
		for (j=0;j<y.length;j++){  
			if (y[j].innerHTML.indexOf("E(this)")>0){
				if (count>1) {
					x=y[j].getElementsByTagName("a"); 
						for (z=0;z<x.length;z++){  
							if (x[z].innerHTML.indexOf("Edit")>0){
								x[z].style.display='none';
							}
							if (x[z].innerHTML.indexOf("View")>0){
								x[z].style.display='none';
							}
							if (x[z].innerHTML.indexOf("Basic")>0){
								x[z].style.display='none';
							}
							if (x[z].innerHTML.indexOf("Advance")>0){
								x[z].style.display='none';
							}
						}
				x=y[j].getElementsByTagName("tr");		
				if (x[0].className=="DTHeadingObjectView") {
					x[0].className="DTHeadingObjectViewInactive";
					}
				count--	
				}
			}
		}
		
	}
	
}





function RequestRefresh(result) {

if (result.responseText!="OK") {
	StopWaitClock();
	alert(result.responseText);	
	return;
}

	displayMessage('/includes/Wait.html',150,150)
	InitializeWaitTimer();
	type=CurrentCell.getAttribute("listname")
	x=GetMyParent("TD",CurrentCell)

	if (x) {

		sendRequest("GetView.aspx?type=" + CurrentCell.getAttribute("listname") ,RefreshView,GetKeychain(CurrentCell),"delete");
	}
	else {
		var urlarray=window.location.href.split('/')
		location.href=urlarray[urlarray.length-1].replace('#',"")
		//if (window.location.href.indexOf("getpage.aspx?type=checkpoints")!=0) {
		//	location.href="getpage.aspx?type=checkpoints"
		//}
		//else {
		//	location.reload(true);
		//}
	StopWaitClock();
	}
}

function RefreshView(result,A) {
	
	TContent=result.responseText;
	
	if (A=="save"){
		x=GetMyParent("TD",CurrentCell)
		x1=GetMyParent("TR",x)
		x1=GetMyParent("TABLE",x1)
		x1=GetMyParent("TD",x1)

	}
	else if (A=="delete") {
		x1=GetMyParent("TD",CurrentCell);
		
	}
	
	else if (A=="CreateNedit1") {
		var oldrow =GetMyParent("TR",CurrentEditCell)
		
			
		x1=GetMyParent("TABLE",CurrentEditCell);
		x1=GetMyParent("TD",x1);
		CurrentEditCell=x1;
		var newrow =GetMyParent("TR",CurrentEditCell)
		newrow.setAttribute('newk',oldrow.getAttribute('newk'))
		newrow.setAttribute('newt',oldrow.getAttribute('newt'))
	}
	
	else if (A=="CreateNedit2") {
		x1=GetMyParent("TABLE",CurrentEditCell);
		x1=GetMyParent("TD",x1);
		CurrentEditCell=null;
		Step="";
	}
	
	//if (x1) {
		x1.innerHTML=TContent;
 		ModifyTopLevelControls();
	//}
	//else {
	//	x.innerHTML=TContent;
	//}

  	StopWaitClock();
}



function LoadMe(me){
        
}


function AddMe(n) { 
   	x=GetMyParent("TR",n);
   	y=x.getElementsByTagName("select"); 
   	var stype=0;
   	
	if (y.length>0) {
	    if (y[0].selectedIndex<1){
		    alert("Please select one")
		    return;
	    }
   	 stype = y[0].value;
	 CheckpointTypeID=stype;
 	}
 	
 	
 	Step="Create";
  	x=GetMyParent("TABLE",n);
  	x=GetMyParent("TR",x);
	 if(x.className=="DTTableRow"){
		 x.className="";
	 }

	for (j=0;j<x.childNodes.length;j++)
	 {  y=x.childNodes[j]
	  if (y.getAttribute("d")=='1'){
		  	y.style.display='';
		  	mykey=GetKeychain(y);
		  	mytype=GetMyType(y)
			Step=="CreateNedit"
		  	 // if (y.getAttribute("parentkn")){
			//	  mykey= mykey.replace('Checkpoint=1', y.getAttribute("parentkn") + '=' + y.getAttribute("parentk"))
			 // }
			  
			//if (mytype=="TransactionStep" && mykey.indexOf("Checkpoint=")!=0){
		  	//    mykey="Checkpoint=" + mykey
				//  Step=="CreateNedit"
		  	//}
		if (mytype=="Checkpoint") {
		    PPlanid= document.getElementById("ACCPPlan"); 
		    mykey+=",ACCPPlan=" + PPlanid.value
		}
			GetEditView(mytype+"."+ stype +".new",mykey,y);
			return
		  }
	  else
		{y.style.display='none';}
	  } 
}

function PopSpotCheck(CheckpointID,CheckpointTypeID){
	var w=720;
	var h=600;
	var random_num = (Math.round((Math.random()*10000)+1))
	var isValid = true;
	
  		var f = document.createElement('form');
		f.method = 'post';
		f.target = 'Result' + random_num;

		var y = document.createElement('input');
		y.value = CheckpointTypeID;
		y.id = 'CType';
		y.name = 'CType';
		y.type = 'hidden';
		f.appendChild(y);
		var y = document.createElement('input');
		y.value = CheckpointID;
		y.id = 'CID';
		y.name = 'CID';
		y.type = 'hidden';
		f.appendChild(y);
		y = document.createElement('input');
		y.value = random_num;
		y.id = 'rndm';
		y.name = 'rndm';
		y.type = 'hidden';
		f.appendChild(y);

testwindow= window.open ("", "Result" + random_num, "location=0,status=0,scrollbars=0,resizable=yes,width=" + w + ",height=" + h);
		
		f.action = 'CTResults.aspx';
		document.body.appendChild(f);
		f.submit();
		document.body.removeChild(f)
		
		var iMyWidth;
		var iMyHeight;
		//half the screen width minus half the new window width (plus 5 pixel borders).
		iMyWidth = (window.screen.width/2) - ((w/2) + 10);
		//half the screen height minus half the new window height (plus title and status bars).
		iMyHeight = (window.screen.height/2) - ((h/2) + 50);
		testwindow.moveTo(iMyWidth,iMyHeight);
		if (window.focus) {testwindow.focus()}
		
}
function validateme(n) { 
 x=GetMyParent("TABLE",n)
 x.setAttribute('haschanged','true');
 validateInput(n);
  
	 // if valid on panel table check parent table as well 
	 if (x.getAttribute("class")=="PanelBox") {
		  x=GetMyParent("TABLE",x)
		 x.setAttribute('haschanged','true');
	 }
 	// Search for any panel on the table
 	else {
		y1=x.getElementsByTagName("TABLE"); 
		for (j=0;j<y1.length;j++)
			 {
			  if (y1[j].getAttribute("class")=="PanelBox"){
				 x=y1[j]
				 x.setAttribute('haschanged','true');
			  }
		}
		
	}

 }

function MinMe(n) { 
  var mytable =GetMyParent("TABLE",n)
  if (mytable) {
  	var newrow =GetMyParent("TR",mytable)
	if (newrow) {
	  if (newrow.getAttribute("newt")){
				location.reload(true);
				CurrentEditCell=null;
				Step="";
				return;
		}
	}
  }
 x=GetMyParent("TABLE",n) // x=n.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
 x=GetMyParent("TR",x)
 
 if (x)  { // if row exist
  if(x.className==""){
	 x.className="DTTableRow";
  }
   y1=x.getElementsByTagName("td"); 
  
	for (j=0;j<y1.length;j++)
	 {
	  if (y1[j].getAttribute("d")=='1' && y1[j].style.display=='')
		{MinChild(y1[j])}
	}
	ModifyTopLevelControls();
  
   }
else { // if row not foudn
	location.href ='status.aspx'
}


	
}

function MinChild(n) { 
  x=n.parentNode;
 //alert(x.tagName) 
  // y=x.getElementsByTagName("td"); 
  
for (j=0;j<x.childNodes.length;j++)
 {  y=x.childNodes[j]
  if (y.getAttribute("d")=='1')
    {y.style.display='none';}
  else
    {y.style.display='';}
  } 
  
  x=x.parentNode;
//alert(x.tagName) 
  
for (j=0;j<x.childNodes.length;j++)
 {  y=x.childNodes[j]
 //  alert(y.tagName) 
  if (y.tagName=="TR")
    {y.style.display='';}
    } 
    
  x=x.parentNode;
y=x.getElementsByTagName("thead"); 
y[0].style.display=''
 
 if (x.parentNode.tagName!='FORM')
    { z=GetMyParent("TBODY",x);
  
      for (j=0;j<z.childNodes.length;j++)
         { y=z.childNodes[j]
      //  alert(y.tagName) 
           if (y.tagName=="TR")
              {y.style.display='';}
         } 

     }
 }
 
 
function A(n) { 
//alert(n.src)
//alert(n.src.indexOf('Advance'))
  x=GetMyParent("TABLE",n) //x=n.parentNode.parentNode.parentNode.parentNode;
//alert(x.tagName + " - " + x.name) 
   y1=x.getElementsByTagName("tr"); 

	if (n.src.indexOf('Advance')>0){
		for (j=0;j<y1.length;j++)
		 { //alert(y1[j].tagName + " - " + y1[j].getAttribute("L")) 
		  if (y1[j].getAttribute("L")=='0')
			{y1[j].style.display='none'}
		  if (y1[j].getAttribute("L")=='1')
			{y1[j].style.display=''}
		  if (y1[j].getAttribute("L")=='2')
			{y1[j].style.display=''} 
		}
		n.src="/Images/Basic.gif"
		n.title="Switch to Basic Mode"
		n.alt="Switch to Basic Mode"
		//alert(n.src);
	}

	else {
		for (j=0;j<y1.length;j++)
		 { //alert(y1[j].tagName + " - " + y1[j].getAttribute("L")) 
		  if (y1[j].getAttribute("L")=='0')
			{y1[j].style.display=''}
		  if (y1[j].getAttribute("L")=='1')
			{y1[j].style.display=''}
		  if (y1[j].getAttribute("L")=='2')
			{y1[j].style.display='none'} 
		}
		n.src="/Images/Advance.gif"
		n.title="Switch to Advanced Mode"
		n.alt="Switch to Advanced Mode"
		//alert(n.src);
	}
	
}
function B(n) { 
  x=GetMyParent("TABLE",n) //x=n.parentNode.parentNode.parentNode.parentNode;
//alert(x.tagName) 
   y1=x.getElementsByTagName("tr"); 
  
for (j=0;j<y1.length;j++)
 {
  if (y1[j].getAttribute("L")=='0')
    {y1[j].style.display=''}
  if (y1[j].getAttribute("L")=='1')
    {y1[j].style.display=''}
  if (y1[j].getAttribute("L")=='2')
    {y1[j].style.display='none'} 
}
} 


function V(n) { 
  x=GetMyParent("TABLE",n) //x=n.parentNode.parentNode.parentNode.parentNode;
//alert(x.tagName + " - " + x.name) 
   y1=x.getElementsByTagName("td"); 
  
for (j=0;j<y1.length;j++)
 { //alert(y1[j].tagName + " - " + y1[j].getAttribute("L")) 
  if (y1[j].getAttribute("V")=='V')
    {y1[j].style.display=''}
  if (y1[j].getAttribute("V")=='E')
    {y1[j].style.display='none'}
 
}
}
function E(n) { 
  x=GetMyParent("TABLE",n) //x=n.parentNode.parentNode.parentNode.parentNode;
//alert(x.tagName + " - " + x.name) 
   y1=x.getElementsByTagName("td"); 
	if (n.src.indexOf('Edit')>0){
		for (j=0;j<y1.length;j++)
		 { //alert(y1[j].tagName + " - " + y1[j].getAttribute("L")) 
		  if (y1[j].getAttribute("V")=='E')
			{y1[j].style.display=''}
		  if (y1[j].getAttribute("V")=='V')
			{y1[j].style.display='none'}
		 
		}
		n.src="/Images/View.gif"
		n.title="Switch to View Mode"
		n.alt="Switch to View Mode"
		
	}
	else {
			for (j=0;j<y1.length;j++)
		 { //alert(y1[j].tagName + " - " + y1[j].getAttribute("L")) 
		  if (y1[j].getAttribute("V")=='V')
			{y1[j].style.display=''}
		  if (y1[j].getAttribute("V")=='E')
			{y1[j].style.display='none'}
		 
		}
		n.src="/Images/Edit.gif"
		n.title="Switch to Edit Mode"
		n.alt="Switch to Edit Mode"
	}

}

function GetMyParent(Type,me) { 
		if (me) {
			  var p = me.parentNode
			  if (p) {
				 //alert("-" + p.tagName + "-" + Type  + "-" )
				 //alert(p.tagName==Type)
				 if (p.tagName=="FORM")
					{return null}
				 if (p.tagName==Type)
					{return p}
				  else
					{return GetMyParent(Type,p)}
				
			  }
			  else {
				  return null
			  }
		}
		else {
			return null;	
		}
  }

function GetCreateParentRow(focusCell) {
	var newrow =GetMyParent("TR",CurrentEditCell);
	var found =false;
	if (newrow) {
		if (newrow.getAttribute('newk')) {
			found=true;
		}
	}
	if (found==false) {
		// newrow =GetMyParent("TR",focusCell);
		newrow=GetMyParent("TR",GetMyParent("TABLE",focusCell))
		 if (newrow) {
				if (newrow.getAttribute('newk')) {
					found=true;
				}
				
		 }
	}
	
	if (found==false) {
		return null
	}
	else {
	   return newrow
	}
	
}


function UserAgreeForPriceChange(me) {
	if (userIsLive!=1) {
		return true;	
	}
	else {
		var chkdefaultprice = -1
		var chkSelprice = -1
		x=GetMyParent("TABLE",me)	
		y=x.getElementsByTagName("span"); 
		if (y.length==0) {
			return true;
		}
		else {
		
				for (z=0;z<y.length;z++) {
					if (y[z].innerHTML.indexOf("Price")>=0) {
						chkdefaultprice=y[z].getAttribute('defaultprice');
						chkSelprice=y[z].innerHTML.replace('Price', '')
						chkSelprice=chkSelprice.replace('$', '')
						chkSelprice=chkSelprice.replace(':', '')
						chkSelprice=chkSelprice.replace(/ /g, '')
						
						if (chkdefaultprice!=chkSelprice) {
							  var poststr = "Lp=" + chkdefaultprice + "&Cp=" + chkSelprice
							  displayMessage('/includes/PriceChanged.aspx?' + poststr,300,150);
							  return false;
						}
						else {
							return true;	
						}
					}
				
				}
		}
	}
}

function SaveMe(me,ToEnable) {


	
		 
		var KC = GetKeychain(me)
		var IsCreateNEdit =false;
		
			var newrow =GetCreateParentRow(me)
			if (newrow) {
				IsCreateNEdit=true;
				KC='Checkpoint' + String.fromCharCode(31) + newrow.getAttribute('newk') 
		
			}
		if (KC==""){
			KC="CheckpointStateEnable=" + ToEnable 
		}
		else {
			KC=KC+ ",CheckpointStateEnable=" + ToEnable 
		}
		
		// multiple keys
		KC= KC.replace(/,/g, String.fromCharCode(30) )
		KC= KC.replace(/=/g, String.fromCharCode(31) )
		
		var d =KC
		if (d.length>0){d=d + String.fromCharCode(30)}
		
		var IsValid=true
		qs="";
		x=GetMyParent("TBODY",me)
		y=x.getElementsByTagName("TABLE");
		
		for (i=0;i<y.length;i++) 
		   {
			  if (i==0) {
				  CurrentCell=y[i]
			  }
		
			 
			///look for each input
			if (y[i].getAttribute("haschanged")== "true" ||ToEnable==1||ToEnable==4) {
				var mytype=GetMyType(y[i]);
				var myid=GetMyKey(y[i]);
				var stype =""
				var temptype=mytype.split(".")
				
				
				//if (Step=="CreateNedit1") {
					if (IsCreateNEdit) {
						temptype= newrow.getAttribute('newt').split(".")
					}
				//}
		
				if (temptype.length >1) {
					mytype=temptype[0]
					stype=temptype[1]
				}
				
				
				//if (Step=="CreateNedit1") {
				if (IsCreateNEdit) {
					d= d+ "type" + String.fromCharCode(31) + mytype + String.fromCharCode(30) + "stype" + String.fromCharCode(31) + stype + String.fromCharCode(30) + "id" + String.fromCharCode(31) +  newrow.getAttribute('newk')   + String.fromCharCode(30)
				}
				else {
					
					if (mytype=="" &&  stype =="" & myid=="") {
						
					}
					else {
					d= d+ "type" + String.fromCharCode(31) + mytype + String.fromCharCode(30) + "stype" + String.fromCharCode(31) + stype + String.fromCharCode(30) + "id" + String.fromCharCode(31) +  myid   + String.fromCharCode(30)
					}
				}
		
			
		
			y1=y[i].getElementsByTagName("input"); 
		
			for (z=0;z<y1.length;z++) {
				if (y1[z].type=="checkbox") {
					if (y1[z].checked) {
						d=d+ y1[z].name + String.fromCharCode(31) + y1[z].value + ',1' + String.fromCharCode(30)
					}
					else {
						d=d+ y1[z].name + String.fromCharCode(31) + y1[z].value + ',0' + String.fromCharCode(30)
					}
				}
				else if (y1[z].type=="radio") {
					if (y1[z].checked) {
						d=d+ y1[z].name + String.fromCharCode(31) + y1[z].value + String.fromCharCode(30)
					}
				}
				
				else {
				d=d+ y1[z].name + String.fromCharCode(31) + y1[z].value + String.fromCharCode(30)
				}
				
			}
		
			
			///look for each select box
		   y1=y[i].getElementsByTagName("select"); 
			for (z=0;z<y1.length;z++) {
				d=d+ y1[z].name + String.fromCharCode(31) + y1[z].value + String.fromCharCode(30)
			}
		   
		   
			///look for each textarea box
		   y1=y[i].getElementsByTagName("textarea"); 
			for (z=0;z<y1.length;z++) {
				d=d+ y1[z].name + String.fromCharCode(31) + y1[z].value.replace('__VIEWSTATE=','__VIEWSTATEXXX=') + String.fromCharCode(30)
			}
		   
 				if (ToEnable!=4) { // test checkpoint
   					d=d.substring(0,d.length-1);
		  		 	d=d + String.fromCharCode(29)
		   
			    }
		  
		 
		   IsValid=true //IsValid && isFormValid(y[i])
		 }
		 }
		 
		 
		
		   
		 if (d.length!=KC.length+1) {
		   d=d.substring(0,d.length-1);
		   if (isFormValid(me)==false){
			 displayMessage('/includes/ErrorOnSubmit.html',350,150)}
		  else {
			 
			     if (ToEnable==4) { // test checkpoint
			        TestCheckpoint (stype,d)
			        return;
			    }
		  
		  
			 	LastClickBtn=me;
   	            me.setAttribute('para', "SaveMe" + "`" + ToEnable)
            	
	            if (UserAgreeForPriceChange(me)) {
		            me=LastClickBtn;
		            LastClickBtn=null;
		            me.setAttribute('para', "")
		 
			         // GetMyParent("TBODY",x).innerHTML;
			          //x= GetMyParent("TABLE",me); //n.parentNode.parentNode.parentNode.parentNode;
			         // x= GetMyParent("tr",x); //n.parentNode.parentNode.parentNode.parentNode;
			         // x= GetMyParent("TABLE",x); //n.parentNode.parentNode.parentNode.parentNode;
			        displayMessage('/includes/Wait.html',150,150)
			        InitializeWaitTimer();
        			
			         sendRequest( "SaveData.aspx" , SaveDataResponse,d,"save")
			         
			    }
		  }
		 }
		//alert(KC +"\n" + d)
	
}

function CreateMe(me,stype,action) {

		CurrentCell=GetMyParent("TABLE",GetMyParent("TABLE",me));

		var KC = GetKeychain(me)
		// multiple keys
		KC= KC.replace(/,/g, String.fromCharCode(30) )
		KC= KC.replace(/=/g, String.fromCharCode(31) )
		
		var d =KC
		
		
		 if (d.length>0){d=d + String.fromCharCode(30)}
			var IsEnable=0
			if (action==1) {
				IsEnable=1;
			}
			
			else if (action==2) {
			
				Step="CreateNedit";
				CurrentEditCell=me;
				IsEnable=0;
			}
			
			// request button from new transaction
			else if (action==3) {
				Step="TransactionRequest";
				CurrentEditCell=me;
				IsEnable=0;
				d=d + "IsTransactionRequest" + String.fromCharCode(31) + '1' + String.fromCharCode(30)
				
			}
		
			
			
			 else{
				 IsEnable=0;
			 }
		 
		 d=d + "IsEnable" + String.fromCharCode(31) + IsEnable + String.fromCharCode(30)
		var IsValid=true
		x=GetMyParent("TBODY",me)
		y=x.getElementsByTagName("TABLE");
		for (i=0;i<y.length;i++) 
		   {
			var newrow =GetMyParent("TR",me)
			newrow.setAttribute('newt',GetMyType(y[i]) + "." + stype)
				
			d= d+ "type" + String.fromCharCode(31) + GetMyType(y[i]) + String.fromCharCode(30) + "stype" + String.fromCharCode(31) + stype + String.fromCharCode(30) + "id" + String.fromCharCode(31) + GetMyKey(y[i])  + String.fromCharCode(30) 
			y1=y[i].getElementsByTagName("input"); 
		
		
			for (z=0;z<y1.length;z++) {
				if (y1[z].type=="checkbox") {
					if (y1[z].checked) {
						d=d+ y1[z].name + String.fromCharCode(31) + y1[z].value + ',1' + String.fromCharCode(30)
					}
					else {
						d=d+ y1[z].name + String.fromCharCode(31) + y1[z].value + ',0' + String.fromCharCode(30)
					}
				}
				else if (y1[z].type=="radio") {
					if (y1[z].checked) {
						d=d+ y1[z].name + String.fromCharCode(31) + y1[z].value + String.fromCharCode(30)
					}
				}
				
				else {
				d=d+ y1[z].name + String.fromCharCode(31) + y1[z].value + String.fromCharCode(30)
				}
				
			}
			
			///look for each select box
		   y1=y[i].getElementsByTagName("select"); 
			for (z=0;z<y1.length;z++) {
				d=d+ y1[z].name + String.fromCharCode(31) + y1[z].value + String.fromCharCode(30)
			}
		   
		   
			///look for each textarea box
		   y1=y[i].getElementsByTagName("textarea"); 
			for (z=0;z<y1.length;z++) {
				d=d+ y1[z].name + String.fromCharCode(31) + y1[z].value.replace('__VIEWSTATE=','__VIEWSTATEXXX=') + String.fromCharCode(30)
				//d=d+ y1[z].name + String.fromCharCode(31) + y1[z].value + String.fromCharCode(30)
			}
			   
		   d=d.substring(0,d.length-1);
		   d=d+"|"
		   IsValid=true //IsValid && isFormValid(y[i])
		 }
		   d=d.substring(0,d.length-1);
		  if (isFormValid(me)==false){
			 displayMessage('/includes/ErrorOnSubmit.html',350,150)}
		  else {
		  
		  
		  	if (action==4) { // test checkpoint
			    TestCheckpoint (stype,d)
			    return;
			}
		  
		  
		  	LastClickBtn=me;
	        me.setAttribute('para', "CreateMe" + "`" + stype + "`" + action)
	        if (UserAgreeForPriceChange(me)) {
		        me=LastClickBtn;
		        LastClickBtn=null;
		        me.setAttribute('para', "")
			    displayMessage('/includes/Wait.html',150,150)
			    InitializeWaitTimer();
			    sendRequest( "CreateNew.aspx" , SaveDataResponse,d,"creat")
			}
		  }
		//alert(KC +"\n" + d)
	
}


function TestCheckpoint(chktype,data){
	var w=720;
	var h=600;
	var random_num = (Math.round((Math.random()*10000)+1))
	var isValid = true;
	
	
	var alldata=data.split(String.fromCharCode(30));

  		var f = document.createElement('form');
		f.method = 'post';
		f.target = 'Result' + random_num;

		var y = document.createElement('input');
		y.value = chktype;
		y.id = 'CType';
		y.name = 'CType';
		y.type = 'hidden';
		f.appendChild(y);
		y = document.createElement('input');
		y.value = random_num;
		y.id = 'rndm';
		y.name = 'rndm';
		y.type = 'hidden';
		f.appendChild(y);

	
	for (z=0;z<alldata.length;z++) {
	    // for each row
	    Col=alldata[z].split(String.fromCharCode(31));
		y = document.createElement('input');
		
		// if the input value contain "<", replace it with charcode 29
		Col[1]= Col[1].replace(/</g, String.fromCharCode(29) )
		y.value = Col[1];
		y.id = Col[0];
		y.name = Col[0];
		y.type = 'hidden';
		f.appendChild(y);
		

	}
	
	
	
	

     
		testwindow= window.open ("", "Result" + random_num, "location=0,status=0,scrollbars=0,resizable=yes,width=" + w + ",height=" + h);
		
		f.action = 'CTResults.aspx';
		document.body.appendChild(f);
		f.submit();
		document.body.removeChild(f)
		
		var iMyWidth;
		var iMyHeight;
		//half the screen width minus half the new window width (plus 5 pixel borders).
		iMyWidth = (window.screen.width/2) - ((w/2) + 10);
		//half the screen height minus half the new window height (plus title and status bars).
		iMyHeight = (window.screen.height/2) - ((h/2) + 50);
		testwindow.moveTo(iMyWidth,iMyHeight);
		if (window.focus) {testwindow.focus()}
		
}
function Validate(vFunc,vvalue){
 f = vFunc.substring(0,vFunc.length-1) ;
 f=f+ ",\"" + vvalue + "\")" ;
 alert(f);
 return eval(f)

}
function VN(min,max,req,val){
 alert(val)
 if (!IsNumeric(val)) {return "Input must be numeric" }
 if (val<min || val>max) {return "Input must be between " + min + " and " + max}
 return "OK"
}

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }


function EditMe(n) { 
  x=GetMyParent("TR",n);

 if(x.className=="DTTableRow"){
	 x.className="";
 }

  
for (j=0;j<x.childNodes.length;j++)
 {  y=x.childNodes[j]
  if (y.getAttribute("d")=='1'){
      y.style.display='';
      if (y.innerHTML=="") {
		  CheckpointTypeID=GetMyType(y);
		  GetEditView(GetMyType(y),GetKeychain(y),y);
		  ModifyTopLevelControls();
         return
      }
      }
  else
    {y.style.display='none';}
  } 

x= GetMyParent("TBODY",n) //x=n.parentNode.parentNode.parentNode;
//alert(x.tagName) 
  
for (j=0;j<x.childNodes.length;j++)
 {  y=x.childNodes[j]
 //  alert(y.tagName) 
  if (y.tagName=="TR")
    {y.style.display='none';}
    } 

x=x.parentNode;
//alert(x.tagName)
y=x.getElementsByTagName("thead"); 
y[0].style.display='none'

if (x.parentNode.tagName!='FORM')
    {
 z= GetMyParent("TBODY",x);

 for (j=0;j<z.childNodes.length;j++)
 {  y=z.childNodes[j]
 //  alert(y.tagName) 
  if (y.tagName=="TR")
    {y.style.display='none';}
    } 
  z=GetMyParent("TR",x);
//     alert(z.tagName) 
  z.style.display='';
  }

  x=GetMyParent("TR",n);
  x.style.display='';
  ModifyTopLevelControls();
  
 
}


function SaveDataResponse(result){
var responsearray=result.responseText.split('`')

if (responsearray[0]!="OK") {
	StopWaitClock();
	alert(responsearray[0]);	
	return;
}


	var kstr="";
	var mtype="";	
	if (CurrentCell.getAttribute("listname")) {
		mtype=CurrentCell.getAttribute("listname");
	}
	else {
		mtype=GetMyType(CurrentCell);
	}

	//kstr=BuildKeychain(CurrentCell)
	kstr=GetKeychain(CurrentCell)


		if (mtype=='' && kstr=='') {
			var mylink=location.href 
			mylink=mylink.substring(mylink.lastIndexOf('?')+1,mylink.length)
			mylink=mylink.replace('type=', '')
			var mypara=mylink.split("&keys=")
			mtype=mypara[0];
			kstr=mypara[1].replace('#', '');
		}
		
		
//location.href.indexOf("type=checkpoint.")>0 || location.href.indexOf("type=reselleraccount.")>0
	 if (kstr.indexOf("pp=1,")>=0) {
		 	location.reload(true);
			StopWaitClock();
			return;
	 }
	 
	
	//='getpage.aspx?type=checkpoint.' + str[1] + '&keys=Checkpoint=' + str[0]
	x=GetMyParent("TD",CurrentCell)
	if (x) {
		//StopWaitClock();
		
       x=GetMyParent("TD",CurrentCell)
		x1=GetMyParent("TR",x)
		
		if (x1.getAttribute("pk")) {
		     sendRequest("GetView.aspx?type=" + x1.getAttribute("pt"),RefreshView,x1.getAttribute("pk"),"save");
		}
		else {
		    sendRequest("GetView.aspx?type=" + mtype,RefreshView,kstr,"save");
		}
		
	}
	else {
		
		if 	(Step=="CreateNedit") {
			var newrow =GetMyParent("TR",CurrentEditCell)
			newrow.setAttribute('newk',responsearray[1])
			
			var parentrow=GetMyParent("TR",GetMyParent("TABLE",CurrentEditCell))
	 		parentrow.setAttribute('k',responsearray[1])
			
			Step="CreateNedit1"
			sendRequest("GetView.aspx?type=" +  newrow.getAttribute('newt') + '&keys=Checkpoint=' + responsearray[1] + '&IsEdit=1',RefreshView,"","CreateNedit1");
			//GetView.aspx?type=Checkpoint.5&keys=Checkpoint=277

		}
		else {
		
	    location.reload(true);
	    StopWaitClock();
		}
	}
	

}


function GetKeychain(me) { 
 var kc = BuildKeychain(me)
 return kc.substring(0,kc.length-1)
}
function BuildKeychain(me) { 
 var p = me.parentNode
 //alert(p.tagName + " - " + p.getAttribute("k"))
 if (p.tagName=="FORM")
    {return ""}
    

 if (p.getAttribute("k")!=null)
    {return BuildKeychain(p) + p.getAttribute("k") + "," }
 
 
 if (p.getAttribute("kn")!=null)
    {return BuildKeychain(p)  + "pp=" + p.getAttribute("pp") + "," + p.getAttribute("kn") + "=" }

 return BuildKeychain(p)
 }
 var subtype
 function GetMyType(me) { 
 var p = me.parentNode
 //alert(p.tagName + " - " + p.getAttribute("k"))
 if (subtype==null) {subtype= ""}
 if (p.tagName=="FORM")
    {return ""}
 
 if (p.getAttribute("kn")!=null)
    { 
    fulltype=p.getAttribute("kn")+subtype;
    subtype=null;
    return fulltype
     }
 if (p.getAttribute("T")!=null)
    {subtype= "." + p.getAttribute("T") }

 return GetMyType(p)
 } 
 function GetMyKey(me) { 
 var p = me
 //alert(p.tagName + " - " + p.getAttribute("k"))
 if (p.tagName=="FORM")
    {return ""}
 
 if (p.getAttribute("k")!=null)
    {return p.getAttribute("k") }

 return GetMyKey(p.parentNode)
 }   
function AOC(n) { 

  x= GetMyParent("TR",n); 
  y1=x.getElementsByTagName("td"); 
  for (j=0;j<y1.length;j++)
 { //alert(y1[j].tagName + " - " + y1[j].title) 
 if (y1[j].title=='newoptions')
   {if (n.value=='N')
    {y1[j].style.display=''}
  else
    {y1[j].style.display='none'}
   }
 if (y1[j].title=='copyoptions')
   {if (n.value=='C')
    {y1[j].style.display=''}
  else
    {y1[j].style.display='none'}
   }
 if (y1[j].title=='shareoptions')
   {if (n.value=='S')
    {y1[j].style.display=''}
  else
    {y1[j].style.display='none'}
   }
}
}



var container ;
function GetEditView(type,keys,c){
	container=c;
	//alert('1');
	//c.innerHTML="----loading----";
	//messageObj = new DHTML_modalMessage(); 
	displayMessage('/includes/Wait.html',150,150)
	InitializeWaitTimer();
	sendRequest("GetView.aspx?type=" + type,ReceiveEditView,keys,"")
}

function ReceiveEditView(result){
	//alert(result.responseText)
	container.innerHTML=result.responseText;
	 EditMe(container);
	 
	 if (Step=="Create") {
		// swicth to Edit Mode
		y=container.getElementsByTagName("img")
		 for (j=0;j<y.length;j++){
			 if (y[j].src.indexOf("/Images/Edit.gif")>0) {
				 E(y[j]);
			 }
		 }
	 }
	 Step="";
	//messageObj.close();	
	 StopWaitClock()
}
var body;
function GetViewBody(type,keys,b){
	//alert(type + "\n" + keys + "\n" + b.tagName + "\n(" + b.innerHTML + ")")
	body=b;
	//c.outerHTML="<table><tbody><tr><td>----loading----</td></tr></tbody></table>";
	displayMessage('/includes/Wait.html',150,150)
	InitializeWaitTimer();
	sendRequest("GetViewBody.aspx?type=" + type,ReceiveViewBody,keys,"")
}

function ReceiveViewBody(result){
 //alert(result.responseText);

 if (body.outerHTML) 
  {  body.outerHTML= result.responseText;}
 else
  {  var html = result.responseText
    html=html.substring(html.indexOf(">")+1,html.lastIndexOf("<"))
  	
	//alert(html)
    body.innerHTML= html;};
	 StopWaitClock()
}




 function importNode (node, allChildren) {
	/* find the node type to import */
				alert(node.nodeType + "  - " + node.tagName);
switch (node.nodeType) {
		case 1:
	/* create a new element */
			var newNode = document.createElement(node.nodeName);
			/* does the node have any attributes to add? */
			if (node.attributes && node.attributes.length > 0)
				/* add all of the attributes */
				for (var i = 0, il = node.attributes.length; i < il;)
				   { alert(node.attributes[i].nodeName + " - "  +node.getAttribute(node.attributes[i].nodeName));
					newNode.setAttribute(node.attributes[i].nodeName, node.getAttribute(node.attributes[i++].nodeName));}
			/* are we going after children too, and does the node have any? */
			if (allChildren && node.childNodes && node.childNodes.length > 0)
				/* recursively get all of the child nodes */
				for (var i = 0, il = node.childNodes.length; i < il;)
					newNode.appendChild(importNode(node.childNodes[i++], allChildren));
			return newNode;
			break;
		case 3:
		return document.createTextNode(node.nodeValue);
			break;
	}
};
function sendRequest(url,callback,postData,action) {
   var timestamp = new Date();
   

		   
    
	
 // url = url + "&ts=" + timestamp.getTime();
 //   alert(url);
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
			document.write(req.responseText);
			return;
		}
		callback(req,action);
		
	}
	
	if (req.readyState == 4) return;
	req.send(postData);

}

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}
function viewDiagnostic(cid, ename){
	
	var w;
	var h;
	w=1000;
	h=600;
	var random_num = (Math.round((Math.random()*10000)+1))
	testwindow= window.open ("Reports/Diagnostics.aspx", "Diagnostics" + random_num, "location=0,status=0,scrollbars=1,resizable=yes,width=" + w + ",height=" + h);
	
	f = document.createElement('form');
	f.action = 'Reports/Diagnostics.aspx';
	f.method = 'post';
	f.target = 'Diagnostics' + random_num;
	i = document.createElement('input');
	i.id = 'cid';
	i.name = 'cid';
	i.type = 'hidden';
	i.value = cid;
	f.appendChild(i);
	i = document.createElement('input');
	i.id = 'ename';
	i.name = 'ename';
	i.type = 'hidden';
	i.value = ename;
	f.appendChild(i);
	document.body.appendChild(f);
	f.submit();
	document.body.removeChild(f)
}
