/* writes value of checkbox to hiddenfield which data then will be sent on server. */
function toggleHistoryId(id, container)
{    
    var hiddenfield = document.getElementById(container + "_hiddenIds");    
    var str = hiddenfield.value;
    
    var re = new RegExp("(^" + id + ";)|(;" + id + ";)");
    var res = re.exec(str);
    if (res==null || res["index"]<0)
    {
        hiddenfield.value+= id + ";";
    }
    else
    {
        re = new RegExp(id + ";");
        hiddenfield.value = str.replace(re, '')
    }
}
/*Simulates tab actions for tab searhcer on Browse Schools/Programs page*/
function changeClass(activeElementId,activeContainerId,notActiveElementId,notctiveContainerId){
            document.getElementById(activeElementId).className="ActiveTab";
            document.getElementById(notActiveElementId).className="NotActiveTab";
            
            document.getElementById(activeContainerId).style.display="inline";
            document.getElementById(notctiveContainerId).style.display="none";            
        }
/*Opens popup window with needed url*/
function OpenPopupWindow(url, H, W, name)
{
	var features =
        'width='        + W +
        ',height='      + H+
        ',directories=' + 0 +
        ',location='    + 0 +
        ',menubar='     + 0 +
        ',scrollbars='  + 0+
        ',status='      + 0 +
        ',toolbar='     + 0 +
        ',resizable='   + 0+
		',left='   		+ 0+
		',top='   		+ 0		
		;
	var wnd = window.open(url, 'popup',features);
	wnd.focus();
	return wnd;
}
/*Swaps rows of table*/
 function exchange(oRowI, oRowJ, oTable)
    {	
	    if(oRowI.rowIndex == oRowJ.rowIndex+1) {
		    oTable.insertBefore(oRowI, oRowJ);
	    } else if(oRowJ.rowIndex == oRowI.rowIndex+1) {
		    oTable.insertBefore(oRowJ, oRowI);
	    } else {
		    var tmpNode = oTable.replaceChild(oRowI, oRowJ);
		    if(typeof(oRowI) != "undefined") {
			    oTable.insertBefore(tmpNode, oRowI);
		    } else {
			    oTable.appendChild(tmpNode);
		    }
	    }
    }
/*Wraps spans with Div*/	
/*Used to simulate vertical tabing of Tab Control from MS AjaxControlToolkit library*/
/*Requires jquery-1.2.1.pack.js*/
  function wrapSpansWithLi()
  {
	  $('.ajax__tab_header > span').each(function() {
												 
												  $(this).wrap('<div></div>');
												  });
  }
  /*Validates on School Editor page is schoolName that user enters unique */
  var checkForSchoolName = function(e) {
	  
	  				var control = e.data.control;
				    var name = this.value;	
					var schoolId = e.data.schoolId;
					var parentId = e.data.parentId;
				    if (name.length <= 0) return;					
					PageMethods.IsSchoolUnique(name, schoolId, parentId, function(res) {
												  
												  if (res)
												  {
													  control.hide("fast");
												  }
												  else
												  {
													 control.show("fast"); 
												  }
												  });
					
				    };
/*Validates on Program Editor page is programName that user enters unique */
/*Requires jquery-1.2.1.pack.js*/
  var checkForProgramName = function(e) {
	  				var control = e.data.control;
					var progId = e.data.progId;
					var schoolId = e.data.schoolId;
				    var name = this.value;				    			    
				    if (name.length <= 0) return;
				    PageMethods.IsProgramUnique(name, progId, schoolId, function(res) {
												  
												  if (res)
												  {
													  control.hide("fast");
												  }
												  else
												  {
													 control.show("fast"); 
												  }
												  });
					
				    };
/*Disables scrolling for iframe in MS'AjaxTabPabel*/	
/*Requires jquery-1.2.1.pack.js*/			    
function disableScrollingForPanelWithFrame(){
    $('iframe').each(
        function(){
            if($(this).parent().get(0).className=="ajax__tab_panel"){
                $(this).parent().css("overflow","hidden");
            }
        }
   )
}
/*Makes element visible*/  
function showBlock(id){
    document.getElementById(id).style.display="block";
}
/*Makes element hidden*/  
function hideBlock(id){
    document.getElementById(id).style.display="none";
}
/*Used In MediaUploader Control*/
/*Requires jquery-1.2.1.pack.js*/
/*Hides custom error's on user click on modification of uploader contrik that are handled on Server.*/
function fileUploaderState(errMsgSpanId,fileUploadId,uploadBtnId) 
{             
    if($('#'+errMsgSpanId).get(0)!=null)                                    
    {
        $('#'+fileUploadId).bind("blur",function(){$('#'+errMsgSpanId).css("display","none")});                                            
        $('#'+uploadBtnId).bind("click",function(){$('#'+errMsgSpanId).css("display","none")});                                            
    }                                
}                                
/*Sets chars limit for textbox,initialize current char counter state*/
/*Requires jquery-1.2.1.pack.js*/
/*Requires fieldlength.js*/
function SetCharLength(txtbxId,counterId,length) {
     $('#'+txtbxId).textlimit(counterId,length);
}
/*Inserts word breaks for too much long words*/
/*Requires jquery-1.2.1.pack.js*/
/*Uses on change history elemens*/
function WordBreaker(context,maxWordLength)
{
    $('.WordBreak',context).each(
         function(){
            $(this).get(0).innerHTML=
                BreakLongWord($(this).text(),maxWordLength);
        }
    )
}
function BreakLongWord(text,maxWordLength)        
{
    var txtcnt=0;
    for(i=0;i<=text.length;i++)
    {
      txtcnt++;
      if(text.charAt(i)==" "||text.charAt(i)=="\n"||text.charAt(i)=="\t")
        {txtcnt=0;} 
      if(txtcnt>=maxWordLength)
      {
        text=text.substring(0,i)+"<wbr>&nbsp;"+text.substring(i,text.length);
        i+=11;
        txtcnt=0;
      }
    } 
    return text;
}
/*Inserts  "..." if the word in very long in the middle*/
/*Requires jquery-1.2.1.pack.js*/
function dotedInMiddleText(context,maxWordLength,charsBeforeAfterDots){
     $('.WordMiddleDots',context).each(
         function(){            
            $(this).text(
                ReplaceByDotsLongText($(this).text(),maxWordLength,charsBeforeAfterDots)
            );    
        }
    )
}
function ReplaceByDotsLongText(text,maxWordLength,charsBeforeAfterDots)        
{
    if(text.length>maxWordLength)
    {
        text=text.substring(0,charsBeforeAfterDots)+"..."+text.substring(text.length-charsBeforeAfterDots,text.length);
    } 
    return text;   
}
/*Sets frame  height on client side*/
/*Requires jquery-1.2.1.pack.js*/
function setClientHeight(frame)
{
    if(frame)
    {
        var doc;
        var frameHeigh=null;
        if(frame.contentWindow&&frame.contentWindow.document){
            doc=frame.contentWindow.document;
        }    
        else if(frame.contentDocument)
            doc=frame.contentDocument
        if(doc&&doc.body&&doc.body.clientHeight>0)
        {           
            if(!doc.seted){
                doc.seted=true;
                $(frame).css("height",doc.body.clientHeight+140);    
            } 
         }  
    }
}
/*closes current window*/
function close(){
    window.close();
}

/*

    if country dropdownlist selected country is "US"-hides block where state's textbox appears

    else hides country dropdownlist's block    

*/

function showStateTextbox(state,div,div2)

{        

    if (state!=null && state=="US")

    {

       div.style.display="none";

       var list=div2.getElementsByTagName('select')[0];

       var textBox=div.getElementsByTagName('input')[0];

       textBox.value=list.options[list.options.selectedIndex].value;

       div2.style.display="inline";

    }

    else

    {

        div.style.display="inline";

        div2.style.display="none";

    }    

 }

 

/*

if selected country isn't "US" then hides required field asterix for zip

*/

function showAsterix(state,span,span2)

{        

    if (state!=null && state=="US")

    {

      span.style.display="inline";

      span2.style.display="inline";

 

    }

    else

    {

      span.style.display="none";

      span2.style.display="none";

 

    }    

 }
 
 /*adds borders to change history table*/

function addBorderToChangeHistory(id,border){

    var table=document.getElementById(id);

    for(var i=0;i<table.rows.length;i++)

    {

        var rowspan=table.rows[i].cells[0].getAttribute('rowspan');

        if(rowspan&&parseInt(rowspan)>=1)

        {

            rowspan=parseInt(rowspan);

            var currentRowIndex=i;

            for(var j=2;j<table.rows[currentRowIndex].cells.length;j++)

            {

                table.rows[currentRowIndex].cells[j].style.borderTop=border;

            }

            currentRowIndex=rowspan+i-1;

            for(var j=0;j<table.rows[currentRowIndex].cells.length;j++)

            {

                table.rows[currentRowIndex].cells[j].style.borderBottom=border;

            }

            i=currentRowIndex;

        }

    }

}



