﻿// JScript File

function OnFocus_SelectText(ctl){
    var control = document.getElementById(ctl);
    control.select();
}

//add option to given control
function AddOption(opText,opValue,ctl){
    var op = new Option();
    op.text = opText;
    op.value = opValue;
    ctl.options[ctl.length] = op;
    return false;
}
////remove option to given control
//function RemoveOption(opText,opValue,ctl){
//    var op = new Option();
//    op.text = opText;
//    op.value = opValue;
//    ctl.options[ctl.length] = op;
//    return false;
//}

//remove all rows from table
function RemoveAllRows(ctl){
    for(var x = ctl.rows.length - 1;x >= 0;x--){
        ctl.deleteRow(x);
    }
}

//clear select element of all options
function RemoveAll(ctl){
    for(var x = ctl.options.length - 1;x >= 0;x--){
        ctl.remove(x);
    }
}
//clear select element of all options
function RemoveAllTo(ctl,last){
    for(var x = ctl.options.length - 1;x >= last;x--){
        ctl.remove(x);
    }
}

//set selected text
function selectedText(obj,setText){
    obj.selectedIndex = 0;
    for(var x = 0; x < obj.length; x++){
        if (obj.options[x].text == setText){
            obj.selectedIndex = x;
            break;
        }
    }
}
//set selected value
function selectedValue(obj,setValue){
    obj.selectedIndex = 0;
    for(var x = 0; x < obj.length; x++){
        if (obj.options[x].value == setValue){
            obj.selectedIndex = x;
            break;
        }
    }
}
function findTextIndex(obj,selText){
    var fnd = -1;
    for(var x = 0; x < obj.length; x++){
        if (obj.options[x].text == selText){
            fnd = x;
            break;
        }
    }
    return fnd;
}
function findValueIndex(obj,selValue){
    var fnd = -1;
    for(var x = 0; x < obj.length; x++){
        if (obj.options[x].value == selValue){
            fnd = x;
            break;
        }
    }
    return fnd;
}

function GetObj(ctl){
    var rtnctl = GetControl(ctl);
    if (rtnctl){
        return rtnctl;
    }
    else{
        alert(ctl + ' Not Found! Contact Computer!');
        return false;
    }
}
function GetObjID(ctl){
    var rtnctl = GetClientID(ctl);
    if (rtnctl){
        return rtnctl;
    }
    else{
        alert(ctl + ' Not Found! Contact Computer!');
        return false;
    }
}
function GetObjNoError(ctl){
    return GetControl(ctl);
}
function CheckRegExp(type,value){
    var regexpTest;
    switch(type){
        case 'multemail':
            regexpTest = /^(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*([,;]*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*)*$/;
            break;
        case 'email':
            regexpTest = /^(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\b)*$/;
            break;
        case 'phone':
            regexpTest = /\d{10}/;
            break;
        case 'birthday':
        case 'date':
            regexpTest = /([0]?[1-9]|[1][0-2])[./-]([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0-9]{4})/;
            break;
    }
    if(value > ' '){
        return regexpTest.test(value);
    }
    else return true;
    
}

//return is nothing.  causes alerts
function CheckObjValue(type,value,obj){
    var valid = new Boolean(CheckRegExp(type,value));
    if (!(valid.valueOf())){
        obj.focus();
        alert('Please enter a valid value!');
        switch(type){
            case 'birthday':
            case 'date':
                alert('Valid date example: 3/14/1879');  //Einstein's birthday
                break;
            case 'phone':
                alert('Please enter number with area code, no special characters.');
                alert('Example: 6074253151');
                break;
            case 'email':
                alert('Example: youremail@yahoo.com');
                break;
            case 'multemail':
                alert('Address must by comma or semicolon delimited with no spaces!');
                alert('Example: youremail@yahoo.com;theiremail@yahoo.com');
                break;
        }
    }
}
function roundNumber(num,dec){
    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    return result;
}
function IsNumeric(value){
    var temp = new String(value);
    var ans = new Boolean(true);
    for(i=0;i<temp.length;i++){
        if (temp.charCodeAt(i) < 48 || temp.charCodeAt(i) > 57){
            ans = false;
            break;
        }
    }
    return ans;
}
function RequirePosNumeric(obj){
    if(!(IsNumeric(obj.value)) || obj.value <= 0){
        alert('Please provide a valid quantitiy!');
        obj.focus();
    }
}
function IsDouble(value){
    //46 => decimal point
    //45 => negative sign
    var temp = new String(value);
    var ans = new Boolean(true);
    if (ans.valueOf() == true){
        var deccnt = new Number(0);
        var negcnt = new Number(0);
        for(i=0;i<temp.length;i++){
            if ((temp.charCodeAt(i) < 48 || temp.charCodeAt(i) > 57) && temp.charCodeAt(i) != 46 && temp.charCodeAt(i) != 45){
                ans = false;
                break;
            }
            else{
                if (temp.charCodeAt(i) == 46){
                    deccnt++;
                    if (deccnt > 1){
                        ans = false;
                        break;
                    }
                }
                if (temp.charCodeAt(i) == 45){
                    negcnt++;
                    if (negcnt > 1){
                        ans = false;
                        break;
                    }
                }
            }
        }        
    }
    return ans;
}
function IsMoney(value){
    var temp = new String(value);
    var ans = new Boolean(true);
    if (ans.valueOf() == true){
        var deccnt = new Number(0);
        var dolcnt = new Number(0);
        for(i=0;i<temp.length;i++){
            if ((temp.charCodeAt(i) < 48 || temp.charCodeAt(i) > 57) && temp.charCodeAt(i) != 46 && temp.charCodeAt(i) != 36){
                ans = false;
                break;
            }
            else{
                //decimal
                if (temp.charCodeAt(i) == 46){
                    deccnt++;
                    if (deccnt > 1){
                        ans = false;
                        break;
                    }
                }
                //dollar sign
                if (temp.charCodeAt(i) == 36){
                    dolcnt++;
                    if (dolcnt > 1){
                        ans = false;
                        break;
                    }
                }
            }
        }        
    }
    return ans;
}    
function ClosePopup(popup){
    //this is the BehaviorID of the mpe
    var mpe = $find(popup);
    if (mpe){
        mpe.hide();
    }
}
function CheckPrice(price)
{
    var numaric = price;
    var deccnt = 0;
    for(var j=0; j<numaric.length; j++)
    {
        var alphaa = numaric.charAt(j);
        var hh = alphaa.charCodeAt(0);
        //46 == .
        //36 == $
        if((hh > 47 && hh<58) || (hh == 46) || (hh == 36))//(hh > 64 && hh<91) || (hh > 96 && hh<123))
        {
            if (hh == 46) deccnt++;
            if (deccnt > 1) return false;
        }
        else{
            return false;
        }
    }
    return true;
}
function CheckQty(qty)
{
    var numaric = qty;
    for(var j=0; j<numaric.length; j++)
    {
        var alphaa = numaric.charAt(j);
        var hh = alphaa.charCodeAt(0);
        if((hh > 47 && hh<58))//(hh > 64 && hh<91) || (hh > 96 && hh<123))
        {
        }
        else{
            return false;
        }
    }
    return true;
}
function setEditPopupPanelSizeC(ctl)
{
    if (ctl)
    {
        if (document.documentElement.offsetHeight > 750)
        {
            ctl.style.height = 600 + 'px';
        }
        else
        {
            var hgt = document.documentElement.offsetHeight * 1;
            if (document.documentElement.offsetHeight <= 750 && document.documentElement.offsetHeight >= 700){
                hgt = 630;
            }
            ctl.style.height = hgt - 50 + 'px';
        }
    }
}
function setEditPopupPanelSize(ctl)
{
    if (ctl)
    {
        if (document.documentElement.offsetHeight > 750)
        {
                ctl.style.height = 600 + 'px';
        }
        else
        {
            var controlName = new String();
            controlName = ctl.id;
            var hgt = document.documentElement.offsetHeight * 1;
            if (document.documentElement.offsetHeight <= 750 && document.documentElement.offsetHeight >= 700){
                hgt = 630;
            }
            if (controlName.indexOf('pnlTask',0) > -1)
            {
            ctl.style.height = (hgt - 10) + 'px';
            }
            else{
            ctl.style.height = (hgt - 50) + 'px';
            }
        }
    }
}  



function AddDataToCol(col,obj){
    col.appendChild(obj);
}

function AddColToRow(row,col){
    row.appendChild(col);
}

function AddRowToTable(tbl,row){
    tbl.appendChild(row);
}

function RequiredFieldCheck(value){
    var testStr = new String(value);
    if(testStr.length <= 0) return false;
    else return true;
}

function GetDocumentDepth(){
    var path = document.location.pathname;
//    alert(path);
    var slashCnt;
    var str;
    str = '';
    var m = path.match(/[\/]/g);
    
    
    if(m){
        slashCnt = m.length - 2;
    }
    else{
        slashCnt = 0;
    }
    
    for(var i=0;i<slashCnt;i++){
        str += '../';
    }
    return str;
}

function CalcFilePathDepth(nextPgDepth){
    var path = document.location.pathname;
//    alert(path);
    var slashCnt;
    var str;
    str = '';
    var m = path.match(/[\/]/g);
    
    
    if(m){
        slashCnt = m.length - 2;
    }
    else{
        slashCnt = 0;
    }
    
    if(slashCnt <= nextPgDepth){
        slashCnt = 0;
    }
    else{
        slashCnt = slashCnt - (slashCnt - nextPgDepth);
    }
    
//    alert(slashCnt);
    for(var i=0;i<slashCnt;i++){
        str += '../';
    }
    return str;
}
function OnClick_RedirectSearchEdit(){
    var path = CalcFilePathDepth(1) + 'SearchEdit.aspx';
//    alert(path);
    window.setTimeout('window.location="' +  path + '";',10);
}
function OnClick_RedirectQuoteMain(){
    var path = CalcFilePathDepth(2) + 'QuoteMain.aspx';
//    alert(path);
    window.setTimeout('window.location="' +  path + '";',10);
}
function OnClick_ToSearchEdit(){
    var path = '../CRM/SearchEdit.aspx';
//    alert(path);
    window.setTimeout('window.location="' +  path + '";',10);
}

function findPos(obj){
    var curleft = curtop = 0;
    
    if (obj.offsetParent){
        curleft = obj.offsetLeft;
        curtop = obj.offsetTop;
        while (obj = obj.offsetParent){
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
    }
//    alert(curleft + " " + curtop);
    return [curleft,curtop];
}

function FillCascDropDown(behaviorID){
//    GetObj('ddlPUCat1').selectedIndex = GetObj('ddlSrchCat1').selectedIndex;
//    GetObj('ddlPUCat1').style.display = GetObj('ddlSrchCat1').style.display;
    var cdd2 = $find(behaviorID);
    cdd2._onParentChange(null,false);
    cdd2.add_populated(CDDPopulate);
}
function CDDPopulate(sender,e){
    var senderid = new String(sender.get_ParentControlID());
//    switch(senderid.substr(senderid.length-1,1)){
//        case '1':
//            var cdd2 = $find('jsCDDPU2');
//            if (cdd2._isPopulated() == true){
//                GetObj('ddlSrchCat2').style.display = '';
//                GetObj('ddlPUCat2').style.display = GetObj('ddlSrchCat2').style.display;
//            }
//            else{
//                GetObj('ddlSrchCat2').style.display = 'none';
//                GetObj('ddlPUCat2').style.display = GetObj('ddlSrchCat2').style.display;
//            }
//            break;
//        case '2':
//            var cdd3 = $find('jsCDDPU3');
//            if (cdd3._isPopulated() == true){
//                GetObj('ddlSrchCat3').style.display = '';
//                GetObj('ddlPUCat3').style.display = GetObj('ddlSrchCat3').style.display;
//            }
//            else{
//                GetObj('ddlSrchCat3').style.display = 'none';
//                GetObj('ddlPUCat3').style.display = GetObj('ddlSrchCat3').style.display;
//            }
//            break;
//    }
    sender.remove_populated(CDDPopulate);
}

function pauseComp(milli){
    var date = new Date();
    var curDate = null;
    do{
        curDate = new Date();
    }
    while(curDate-date < milli);
}

function BlindHideShow(ctlId,imgId,durationTime){
    if($(ctlId).style.display == 'none'){
        Effect.BlindDown(ctlId,{duration:durationTime});
        $(imgId).src = '../CRM/CRM_images/collapse.jpg';
    }
    else{
        Effect.BlindUp(ctlId,{duration:durationTime});
        $(imgId).src = '../CRM/CRM_images/expand.jpg';
    }
}

function CheckDateVsToday(ctl){
    if(ctl.value <= ' ')return 0;
    if(!(isDate(ctl.value))){
//        alert('Please provide a valid date in the format: MM/DD/YYYY');
        ctl.focus();
        return;
    }
    var today = new Date();
    var thisYr = today.getFullYear();
    var thisMnth = new String(today.getMonth()+1);
    thisMnth = (thisMnth.length == 1)? '0' + thisMnth : thisMnth;
    var thisDay = new String(today.getDate());
    thisDay = (thisDay.length == 1)? '0' + thisDay : thisDay;
    
    var myDate = ctl.value;//.replace(/[\/]/g,'');
    if (myDate.indexOf('/') == 1) myDate = '0' + myDate;
    myDate = myDate.replace(/[\/]/g,'');
    if(myDate.length == 7){
        //day short 1 char
        myDate = myDate.substr(0,2) + '0' + myDate.substr(2,5);
    }
    myDate = myDate.substr((myDate.length)-4, 4) + myDate.substr(0,(myDate.length) - 4);
    
    alert(myDate);
    alert((thisYr + thisMnth + thisDay));
    alert(myDate == (thisYr + thisMnth + thisDay));
    alert(myDate > (thisYr + thisMnth + thisDay));
    alert(myDate < (thisYr + thisMnth + thisDay));
    
    if(myDate == (thisYr + thisMnth + thisDay)){
        return 0;
    }
    else if(myDate < (thisYr + thisMnth + thisDay)){
        return -1;
    }
    else if(myDate > (thisYr + thisMnth + thisDay)){
        return 1;
    }   
    
}

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}
		if (i==2) {this[i] = 29;}
   } 
   return this;
}

function isDate(dtStr){
    if(dtStr <= ' ')return true;
    
    var today = new Date();
//    minYear = today.getFullYear() - 200;
    maxYear = today.getFullYear() + 200;
    
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date");
		return false;
	}
return true;
}

////function ValidateForm(){
////	var dt=document.frmSample.txtDate
////	if (isDate(dt.value)==false){
////		dt.focus()
////		return false
////	}
////    return true
//// }


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+$/,"");
}

function ShowWaitOnly(msg){
    new Popup('divWaitContainer',null,{modal:true,duration:0});
    $('waitMsg').innerText = msg;
    $('divWaitContainer').popup.show();
}

function HideWaitOnly(){
    $('divWaitContainer').popup.hide();
}

//new using JavaScript/AjaxScripts/modalbox.js
function ShowWait(show,msg){
    if(show){
        $('waitMsg').innerText = msg;
        Modalbox.show($('divWaitContainer'),{title: this.title,width:650,height:100,
                    slideDownDuration:0,slideUpDuration:0,vCenter:true,hideFrame:true});
    }
    else{
        Modalbox.hide();
    }
}

//original using JavaScript/AjaxScripts/popup.js
function ShowWait2(show,msg){
    if(show){
        new Popup('divWaitContainer',null,{modal:true,duration:0});
        $('waitMsg').innerText = msg;
        $('divWaitContainer').popup.show();
    }
    else{
        $('divWaitContainer').popup.hide();
    }
    RegisterOnMouseWheel(show);
}

//original using JavaScript/AjaxScripts/popup.js
function ShowWaitMsg(show,msg){
    if(show){
        new Popup('divWaitContainer',null,{modal:true,duration:0});
        $('waitMsg').innerText = msg;
        $('divWaitContainer').popup.show();
    }
    else{
        $('divWaitContainer').popup.hide();
    }
    RegisterOnMouseWheel(show);
}

//register scroll event for wait popup
function RegisterOnMouseWheel(doIt){
    if(doIt){
        var str = new String(document.documentElement.scrollTop);
        scrollPos = str.replace('px','');
        window.onmousewheel = document.onmousewheel = onScroll;
    }
    else{
        window.onmousewheel = document.onmousewheel = '';
    }
}
//move modal wait popup if user scrolls
function onScroll(event){
    var str = new String(document.documentElement.scrollTop);
    var newPos = new Number(str.replace('px',''));
    var str2 = new String($('divWaitContainer').style.top);
    var puPos = new Number(str2.replace('px',''));
    $('divWaitContainer').style.top = (puPos + (newPos - scrollPos)) + 'px';
    scrollPos = newPos;
}

function ChangeMsg(ctl,msg){
    if(ctl){ 
        ctl.innerText = msg;
    }
}

function WSOnError(result){
    alert("Error: " + result.get_message());
}

//displays Help popup
function DisplayHelp(puId){
    var T_width = 500;
    var options =
    {
     title:$(puId).title,
     width:T_width,
     vCenter:false,
     draggable: false
    };
    Modalbox.show($(puId),options);
}



//------------------------------------------------------------------------------------------------//
//                                  Customer information
var AccntInfoIdx = {agentNum:0,agentName:1,credit:2,balance:3,hiact:4,available:5,textCredit:6,textBalance:7,textAvail:8};

function GetCustAcctInfo(custnum,onComplete){
    CRMService.GetCustomerAccntInfo(custnum,onComplete);
}

//------------------------------------------------------------------------------------------------//


//------------------------------------------------------------------------------------------------//
//                                  Product information
function FormatCategoryURL(cat1,cat2){
    var urlStr = 'kv=';
    if(cat1 > ' ' && cat2 <= ' '){
        urlStr += 'Category1:' + cat1 + ';&cat=Category2';
    }
    else if(cat1 > ' ' && cat2 > ' '){
        urlStr += 'Category1:' + cat1 + ';' + 'Category2:' + cat2 + ';&cat=Category3';
    }
    
    return urlStr
}

//------------------------------------------------------------------------------------------------//

////////////////////////-----------------------------------------Tab Navigation-----------------------------------------//
//////////////////////function Tab_onClick(tab,bodyID){
//////////////////////    var tabParent = tab.parentElement;
//////////////////////    var tabBodyIdx = 0;
//////////////////////    for(var x = 0; x < tabParent.childNodes.length; x++){
//////////////////////        var theTab = tabParent.childNodes[x];
//////////////////////        theTab.className = '';
////////////////////////        if(theTab == tab){
////////////////////////            tabBodyIdx = x + tabParent.childNodes.length/2;
////////////////////////        }
//////////////////////    }
//////////////////////    tab.className = 'Active';
////////////////////////    tabParent.childNodes[tabBodyIdx].className = 'Active';
//////////////////////    $(bodyID).style.display = '';
//////////////////////}

////////////////////////------------------------------------------------------------------------------------------------//

function BuildSheetIn(pageDepth,containerID,sheetNum,viewType,onlyBought){
    var view;
    switch(viewType.toLowerCase()){
        case 'emp':
            view = '122';
            break;
        case 'admin':
            view = '61';
            break;
        case 'cust':
            view = '0';
            break;
        default:
            $(containerID).innerText = 'Unhandled error!  Try again later!';
            return;
            break;
    }
    var bo = 0;
    if(onlyBought){
        bo = 1;
    }
    if(sheetNum <= ' '){
        $(containerID).innerText = 'Unable to find your price sheet!  Please contact a Howard Lighting Representative.';
        return;
    }
    $(containerID).innerHTML = '<font color=red><img src="' + pageDepth + 'Images/indicator.gif" />&nbsp;Building Sheet.  Please wait...</font>';
    var pageAddr = pageDepth + 'Pricing/PriceSheet/Sheet.aspx?ps=' + sheetNum + '&v=' + view + '&bo=' + bo;
    new Ajax.Updater(containerID,pageAddr,{asynchronous:true});
}

function STephanie(){
    alert(';alksdf;lasdhf');
}


function CheckForTab(e){
    var keycode;
    if(window.event){
        keycode = window.event.keyCode;
    }
    else if(e){
        keycode = e.which;
    }
    if(keycode == 9) return true;
    else return false;
}

function MakeTextBox(ele,maxLen){
//    if(ele.childNodes[0].tagName != 'INPUT'){
//        var eleText = ele.innerText;
//        ele.innerText = '';
//        var obj = document.createElement('input');
//        obj.type = 'text';
//        obj.value = eleText;
//        obj.maxLength = maxLen;
//        obj.onblur = function(){RemoveText(ele);};
//        obj.onfocus = function(){obj.select();};
//        obj.onkeydown = function(){if(CheckForTab()){TabNext(obj);}};
//        ele.appendChild(obj);
//        ele.childNodes[0].focus();
//    }
    var cell = ele;
    var children = cell.childNodes;

	var hasBox = false;
	var theBox;
	for (var i = 0;i <= children.length-1;i++)
	{
		curChild = children[i];
		//alert(curChild.outerHTML);
		if(curChild.tagName=="INPUT")
		{
			hasBox = true;
			theBox = curChild;
			break;
		}
	}
	if(hasBox)
	{
        //do nothing
	}
	else
	{
		var txt = cell.innerText;
	
		var newBox = document.createElement("input");
		newBox.type = 'text';
		newBox.name="name";
////////		newBox.abbr = 'autogen';
		//newBox.setAttribute("value",txt.toString());
		newBox.value = txt;
	    
//		newBox.onkeydown = function(){TabNext(this.parentNode,false);}
        newBox.onblur = function(){RemoveTextBox(this.parentNode);};
        newBox.onfocus = function(){this.select();};
        newBox.style.width = '95%';
	
		cell.replaceChild(newBox,cell.firstChild);
		
		theBox = newBox;
	}
	var obj = theBox;
	if (obj != null){
		cell.focus();
		if (obj.select)
			obj.select();
	}
}
function RemoveTextBox(ele){
    var eleText = ele.childNodes[0].value;
    ele.removeChild(ele.childNodes[0]);
    ele.innerText = eleText;
}
function TabNext(ele,skipRemove){
alert('here');
    if(CheckForTab()){
//        if(!skipRemove)
//            RemoveTextBox(ele);
	    var cell = ele;
	    var row = cell.parentNode;
	    var nextCell;
    	
	    if(cell.nextSibling) //Has next cell
	    {
		    nextCell = cell.nextSibling;
	    }
	    else if(row.nextSibling) //Has next row
	    {
		    nextCell = row.nextSibling.firstChild;
//		    alert('next row');
	    }
	    else
	    {
		    nextCell = row.parentNode.rows[0].cells[0];
	    }
//	    alert(nextCell.onclick);
	    if(nextCell.onclick)
	        nextCell.click();
	    if(skipRemove){
//	    alert(nextCell + '   ' + nextCell.innerText);
    	    nextCell.focus();
    	}
    }
}




function ZeroMaxLen(ele){
    ele.value = '';
}