﻿/*
=============================================
 Copyright (C) 2008 Al Jazeera Network
 All rights reserved.
 Aljazeera Software unit
 Developer: Alsherbeny Ahmed 
=============================================
*/

function $(id) {
    return document.getElementById(id);
}
function $$(id) {
    return document.getElementsByName(id);
}

function MOver(Source, BesideItem, selected)
{
	if(!selected)
	{
		if(MainOpen == -1)
		{
		document.getElementById(BesideItem).style.backgroundColor = "#CEB469";
		document.getElementById(Source).style.color = "#b68809";
		}
	}
}

function MOut(LinkTD, NotchTDID, selected)
{
	if(!selected)
	{
		
		if(MainOpen == -1)
		{
		document.getElementById(NotchTDID.id).style.backgroundColor = "";
		LinkTD.style.color = "";
		}
	}
}

function SelectChannel(PostType , TextBoxCtrl )
{
    window.open('/Services/Templates/frmChannels.aspx?PostType=' + PostType  + '&TextCTrl=' + TextBoxCtrl ,'SelectPost' + PostType ,'menubar=0,resizable=1,width=830,height=490,scrollbars=1,status=1,top=200,left=50');
}

function SelectPosting(PostType , TextBoxCtrl )
{
    window.open('/Services/Templates/frmSelectPosting.aspx?PostType=' + PostType  + '&TextCTrl=' + TextBoxCtrl ,'SelectPost' + PostType ,'menubar=0,resizable=1,width=830,height=490,scrollbars=0,status=1,top=200,left=50');
}

function MaxCount(field,maxlimit) {
	var charcnt = field.value.length;        

	if (charcnt > maxlimit) { 
		field.value = field.value.substring(0, maxlimit);
	}
}
function textCounter(field,counter,maxlimit,linecounter) {
	var fieldWidth =  parseInt(field.offsetWidth);
	var charcnt = field.value.length;        

	if (charcnt > maxlimit) { 
		field.value = field.value.substring(0, maxlimit);
	}
	else{ 
	    var percentage = parseInt(100 - (( maxlimit - charcnt) * 100)/maxlimit) ;
	    document.getElementById(counter).style.width =  parseInt((fieldWidth*percentage)/100)+"px";
	    //document.getElementById(counter).innerHTML= "" + percentage + "%"
	    document.getElementById(counter).align="center"
	    setcolor(document.getElementById(counter),percentage,"background-color");
	}
    //field.style.height= percentage  + 150;
}



function setcolor(obj,percentage,prop){
	obj.style[prop] = "rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
}

function changestyle(){
    document.getElementById('ctl00_TemplateStyle').href = '/Styles/PrintAble.css';
}


function showMenu(cntrl) 
{

var TargetControl; 
    TargetControl = document.getElementById(cntrl);
    if (TargetControl)
    {
    TargetControl.style.display="";
    }		
}
function hideMenu(cntrl) 
{

var TargetControl; 

    TargetControl = document.getElementById(cntrl);
    if (TargetControl)
    {
    TargetControl.style.display="none";
    }		
}

function ShowHide(cntrl) 
{
var TargetControl; 

    TargetControl = document.getElementById(cntrl);
    if (TargetControl)
    {
    if (TargetControl.style.display == "none")
    {
    TargetControl.style.display=""; 
    }
    else 
    {
    TargetControl.style.display="none"; 
    }
    }		
}
function OpenDocWindow(url)
{
    window.open(url,"DocWindow","menubar=1,resizable=1,width=650,height=540,scrollbars=1,status=1");
}
function OpenWindow(url,width,height)
{
    window.open(url,"DocWindow" + width,'menubar=1,resizable=1,width=' + width + ',height=' + height + ',scrollbars=1,status=1');
}


function OpenSearch(prefix, searchText)
{
    var ctrl= document.getElementById(searchText) ; 
    if (ctrl)
    {
        window.location = prefix + "/Search/?Kw=" + ctrl.value;
    }
}

function resizeCaller() {
    var dyniframe=new Array()
    for (i=0; i<iframeids.length; i++)
    {
        if (document.getElementById)
            resizeIframe(iframeids[i])
        //reveal iframe for lower end browsers? (see var above):
        if ((document.all || document.getElementById) && iframehide=="no"){
            var tempobj=document.all? document.all[iframeids[i]] : document.getElementById(iframeids[i])
            tempobj.style.display="block"
        }
    }
}

function resizeIframe(frameid){
    var currentfr=document.getElementById(frameid)
    var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
    var FFextraHeight=parseFloat(getFFVersion)>=0.1? 16 : 0 //extra height in px to add to iframe in FireFox 1.0+ browsers

    if (currentfr && !window.opera){
        currentfr.style.display="block"
        if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight) //ns6 syntax
            currentfr.height = currentfr.contentDocument.body.offsetHeight+FFextraHeight; 
        else if (currentfr.Document && currentfr.Document.body.scrollHeight) //ie5+ syntax
            currentfr.height = currentfr.Document.body.scrollHeight;

    }
}

function Validate_Required(e,c,m)
{
var field = document.getElementById(e);
var target = document.getElementById(c);

if (field){
  if (field.value == null || field.value == ""){
    field.select();
    target.innerHTML=m;
    return false;
 }
  else
  { target.innerHTML='';
    return true;}
}
}

function Validate_Email(e,c,m){
var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
var target = document.getElementById(c);
var field = document.getElementById(e)
if (field){
    var returnval=emailfilter.test(field.value)
    if (returnval==false)
        {field.select();
          target.innerHTML=m;
        }
        else
        target.innerHTML='';
    return returnval
    }
else 
    return false; 
}

function Validate_Length(e, c, min, max)
{
    var field = document.getElementById(e);
	var target = document.getElementById(c);
	var uInput = field.value;
	if(uInput.length >= min && uInput.length <= max){
		target.innerHTML='';
		return true;
	}else{
	    field.select()
	    target.innerHTML='Max allowed '+max;
		//target.alert("Please enter between " +min+ " and " +max+ " characters");
		return false;
	}
}

function Validate_Match(e,d,c,m)
{
var field = document.getElementById(e);
var field2 = document.getElementById(d);
var target = document.getElementById(c);


      if (field.value == field2.value)
        { target.innerHTML='';
          return true;
        }
       else
        {
        field2.select();
        target.innerHTML=m;
        return false;
        }
}

function SetMenuItem()
{
try {
    var channel = document.getElementById("_CHId");
    var menuItem; 
    var guid;

    if (channel){
        guid = channel.value;
        if (guid){
            menuItem= document.getElementById("TDM" + guid);
            if (menuItem)
                menuItem.className= 'MenuOver'; 
        }
    }
}    
catch(e){
    return null;
}
}

/*
submit comment on a posting
*/
 function submitComment(btn) {
    var theform; 
    var loadingImg;
    
    loadingImg= document.getElementById('_imgLoading');
    //theform = document.forms("_fbForm");
    theform = document.getElementById("_fbForm");
    
    if (theform)
    {
        document.getElementById('_fbName').value = document.getElementById('txtfbName').value;
        document.getElementById('_fbTitle').value = document.getElementById('txtfbTitle').value;
        document.getElementById('_fbBody').value = document.getElementById('txtfbComment').value;
        document.getElementById('_fbGuid').value = document.getElementById('txtfbGuid').value;
        
        theform.method= 'post';
        theform.target= '_blank';
        
     
        loadingImg.style.display='';
        btn.disabled='disabled';

        var status = AjaxRequest.submit(
            theform
            ,{
            'onSuccess':function(req){ 
                document.getElementById('_output').innerHTML=req.responseText; 
                if (document.getElementById('_cmtAddeddSucc'))
                    document.getElementById('_cmtMainTable').style.display='none'; 
                else
                   btn.disabled='';
                     loadingImg.style.display='none';
                
            }
            ,'onError':function(req){
                document.getElementById('_output').innerHTML=req.responseText;
                btn.disabled='';
                 loadingImg.style.display='none';
            }
            ,'onTimeout':function(req){
                document.getElementById('_output').innerHTML="time out";
                btn.disabled='';
                  loadingImg.style.display='none';
            }         
            }
        );
  }

  return status;
}


/*************************/
//Specify affected tags. Add or remove from list:
var tgs = new Array('div','td','span','p');

//Specify spectrum of different font sizes:
var szs = new Array('10px','11px','12px ','13px','14px','15px','16px','17px','18px','19px','20px','21px','24px','27px','35px','40px' );
var startSz = 7;

function FormSize( trgt,inc ) {
	if (!document.getElementById) return
	var d = document,cEl = null,sz = startSz,i,j,cTags;
	
	sz += inc;
	if ( sz < 0 ) sz = 0;
	if ( sz > 15 ) sz = 15;
	startSz = sz;
		
	if ( !( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];

	cEl.style.fontSize = szs[ sz ];

	for ( i = 0 ; i < tgs.length ; i++ ) {
		cTags = cEl.getElementsByTagName( tgs[ i ] );
		for ( j = 0 ; j < cTags.length ; j++ ) cTags[ j ].style.fontSize = szs[ sz ];
	}
}


//////////////////////////////////////
function bookMark()
{
var title; 
var url;

title= document.title;
url=window.location;

   if (window.sidebar)
   {
         window.sidebar.addPanel(title, url,"");
  }
  else if( window.external )
  {

       window.external.AddFavorite( url, title);
  }
  else if(window.opera && window.print)
  {
      return true;
  }

}


function showVideo(filePath,outputDiv)
    {
        var s1 = new SWFObject("/Media/Flash/player.swf","single","340","220","9");
        s1.addParam("allowfullscreen","true");
        s1.addParam("allowscriptaccess","always");
        s1.addVariable("file",filePath);
        s1.addVariable("image","/Media/Images/preview2.jpg");
        s1.addVariable("streamer","lighttpd");
        s1.write(outputDiv);
     }

function showVideo2(filePath,outputDiv,fwidth,fheight)
    {
        var s1 = new SWFObject("/Media/Flash/player.swf","single",fwidth,fheight,"9");
        s1.addParam("allowfullscreen","true");
        s1.addParam("allowscriptaccess","always");
        s1.addVariable("width",fwidth);
        s1.addVariable("height",fheight);
        s1.addVariable("file",filePath);
        s1.addVariable("image","/Media/Images/preview2.jpg");
        s1.addVariable("streamer","lighttpd");
        s1.write(outputDiv);
     }

function showAudio(filePath,outputDiv)
    {
        var s1 = new SWFObject("/Media/Flash/Audioplayer.swf","audiomp3","240","45","9");
        s1.addParam("autostart","no");
        s1.addParam("transparentpagebg","yes");
        s1.addVariable("soundFile",filePath);
        s1.write(outputDiv);
     }

var _lastDiv;
function ShowVideoData(ctrlid)
{
    var t=document.getElementById('_divVideoTitle');
    var s=document.getElementById('_divVideoSummary');
    var currentDiv =document.getElementById(ctrlid);
    var lastDiv =document.getElementById(_lastDiv);
    
      //alert(ctrlid);
    if ( t && s)
    {
        t.innerHTML = document.getElementById('_dht' + ctrlid).innerHTML;
        s.innerHTML =  document.getElementById('_dhs' + ctrlid).innerHTML;
        if (lastDiv)
            {lastDiv.className = 'thum'; 
            }
        currentDiv.className= 'thumSelected';
        _lastDiv = currentDiv.id;
    }
    
}
     
    
    
    
function PrintThisPage(dateCtrl , titleCtrl , content1) 
{
var txtOption='toolbar=yes,location=no,directories=yes,menubar=no,resizable=yes,scrollbars=yes,width=900,height=600,left=100,top=25';
var txtTitle; 
var txtBody;
var txtDate;
var txtFooter;
try{
    txtTitle = document.getElementById(titleCtrl).innerHTML;
    txtBody = document.getElementById(content1).innerHTML;
    txtDate = document.getElementById(dateCtrl).innerHTML;
    txtFooter = document.getElementById('_postingFooter').innerHTML;
}
catch(e){

};
var winprint=window.open('','',txtOption);
winprint.document.open();
winprint.document.write('<html dir=rtl><link href=/Styles/PrintAble.css rel=stylesheet type=text/css />');
winprint.document.write('<body><form><Table align=center width=650px border="0" cellpadding="0" cellspacing="5">');
winprint.document.write('<tr><td><img src=/Media/Images/logo-print.jpg></td></tr>');
winprint.document.write('<tr><td>');
winprint.document.write(txtDate);
winprint.document.write('</td></tr>');
winprint.document.write('<tr><td>');
winprint.document.write(txtTitle);
winprint.document.write('</td></tr>');

winprint.document.write('<tr><td>'); 
winprint.document.write(txtBody);
winprint.document.write('</td></tr></Table><br><div>');
winprint.document.write(txtFooter);
winprint.document.write('</div></form></body></html>');
winprint.document.write('<script>window.print();<');
winprint.document.write('/');
winprint.document.write('script>');
winprint.document.close();
winprint.focus();
}

////////////////Style change 

function createjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 return fileref
}

function replacejscssfile(oldfilename, newfilename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist using
 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
 var allsuspects=document.getElementsByTagName(targetelement)
 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
  if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(oldfilename)!=-1){
   var newelement=createjscssfile(newfilename, filetype)
   allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i])
  }
 }
}
///////////////Scroll functions //////////////////
var gTop=0;
var gIncVal=0;
  function scrollMe(arg)
  {    
    var objEncNav=document.getElementById('_encNav');
    
    // var scrollAmount define the fast and
    // the amount of scrolling pane
    var scrollAmount=240;
    var objEncNavHeight=parseInt(objEncNav.offsetHeight);
    var objEncNavTop=objEncNav.style.top;
    
    // var barHeight defines the heigt of inner layer
    // it must set -10 from max height
    var barHeight=290;   
    
    if(!objEncNavTop)
      objEncNavTop=0;
    else
      objEncNavTop=objEncNavTop.substring(0,objEncNavTop.length-2);
      
    if(arg>0)
    {
      if(objEncNavTop>=0 || objEncNavTop>(-(objEncNavHeight-(barHeight+scrollAmount))))
      {
        incrementValue=scrollAmount;
      }
      else if(objEncNavTop<(-(objEncNavHeight-(barHeight+scrollAmount))))
      {
        incrementValue=(objEncNavHeight-barHeight)+parseInt(objEncNavTop);
      }
      else
      {
        incrementValue=0;
      }
      encSmoothScroll("minus", parseInt(objEncNavTop), parseInt(incrementValue));
    }
    else
    {
      if(objEncNavTop<0 && ((parseInt(objEncNavTop)+scrollAmount) < 0))
      {
        incrementValue=scrollAmount;
      }
      else
      {
        incrementValue=-objEncNavTop-0;     
      }
      encSmoothScroll("plus", parseInt(objEncNavTop), parseInt(incrementValue));
    }
  }
  function encSmoothScroll(dir, currentVal, incValue)
  {
    gTop=currentVal;
    gIncVal=incValue;
    encScrollBy(dir, 0)
  }
  function encScrollBy(dir, val)
  { 
    if(val<gIncVal)
    {
      var tmpInc;      
      if((gIncVal-val)>1)      
      {
        tmpInc=Math.ceil((gIncVal-val)/10);
        if(tmpInc <1)
          tmpInc=1;
      }
      else
      {
        tmpInc=gIncVal-val;
      }
      
      val+=tmpInc;
      
      var objEncNav=document.getElementById('_encNav');
      if(dir=="plus")
      {
        objEncNav.style.top=gTop+val+"px";
      }
      else if(dir=="minus")
      {
        objEncNav.style.top=gTop-val+"px";
      }
      var t=setTimeout("encScrollBy('"+dir+"', "+val+");", 10);
    }
    else
    {
      clearTimeout(t);
    }
  }
