﻿
onerror=handleErr
var method;

function handleErr(msg,url,l)
{
    txt="Line Number="+ l  + "$#$#Errormessage=" + msg + " $#Method Name=" + method +"$#$# URL:" + url;
    ClickHandler.LogError(txt);
    return true
}


function LogOut()
{
    method = "LogOut";
    ClickHandler.LogOut()
}


function displayMyData()
  {
     //parent.frames["Left"].hightLightNode("1");
     var CurrentId=parent.frames["description"].TemplateId; 
     var res="";
     var inheritDate="false";
     var currentSelectedContainerId=parent.frames["description"].selectedContainerId;
     if(currentSelectedContainerId==null || currentSelectedContainerId=="undefined")
          return;
     var mySplitResult = currentSelectedContainerId.split("pnlDescription");
     currentSelectedContainerId=mySplitResult[1];
     res=ClickHandler.getMySingleInheritInfo(CurrentId,currentSelectedContainerId);
     
     if(res.value!="")    
     {
       if(parent.frames["Left"] != null)
        myDescriptionList=parent.frames["Left"].document.getElementById("selDescription");
       var mySplitResult = res.value.split(":");
       var result=ClickHandler.getDescriptionWithOrder(mySplitResult[0]);
       inheritDate=mySplitResult[3];
      var myDataTable=result.value;
      myDescriptionTable=myDataTable;
    var myObj=parent.frames["Left"].document.getElementById("dvOption");
    if(myObj!=null && myDescriptionList!=null)
    {
       myObj.style.display="block";
      if(parent.frames["Left"].document.getElementById("selDescription")!=null)
      {
          if(typeof(myDataTable)=="object") 
        { 
      //Clear the dropDownlist
          removeAllOptions(myDescriptionList);
                
        // Add ""choose style.. "" option
       optn=document.createElement("OPTION");
       optn.value="0";
       optn.text="Select Description";
       // optn.selected = "selected";
       myDescriptionList.options.add(optn); 
        
        //Populate it..
          for(var i=0; i< myDataTable.Rows.length; i++)
          {
                optn=document.createElement("OPTION");
                optn.value=myDataTable.Rows[i].DescOrder;
                optn.text=myDataTable.Rows[i].BriefDescription;
                if(optn.value==mySplitResult[1])
                optn.selected = "selected";
                myDescriptionList.options.add(optn);            
          } 
           if(parent.frames["Left"].document.getElementById("chkInherit")!=null)
              parent.frames["Left"].document.getElementById("chkInherit").checked=true;   
              if(inheritDate=="True")
              {
               if(parent.frames["Left"].document.getElementById("chkDate")!=null)
                parent.frames["Left"].document.getElementById("chkDate").checked=true;  
              
              }  
              else if(parent.frames["Left"].document.getElementById("chkDate")!=null)
                parent.frames["Left"].document.getElementById("chkDate").checked=false;  
                
    }
                
      }
    }
        
     }
     else if(parent.frames["Left"]!=null)
     {
      if(parent.frames["description"].selectedContainerId.title==null || parent.frames["description"].selectedContainerId.title=="")
      {
         var myObj=parent.frames["Left"].document.getElementById("dvOption");
         if(myObj!=null)
          myObj.style.display="none";
          }
          
     }
   
     
  } 
  
function TextSelection(Identity)
{
    var range = parent.frames["description"].window.getSelection().getRangeAt(0);
    var newNode = parent.frames["description"].document.createElement('A');
     var homePage = ClickHandler.getHomePage().value;
    newNode.setAttribute('href',homePage);
    newNode.setAttribute('onclick','alert("hello all")');
    range.surroundContents(newNode);
//    var myDiv = parent.frames["description"].document.getElementById(Identity);
//    //alert(obj.textContent);
//    var selection = parent.frames["description"].window.getSelection();
//      var range = parent.frames["description"].document.createRange();
//      //selection.setBaseAndExtent(myDiv, 0, myDiv, 1);
////        var selection = parent.frames["description"].window.getSelection();
//    range.setStart( selection.baseNode, selection.baseOffset );
//    range.setEnd( selection.extentNode, selection.extentOffset );
//     var node = parent.frames["description"].document.createElement("A");
//     node.setAttribute('href',"home.aspx");
//     range.insertNode(node);
//       selection.removeAllRanges();
//   parent.frames["description"].window.getSelection().addRange(range);
//   //alert(range.innerHTML);
//    alert(parent.frames["description"].window.getSelection());
}  
//function ClearText(Identity)
//{
//   
//    method = "ClearText";
//      var curRefer =  parent.frames["Left"].location.href;
//     if(curRefer!=null && curRefer.indexOf("LeftInherit.aspx")!=-1)
//      { 
//         displayMyData();
//        }
//    selectedContainerId=Identity;
//    var obj =null;
//    if(parent.frames["description"].document !=null)
//        obj=parent.frames["description"].document.getElementById(Identity);
//    if(obj != null)
//    {
//        var val=obj.innerHTML;
//        if(val.indexOf("Lorem ipsum dolor sit amet") != -1)
//            obj.innerHTML="";
//        ReflectDescriptionStyle();
//    }
//}

function setChanged(event)
{
    method = "setChanged";
    ClickHandler.SetChanged();
}



/*------------------------------------ */
/* methods for Permission checking -- by sudeep */
/*------------------------------------ */

function checkPermission(permName, showErrorMessage) {
    if (typeof showErrorMessage == "undefined") {
        showErrorMessage = true;
    }
    var result = CMSPermissions.CheckUserPermission(permName);    
    if (result.value == false) {
        parent.frames["description"].hideLoading();
        
        if (showErrorMessage)
            alert('Sorry, you don\'t have permission to use this feature.');
    }
        
    return result.value;   
   
   
}

function ClearText(Identity)
{
    //alert("debugging");

   
    method = "ClearText";
    
    var tempid = parent.frames["description"].TemplateId;
   
    var curRefer =  parent.frames["Left"].location.href;
    var myCount=ClickHandler.chkDescriptionInherit(tempid,Identity);
    //alert(myCount);
    selectedContainerId=Identity;
    //alert(parent.frames["description"].document.getElementById(selectedContainerId).contentEditable);
    if(myCount.value !="0")    
    {
        parent.document.getElementById("main").cols="*,254";
        if(curRefer!=null && curRefer.indexOf("LeftInherit.aspx")!=-1)
        { 
         displayMyData();
        }
        else
        {
            
            
            var res = ClickHandler.getIsIndex(tempid);
            if(res.value)
                parent.frames["Left"].location.href="Private/LeftInherit.aspx";
            else
                parent.frames["Left"].location.href="../Private/LeftInherit.aspx";
        }
   }
    
    
    var obj =null;
    //alert(parent.document.getElementById("description"));
    if(parent.frames["description"].document !=null)
        obj=parent.frames["description"].document.getElementById(Identity);
    // alert(obj);
    if(obj != null)
    {
        var val=obj.innerHTML;
        if(val.indexOf("Lorem ipsum dolor sit amet") != -1)
            {
                if(navigator.userAgent.toLowerCase().indexOf("safari") != -1)
                 {
                    obj.innerHTML = ' ';
                    
                 }
                 else if(navigator.userAgent.toLowerCase().indexOf("firefox") != -1)
                 {
                  
                   
                     obj.innerHTML = "";
                 }
                 else
                 {
                   
                    obj.innerHTML = "";
                    
                    
                 }
                
                
            }
        ReflectDescriptionStyle();
    }
}




 function PasteHandler(event,obj) {
    method='PasteHandler';
    // skip when it isnt Ctrl+V
    if(navigator.platform.toLowerCase().indexOf('mac') == -1){
        var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	    var controlKey = (event.altKey ? 1 : 0) + (event.ctrlKey ? 2 : 0) + (event.shiftKey ? 4 : 0);
	    if (!(controlKey == 2 && keyCode == 86)) return;
	}
	else{
	     var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	    var controlKey = (event.altKey ? 1 : 0) + (event.ctrlKey ? 2 : 0) + (event.shiftKey ? 4 : 0) + (event.metaKey ? 5 : 0);
	    if (!(controlKey == 5 && keyCode == 86)) return;
	}
	if (!document.all){
	  setTimeout('clearFormat();',5);
	  return;
	}
	// skip when the element is not in editable mode
	// implies user is not in admin mode.
	// for non-IE we should use event.target object rather than srcElement
	if (!window.event.srcElement.contentEditable) return;
	var str = window.clipboardData.getData('text');
	document.execCommand('Paste',false,str);
	document.getElementById(obj).contentEditable = false;
	setTimeout("settingContentEditable(" + obj  + ")",2);
	window.clipboardData.setData('Text',str);
	setTimeout("setParagraph();",5);
	return false;
}
function setParagraph(){
    var strs = document.getElementById(selectedContainerId);
    strs.innerHTML = strs.innerHTML.replace(/<br>/gi,'</p><p>');
}
function SetSelectedContainerId(obj){
  selectedContainerId = obj;
    
    if (selectedContainerId != null && typeof (selectedContainerId) != "undefined" && selectedContainerId.indexOf("pnlDescription") >= 0 ) {
     
        if (parent.frames["description"].CurrentDynamicListControl != null) {
            parent.frames["description"].CurrentDynamicListControl.DynamicControlRequest = false;
        }
    }
}
function settingContentEditable(obj){
    obj.contentEditable = true;
}
function clearFormat(){
     var myDiv = document.getElementById(selectedContainerId);
     var str = document.getElementById(selectedContainerId).innerHTML;
     str = str.replace(/\n/ig,' ');
     str = str.replace(/<xml.*?>.*?<\/xml.*?>/ig, '');
     str = str.replace(/<style.*?>.*?<\/style.*?>/ig,'');
     str = str.replace(/((?!<((\/)?p|br))<[^>]*>)/gi,'');
     str = str.replace( /{\S[^}{]*}/ig, '' );
     str = str.replace(/<p\s[^>]*>/gi,'<p>');
     str = str.replace(/<br>/gi,'</p><p>')
     return myDiv.innerHTML = str;
}
function DisableRightClick(e){
    method='DisableRightClick';
    if (document.all){
        if (event.button==2){
          alert('context menu is disabled');
          return false;
        }
      }
      else{
        if (e.button==2){
          e.preventDefault();
          e.stopPropagation();
          alert('context menu is disabled');
          return false;
        }
      }
  }

  function scaleSize(maxW, maxH, currW, currH) {
      var resizeFactor = 0;
      if (currH > currW) {
          //The image is Portrait
          resizeFactor = currH / maxH;
          currH = maxH;
          currW = currW / resizeFactor;
      }
      else {
          //The image is Landscape or Square
          resizeFactor = currW / maxW;
          currW = maxW;
          currH = currH / resizeFactor;
      }
      return [currW, currH];
  }

  function ResizeImage()
                {
                   
                    try {

                        var objImgSize = ClickHandler.GetImgSizeForResize(parent.frames['description'].document.getElementById('backgroundimg').src);
                        if (objImgSize != null && objImgSize.value != null) {

                            var pageSize = getPageSizeInternal();
                            var newSize = scaleSize(pageSize.pageWidth, pageSize.pageHeight, objImgSize.value.Width, objImgSize.value.Height);
                            if (newSize[1] < pageSize.winHeight) {
                                if (newSize[1] > newSize[0]) {
                                 
                                    var rt = newSize[1] / newSize[0];
                                    var ht = pageSize.winHeight;
                                    var wd = ht / rt;
                                    parent.frames['description'].document.getElementById('backgroundimg').style.width = wd + 'px';
                                    parent.frames['description'].document.getElementById('backgroundimg').style.height = Math.round(ht) + 'px';
                                    return;
                                }
                                else {
                                    var rt = newSize[1] / newSize[0];
                                    var ht = pageSize.winHeight;
                                    var wd = ht / rt;
                                    parent.frames['description'].document.getElementById('backgroundimg').style.width = wd + 'px';
                                    parent.frames['description'].document.getElementById('backgroundimg').style.height = Math.round(ht) + 'px';
                                    return;
                                }
                            }
                            parent.frames['description'].document.getElementById('backgroundimg').style.width = newSize[0] + 'px';
                            parent.frames['description'].document.getElementById('backgroundimg').style.height = Math.round(newSize[1]) + 'px';


                        }

                    }
                    catch(e)
                    {
                        
                      
                   
                    var resultFlash = ClickHandler.GetBackGroundFlash(TemplateId);
                    if(resultFlash == null || resultFlash.value == null || resultFlash.value == '')
                        return;
                    else{
                        var IsFlash = resultFlash.value.split('$')[1];
                        if(IsFlash == '' || IsFlash != 'True')
                            return;
                        else{
                            var FlashFileNames = resultFlash.value.split('$')[0];
                            var FlashFileName = FlashFileNames.split('/')[FlashFileNames.split('/').length - 1];
                            var FlashObj = ClickHandler.GetSwfInfoForBackground(FlashFileName);
                            if(typeof(FlashObj) != 'object' || FlashObj == null || FlashObj.value == null)
                                return;
                            else{
                                    //get flash size
                                    var pageSize = getPageSizeInternal();
                                    var FlashWidth = FlashObj.value.Width;
                                    var FlashHeight = FlashObj.value.Height;
                                    var WidthRatio = FlashWidth / pageSize.winWidth;
                                    var HeightRatio = FlashHeight /pageSize.winHeight;
                                    
                                    if(WidthRatio < HeightRatio)
                                     {
                                        var wid =  FlashWidth/WidthRatio;
                                        var ht =  FlashHeight/WidthRatio;
                                    }
                                    else
                                    {
                                        var wid = FlashWidth /  HeightRatio ;
                                        var ht =  FlashHeight / HeightRatio;
                                        }

                 
                                        var newWidth = 'width="'+parseInt(wid)+'"';
                                        var newstyle = ' style="WIDTH:'+parseInt(wid)+'px;HEIGHT:'+parseInt(ht)+'px" '; 
                                        var newHeight = 'height="'+parseInt(ht)+'"';
                                 
                    
                                    
                                    if( parent.frames["description"].document.getElementById('myFlashDiv') != null)
                                    {
                                    parent.frames["description"].document.getElementById('myFlashDiv').innerHTML =    parent.frames["description"].document.getElementById('myFlashDiv').innerHTML.replace(/style\s*=\s*"\s*width\s*:\s*\d+px\s*;\s*height\s*:\s*\d+px"/gi,newstyle);
                                    parent.frames["description"].document.getElementById('myFlashDiv').innerHTML =   parent.frames["description"].document.getElementById('myFlashDiv').innerHTML.replace(/width=""\d+""/gi,newWidth).replace(/height=""\d+""/gi,newHeight);
                                    }
                               
                            }
                         
                        }
                    }
                        

                  }
              
                } 

             
