function showLevelCtrl(mode)
{

  if(mode == 'show')
  {    
    editorBox();
  }
  else if(mode == 'hide')
  {
    //document.getElementById("levelCtrl").style.display = "none";
    closeEditorBox();
  } 
  //return false;
}

underscore = {
    position: function (element) {
        var p = {x: element.offsetLeft || 0, y:element.offsetTop || 0};
        while (element = element.offsetParent) {
            p.x += element.offsetLeft;
            p.y += element.offsetTop;
        }
        return p;
    }
}

speed=80;//time to loop in milliseconds
sy=50;//nr pixels srolled/loop

function goD(d){
//check if div position is above or below the body srollTop
if(document.getElementById(d).offsetTop<document.body.scrollTop){
window.scrollBy(0,-sy);
if(document.getElementById(d).offsetTop>=document.body.scrollTop){
document.body.scrollTop=document.getElementById(d).offsetTop;
clearTimeout(w);
}
else{var w=setTimeout('goD(\''+d+'\')',speed);}
}
else{
window.scrollBy(0,sy);
if(document.getElementById(d).offsetTop<=document.body.scrollTop){
document.body.scrollTop=document.getElementById(d).offsetTop;
clearTimeout(w);
}
else{var w=setTimeout('goD(\''+d+'\')',speed);}
}
}



function editorBox()
{	
var p = underscore.position(document.getElementById("editorBox"));
	document.getElementById("editorBox").style.width =340 ;//290
	document.getElementById("editorheader").style.width =340;   //290;
	document.getElementById("editorclose").style.left = 320;
	document.getElementById("editorcontents").style.width =330 ;   //290;	
	document.getElementById("editorclose").style.top = 1;
   	var x,y;
   		x = event.clientX + document.body.scrollLeft
   	if (x > 700)
   	{
   		x = event.clientX + document.body.scrollLeft-30
   	}
			y = event.clientY + document.body.scrollTop		
	
		//	alert(y)
		//alert(document.body.scrollTop+'||'+document.getElementById("editorBox").offsetTop)
			//		alert(document.getElementById("editorBox").offsetTop)	
			
       if ( y < 100)
		{
		document.getElementById("editorBox").style.left=x-310;
		document.getElementById("editorBox").style.top=y;
		 window.scrollBy(0,-50);
		}		
		else if (y > 101 && y < 200)
		{
		document.getElementById("editorBox").style.left=x-310;
		document.getElementById("editorBox").style.top=y;
		}	
		else if (y > 201 && y < 400)
		{
		document.getElementById("editorBox").style.left=x-310;
		document.getElementById("editorBox").style.top=y-100;
		}
		else
		{
		
		     if (y > 401 && y < 550 )
		    {
			    document.getElementById("editorBox").style.left=x-310;
		        document.getElementById("editorBox").style.top=y-200;
		    }	
		    else if (y > 551)
		    {
			    document.getElementById("editorBox").style.left=x-310;
		      document.getElementById("editorBox").style.top=y-230;
		    }
			    d=document.getElementById("editorBox").id;
			    if(document.getElementById(d).offsetTop<document.body.scrollTop)
			    {
                   window.scrollBy(0,sy);
                }
                if(document.getElementById(d).offsetTop>=document.body.scrollTop)
               {
                document.body.scrollTop=document.getElementById(d).offsetTop;
                 window.scrollBy(0,-sy);
               }
			}
		document.getElementById("editorBox").style.zIndex=1000;    
	document.getElementById("editorBox").style.display = "";
	document.getElementById("editorheader").className = "editorheader";
	document.getElementById("editorBox").style.borderColor = "#2B5B73";
			document.getElementById("editorcontents").style.marginLeft="5px";
		document.getElementById("editorcontents").style.marginRight="5px";
		 
	document.getElementById("editorBox").style.lineHeight='25.3px';
	
	//document.getElementById("txtSearchEntities").focus();
}

function editorBoxItem()
{	
	document.getElementById("editorBoxItem").style.width =370 ;//290
	document.getElementById("editorheader").style.width =370;   //290;
	document.getElementById("editorclose").style.left = 360;
	document.getElementById("editorcontents").style.width =360;   //290;	
	document.getElementById("editorclose").style.top = 1;
	document.getElementById("editorBoxItem").style.top = document.documentElement.scrollTop;	
	document.getElementById("editorBoxItem").style.display = "";
	document.getElementById("editorheader").className = "editorheader";
	document.getElementById("editorBoxItem").style.borderColor = "#2B5B73";
			document.getElementById("editorcontents").style.marginLeft="5px";
		document.getElementById("editorcontents").style.marginRight="5px";
		 
	document.getElementById("editorBoxItem").style.lineHeight='25.3px';
	
	//document.getElementById("txtSearchEntities").focus();
}
function showLevelCtrlItem(mode)
{

  if(mode == 'show')
  {    
    editorBoxItem();
  }
  else if(mode == 'hide')
  {
    //document.getElementById("levelCtrl").style.display = "none";
   document.getElementById("editorBoxItem").style.display = 'none';
  } 
  //return false;
}

function closeEditorBox()
{
  document.getElementById("editorBox").style.display = 'none';
}
function showforhide()
{
 if (document.getElementById("editorBox"))
   {
      document.getElementById("editorBox").style.display = 'none';
      
    }  
}

//Added By Girish ---------------------Start

function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

// Global object to hold drag information.
var myDragObj;
var dragObj = new Object();
dragObj.zIndex = 0;

var latest_zIndex;

function dragStart_float(event, obj) {
  
  var el;
  var x, y;
  var id;
  
  id = obj.id;
    
  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id)
  {
    dragObj.elNode = document.getElementById(id);    
  }
  else 
  {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

// Update element's z-index.
//dragObj.elNode.style.zIndex = ++dragObj.zIndex;
  dragObj.elNode.style.zIndex = 100000;
  
//alert(dragObj.elNode.style.zIndex);
// Capture mousemove and mouseup events on the page.

  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo_float);
    document.attachEvent("onmouseup",   dragStop_float);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo_float,   true);
    document.addEventListener("mouseup",   dragStop_float, true);
    event.preventDefault();
  }
}

function dragGo_float(event) {

  var x, y;
  
  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Move drag element by the same amount the cursor has moved.
	//alert(dragObj.elNode.id);
  
  //dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  //dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";
  
  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX);
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY);
  
  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop_float(event) 
{
	// Stop capturing mousemove and mouseup events.
	isMouseDown = false;
	
	if(dragObj.elNode.id == "div_tree")
	{
		if(eval(dragObj.elNode.style.left.substr(0,dragObj.elNode.style.left.length-2)) < 175)
		{
			dragObj.elNode.style.left = 11;
			dragObj.elNode.style.top = 1;
			document.getElementById("td_workflow_canvas").width = "76%";
		}
		else
		{
			document.getElementById("td_workflow_canvas").width = "100%";
		}
	}
	
	if (browser.isIE) 
	{
		document.detachEvent("onmousemove", dragGo_float);
		document.detachEvent("onmouseup",   dragStop_float);
	}
	if (browser.isNS) 
	{
		document.removeEventListener("mousemove", dragGo_float,   true);
		document.removeEventListener("mouseup",   dragStop_float, true);
	}
}






//input only decimal/numeric data

//input only decimal/numeric data

function input_filterAmt (str, dec, bNeg) 
{ // auto-correct input - force numeric data based on params. 
 var cDec = '.'; // decimal point symbol 
 var bDec = false; var val = ""; 
 var strf = ""; var neg = ""; var i = 0; 

 if (str == "") return parseFloat ("0").toFixed (dec);  

/*
  if (str == "0" || str == "0.0" || str==0)
  {
  alert ("Please Enter value!!!")
  return parseFloat ("0").toFixed (dec); 
  
  }*/
   
 if (bNeg && str.charAt (i) == '-') { neg = '-'; i++; } 

 for (i; i < str.length; i++) 
 { 
  val = str.charAt (i); 
  if (val == cDec) 
  { 
   if (!bDec) { strf += val; bDec = true; } 
  } 
  else if (val >= '0' && val <= '9') 
   strf += val; 
 } 
 strf = (strf == "" ? 0 :  strf); 
 //strf=strf.substr(1,strf.length-1));
 return parseFloat (strf).toFixed (dec); 
} 


//Check & uncheck checkboxes
function check_uncheck (Val) 
        {     
        var ValChecked = Val.checked;
        var ValId =Val.id;
        var frm = document.forms[0];
        // Loop through all elements
        for (i=0; i<frm.length; i++)  
        {
        // Look for Header Template's Checkbox
        if (this!=null)
        {   if (ValId.indexOf ('CheckAll') != -1) 
            {
                // Check if main checkbox is checked, then select or deselect datagrid checkboxes 
               if(ValChecked) 
                {
                    if(frm.elements[i].disabled == false)
                        frm.elements[i].checked = true;
                }
                else frm.elements[i].checked = false;
            } 
             else if (ValId.indexOf ('deleteRec') != -1) 
            {
                // Check if any of the checkboxes are not checked, and then uncheck top select all checkbox
                if(frm.elements[i].checked == false)   frm.elements[1].checked = false;  }       
        }// if
        }// for
        }// function
    
    
    //Campare two dates
    function compareDate(Date1,Date2)
    {

	    ar_Date1 = Date1.split("/");
	    ar_Date2 = Date2.split("/");
    		
	    if (parseInt(ar_Date1[2],10) < parseInt(ar_Date2[2],10))
	    {
		    return -1;
    		
	    }
	    else if (parseInt(ar_Date1[2],10) > parseInt(ar_Date2[2],10))
	    {
		    return 1;
	    }
	    else if (parseInt(ar_Date1[2],10) == parseInt(ar_Date2[2],10))
	    {
		    if (parseInt(ar_Date1[1],10) < parseInt(ar_Date2[1],10))
		    {
			    return -1;
		    }
		    else if (parseInt(ar_Date1[1],10) > parseInt(ar_Date2[1],10))
		    {
			    return 1;
		    }
		    else if (parseInt(ar_Date1[1],10) == parseInt(ar_Date2[1],10))
		    {
			    if (parseInt(ar_Date1[0],10) < parseInt(ar_Date2[0],10))
			    {
				    return -1;
			    }
			    else if (parseInt(ar_Date1[0],10) > parseInt(ar_Date2[0],10))
			    {
				    return 1;
			    }
			    else if (parseInt(ar_Date1[0],10) == parseInt(ar_Date2[0],10))
			    {
				    return 0;
			    }		
    		
		    }		
	    }
    	
    }
    

//to get proper chars input 



    function KeyPress(obj)
{  

    var flag=checkChars(obj);
       if  (flag==false)
       {
        obj.focus();
              return false;
               
       }
       
      return true;
          
      
          
}

//to start data with alpha
               
  function Validate(obj)
{ 

  
        var asciiValue =obj.value.charCodeAt(0);
                       
  if (asciiValue >= 48  && asciiValue <= 57 )
  {
    
    alert("Enter Text Only/Don't Start with Number");
    obj.value='';
        obj.focus();
    return false;
		             
 }	 
		            
  if (obj.value=="" || asciiValue==95 ||  asciiValue==32 || obj.value.length==0)
  {
    alert("Enter Text Only/Don't Start with Space or _");
    obj.value='';
        obj.focus();
    return false;
		            
    }

    
   //obj.value=obj.value.replace(/^\s+|\s+$/g,"");
  var flag=checkChars(obj);
   if  (flag==false)
    {
      obj.focus();
      return false;
                           
    }    
       obj.value=obj.value.substring(0,1).toUpperCase() + obj.value.substring(1,obj.value.length);
      return true;  
                   
    }
    

  function KeyPressEnter()
{  

if (window.event.keyCode == 13 || window.event.keyCode == 9 )
{
 event.returnValue = false;
}

// return !(window.event && window.event.keyCode == 13); 
// if(window.event.keyCode == 13)
//    {   
//    alert("DF")
////    obj.value = input_filterAmt (obj.value, 2, 1);
////    obj.focus();
////        event.returnValue = false;
////        
//          return false;
//      //  document.getElementById("ctl00_CPHAdmin_btnSearch").click();
//       //
//    }        
}


/*KeyPresstab not in used*/

function KeyPresstab(obj)
{  
  
    var flag=checkChars(obj);
       if  (flag==false)
       {
        obj.focus();
     //   event.returnValue = false;
        return false;
       }
        //obj.focus();
      return true;
          
      //  document.getElementById("ctl00_CPHAdmin_btnSearch").click();
       //
           
}

     function checkChars(obj)
  {
    // validation fails if the input is blank
      obj.value= obj.value.replace(/^\s+|\s+$/g,"");
       
       if(obj.value.length*1 == 0) {
      //alert("Please enter valid characters!");
      obj.value='';
      //obj.focus();
      return false;
    }
     if(obj.value.substring(0,1) == '_') {
      //alert("Please enter valid characters!");
      obj.value='';
      obj.focus();
      return false;
    }
     if(obj.value.substring(0,1) == '') {
     //alert("Error: Input contains invalid characters!");
     obj.value='';
      //obj.focus();
      return false;
    } 
//for whitespace

    var re = /^[0-9A-Za-z@".'&\s\t]+$/;
    // var regEx =   /[^0-9A-Za-z]+/g+;
    var regEx =   /[^0-9A-Za-z@".'&\s\t]+/g;
    // validation fails if the input doesn't match the regular expression
    if(!re.test(obj.value)) 
    {
       obj.value=obj.value.replace(regEx,'');
     // obj.value= obj.value.replace(/^\s+|\s+$/g,"");//To trim string
      obj.title = '';
      alert("Please enter valid characters!");
       obj.focus();
           return false;
    }   
    // validation was successful
    //obj.focus();
        // obj.focus();
   return true;
  }
  
  
  //to check value more than 0   ***
  
  function  valueValidate(sender, args)

{


var iInt=parseInt(args.Value);
if (iInt * 1  > 0)
{
      args.IsValid = true;
         return;

}

else
{
      args.IsValid = false;
         return;
}

}

//for error msg with control focus on error
function AbortEntry(sMsg, Obj)
 {
	alert(sMsg);
      Obj.focus();	 
	return false;
 }
 
 //trim function--start
 
 String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
 //trim function--stop

function capitalizeMe(obj) {
        val = obj.value;
        newVal = '';
        val = val.split(' ');
        for(var c=0; c < val.length; c++) {
                newVal += val[c].substring(0,1).toUpperCase() +
val[c].substring(1,val[c].length) + ' ';
        }
        //obj.value = newVal;
        return  newVal;
}
/*

String.prototype.selectRange = function (iStart, iLength) {
    if (this.textbox.createTextRange) {
        var oRange = this.textbox.createTextRange(); 
        oRange.moveStart("character", iStart); 
        oRange.moveEnd("character", iLength - this.textbox.value.length); 
        oRange.select();
    } else if (this.textbox.setSelectionRange) {
        this.textbox.setSelectionRange(iStart, iLength);
    } 

    this.textbox.focus(); 
};
*/

//for text area maxlength  str=obj

function ValidateMaxLength(obj, maxLength)
 {
   var str=obj.value.trim();
 //alert(obj.value.length);
   var evntKeyCode = window.event.keyCode;
  // Ignore keys such as Delete, Backspace, Shift, Ctrl, Alt, Insert, Delete, Home, End, Page Up, Page Down and arrow keys
    var escChars = ",8,17,18,19,33,34,35,36,37,38,39,40,45,46,";
    if (escChars.indexOf(',' + evntKeyCode + ',') == -1) 
    {
         if (str.length > maxLength)
          {
            alert("You cannot enter more than " + maxLength + " characters.");
     
              if (obj.createTextRange) {
                  var oRange = obj.createTextRange(); 
                  oRange.moveStart("character", maxLength); 
               // oRange.moveEnd("character", 10 - obj.value.length); 
                  oRange.moveEnd("character",  obj.value.length);
                  oRange.select();
              } else if (obj.setSelectionRange) {
                  obj.setSelectionRange(maxLength, obj.value.length);
              } 
   
            obj.focus();
            return false;
          }
     }
    return true;
}

//for integer

 function checkNumber(obj,bDec)
  { 

    obj.value= obj.value.replace(/^\s+|\s+$/g,"");//To trim string
    
    if (obj.value.length==0 )
    {
    obj.focus();
    return false;
    }
     if(obj.value.substring(0,1) == '0')
      {
       if (obj.value*1==0)
       {
          obj.value='';
       }
       else
       {
          obj.value=obj.value.substring(1,obj.value.length);
       } 
          obj.focus();
          return false;
    }
    if(obj.value.substring(0,1) == '.')
      {       
        obj.value="";  
        obj.focus();
        return false;
    }
    
    
      if (obj.value.split(".").length-1 > 1 ) 
          {
                alert ("Please enter Dot not more than one  !!");
                obj.value=obj.value.substring(0,obj.value.length-1);               
                obj.focus(); 
                return false
            }
    
    
       var regEx,re ; 
      // regular expression to match numeric characters and spaces
   if ( bDec*1==0 )
   {
    regEx =  /^[0-9\s\t]+$/;
    re=/[^0-9\s\t]+/g;

    }
    else
    {
      regEx =   /^[.0-9\s\t]+$/;
          re=/[^.0-9\s\t]+/g;
             // obj.value=parseFloat(obj.value).toFixed(2);
    }
    // validation fails if the input doesn't match the regular expression
    if(!regEx.test(obj.value)) 
    {    
       // var regEx =   /[^0-9\s]+/g;
       obj.value=obj.value.replace(re,'');     
       obj.title = '';
       alert("Please enter valid Digits !!!");
        obj.focus();
           return false;
    }
    
    
    // validation was successful
    return true;
  }
  


function ValidateForm(obj){
	
/*	if (isDate(obj.value)==false)
	{
	 // alert("The date should be  in the  dd/mm/yyyy formate.");
	    obj.value='';
		obj.focus()
		return false
	}
    return true*/
  /*  
    var RegExPattern = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
    var errorMessage = 'Please enter valid date as month, day, and four digit year.\nYou may use a slash, hyphen or period to separate the values.\nThe date must be a real date. 30/2/2000 would not be accepted.\nFormay dd/mm/yyyy.';
        if ((obj.value.match(RegExPattern)) && (obj.value!='')) {
     return true;
    } else {
        alert(errorMessage);
        obj.focus();
        return false;
    } 
    */
     alert('Please Select date from Calender only');
         obj.value='';
       obj.focus();
       return false;
 }


function LimitAttach(form,file,foc) 
{

	extArray = new Array(".gif",".jpg",".jpeg",".doc",".pdf",".rtf",".bmp");
	allowSubmit = false;

	if (!file) return;
	while (file.indexOf("\\") != -1)	
		file = file.slice(file.indexOf("\\") + 1);
		ext = file.slice(file.indexOf(".")).toLowerCase();
		for (var i = 0; i < extArray.length; i++) 
			{
				if (extArray[i] == ext) { allowSubmit = true; break; }
			}
			//if (allowSubmit) form.submit();
		if (allowSubmit)
		{
	//	document.getElementById('hdnimg').value=document.getElementById('fileUpload').value;

			return true;
			}
		else
		{
			alert("Please only upload files that end in types:  " 
			+ (extArray.join("  ")) + "\nPlease select a new "
			+ "file to upload and submit again.");

//   	   var who=document.getElementsByName('fileAttachment')[0];
//       who.value="";
//       var who2= who.cloneNode(false);
//       who2.onchange= who.onchange;
//       who.parentNode.replaceChild(who2,who);      
			foc.select();
	foc.focus();
		 return false;
	}
       
}   


//Added By Girish ---------------------Stop


















