﻿// JScript File
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function GetSafeDocumentIDVal(theID)
{
    if (document.getElementById(theID) != null)
        return getElementValue(document.getElementById(theID));
    return "";
}

function callback_page_content(result)
{
    var split_array=result.split("~|~");
    for ( var i = 0 ; i < split_array.length ; i++ )
	{
	    theID = "page_content";
	    if (i > 0)
	        theID = theID + i;
	    document.getElementById(theID).innerHTML = split_array[i];
	}
	FlowPlayer=document.getElementById("FlowPlayer");
	window.scroll(0,0);
}

function BuiltEditors()
{
    var oInp = document.getElementsByTagName('textarea');
    var oRad = new Array();
    var j=0;
    for(var i=0;i<oInp.length;i++)
    {
        if(oInp[i].getAttribute('type')=='textarea')
        {
            var oFCKeditor = null;
            theID = oInp[i].getAttribute('id');
            oFCKeditor = new FCKeditor( theID );
            oFCKeditor.BasePath = "js/FCKeditor/" ;
	        oFCKeditor.Config[ "AutoDetectLanguage" ] = false ;
	        oFCKeditor.Config[ "DefaultLanguage" ] = "he" ;
	        oFCKeditor.Config[ "ContentLangDirection" ]	= "rtl" ;
	        oFCKeditor.Height = "300px"
	        oFCKeditor.ReplaceTextarea() ;
            j++;
        }
    }
}

function callback_page_content_with_editor(result)
{
    callback_page_content(result);
    BuiltEditors();
}

function callback_page_content_sc(result)
{
    document.getElementById("page_content").innerHTML = result;
    GetChapterAgain(document.getElementById('selector0').options[document.getElementById('selector0').selectedIndex].value);
}

function errorCallback(result)
{
    alert(result);
}

function downloadFile()
{
	document.getElementById("downloadFile").click();
}

function callback_create_csv(result)
{
    document.getElementById("download_csv").href=result;
    document.getElementById("download_csv").click();
}

function submitASPForm(sNewFormAction)
{
    document.forms[0].action = sNewFormAction;
    document.forms[0].__VIEWSTATE.name = 'NOVIEWSTATE';
    document.forms[0].submit();
}

function popUp(URL , WiNname) 
{
    theWin = window.open(URL, WiNname , "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=700,height=525,left = 290,top = 249.5");
    if (theWin == null)
    {
        alert("המערכת זיהתה חסימה של PopUp Blocker. אנא נטרלו את פעולתם על האתר לעבודה תקינה.");
    }
}

function ChangePic(theID , theVal)
{
    RS.Execute("adm_utils.aspx", "ChangePic" , theID , theVal , callback_change_pic, errorCallback);       
}

function ChangeVid(theID , theVal , theTable)
{
    RS.Execute("adm_utils.aspx", "ChangeVid" , theID , theVal , theTable , callback_change_vid, errorCallback);       
}

function callback_ChangeActiveStateRow(result)
{
    var split_array=result.split("~~|~~");
    if (split_array.length = 2)
    {
        document.getElementById(split_array[0]).innerHTML = split_array[1];
    }
}

function callback_change_pic(result)
{
    var split_array=result.split("~|~");
    if (split_array.length = 2)
    {
        document.getElementById(theID).value = split_array[0];
        var split2 = (split_array[0]).split("_val");
        theBrowseId = split2[0] + "_pic_beowse";
        document.getElementById(theBrowseId).innerHTML = split_array[1];
    }
}

function ChangeVideoPlayer(theObjID , iframeSrc)
{
    theObjID = theObjID + "_palyer";
    theHTML = "<IFRAME SRC=\"" + iframeSrc;
    theHTML += "\" WIDTH=\"450\" HEIGHT=\"400\" FRAMEBORDER=\"0\"></IFRAME>";
    document.getElementById(theObjID).innerHTML = theHTML;
}

function callback_change_vid(result)
{
    var split_array=result.split("~|~");
    if (split_array.length = 2)
    {
        //document.getElementById(theID).value = split_array[0];
        var split2 = (split_array[0]).split("_val");
        theBrowseId = split2[0] + "_vid_beowse";
        document.getElementById(theBrowseId).innerHTML = split_array[1];        
    }
}

function OpenPicBrowser(theID , maxPics)
{
    theVal = document.getElementById(theID).value;
    theURL = "adm_pic_popup_selector.aspx?pics_ids=" + theVal + "&theID=" + theID + "&maxPics=" + maxPics;
    popUp(theURL , 'PicSelector');
}

function OpenVidBrowser(theID , maxPics , vidTable , vidTableTags , vidTableTagsRef)
{
    theVal = document.getElementById(theID).value;
    theURL = "adm_video_popup_selector.aspx?pics_ids=" + theVal + "&theID=" + theID + "&maxPics=" + maxPics + "&vidTable=" + vidTable + "&vidTableTags=" + vidTableTags + "&vidTableTagsRef=" + vidTableTagsRef;
    popUp(theURL , 'PicSelector');
}

function replaceAll(theStr , toReplace , withReplace)
{
    newStr = theStr.replace(toReplace , withReplace);
    if (newStr != theStr)
        return replaceAll(newStr , toReplace , withReplace);
    return newStr;
}

function tagSelect(CollectionVal , Collectiontxt , CollectionTable)
{
    Collectiontxt = replaceAll(Collectiontxt , "~~qoute~~"  , "\"");
    Collectiontxt = replaceAll(Collectiontxt , "~~apos~~"  , "'");
    //Collectiontxt = Collectiontxt.replace("~~qoute~~" , "\"").replace("~~apos~~" , "'");
    currVal = document.getElementById(CollectionTable+"_coll").value;
    if (currVal.indexOf(Collectiontxt + ";") >= 0)
    {
        //return;
        //loc = currVal.indexOf(Collectiontxt + ";");
        //len = Collectiontxt.length + 1;
        //currVal1 = currVal.substr(0 , loc);
        //currVal2 = "";
        //if ((loc + len) < currVal.length)
        //{
            //currVal2 = currVal.substr((loc + len + 1) , currVal.length - (loc + len + 1));
        //}
        //currVal = currVal1 + currVal2;
        newVal = replaceAll(currVal , Collectiontxt + ";" , "")
        //document.getElementById(CollectionTable+"_coll").value = currVal;
        document.getElementById(CollectionTable+"_coll").value = newVal;
        if (window.document.getElementById("tag_" + CollectionVal) != null)
            window.document.getElementById("tag_" + CollectionVal).className = "tags";
        closeCollDiv(CollectionTable+"_coll");
    }
    else
    {
        loc = currVal.lastIndexOf(";");
        len = currVal.length;
        currVal = currVal.substr(0 , loc + 1);
        currVal = currVal + Collectiontxt  + ";";
        document.getElementById(CollectionTable+"_coll").value = currVal;
        if (window.document.getElementById("tag_" + CollectionVal) != null)
            window.document.getElementById("tag_" + CollectionVal).className = "tags_selected";
        closeCollDiv(CollectionTable+"_coll");
    }
    
}
function SplitByChar(toSplit , theCharToSplitWith)
{
    loc = toSplit.lastIndexOf(theCharToSplitWith);
    len = toSplit.length;
    return toSplit.substr(loc + 1,len-loc);
}

function tagKeyPress(CollectionTable , MiddleTable , collectionTextField , CollCss)
{
    var currVal = document.getElementById(MiddleTable+"_coll").value;
    theString = SplitByChar(currVal , ";");
    theString = SplitByChar(theString , ",");
    theString = SplitByChar(theString , ":");
    if (theString != "")
        RS.Execute("adm_utils.aspx", "GetCollectionFill" , MiddleTable , CollectionTable , collectionTextField , theString , CollCss , currVal , callback_coll_fill, errorCallback);       
    else
        closeCollDiv(CollectionTable+"_coll");
    return true;
}

function getMousePosition(e) 
{ 
   return e.pageX ? {'x':e.pageX, 'y':e.pageY} : 
          {'x':e.clientX + document.documentElement.scrollLeft + document.body.scrollLeft, 'y':
          e.clientY + document.documentElement.scrollTop + document.body.scrollTop}; 
}; 
   
function showMousePos(e) 
{ 
   if (!e) e = event; // make sure we have a reference to the event 
   var mp = getMousePosition(e);
   var divref = document.getElementById('PopUp');
   divref.style.left = mp.x + 'px';
   divref.style.top  = mp.y + 'px';   
   divref.style.display = 'block'; 
   return;
}; 


function openTechDetails(theUpdater , sCreate , sUpdate , sPublish)
{
    theHtml = "<table dir=rtl class=adm_table  cellspacing='1' cellpadding='0' border='1'>";
    theHtml += "<tr><td>";
    theHtml += "<table dir='rtl' border='0' cellpadding='6' cellspacing='0'>";
    theHtml += "<tr class='adm_table_header_nbg'>";
    theHtml += "<td nowrap valign=top>";
    theHtml += "<table>";
    theHtml += "<tr>";
    theHtml += "<td>אחרון לעדכן: </td>";
    theHtml += "<td>" + theUpdater + "</td>";
    theHtml += "</tr>"; 
    theHtml += "<tr>";
    theHtml += "<td>נוצר ב: </td>";
    theHtml += "<td>" + sCreate + "</td>";
    theHtml += "</tr>"; 
    theHtml += "<tr>";
    theHtml += "<td>עודכן לאחרונה ב: </td>";
    theHtml += "<td>" + sUpdate + "</td>";
    theHtml += "</tr>"; 
    theHtml += "<tr>";
    theHtml += "<td>פורסם לאתר ב: </td>";
    theHtml += "<td>" + sPublish + "</td>";
    theHtml += "</tr>"; 
    theHtml += "</table>";
    theHtml += "</td>";
    theHtml += "</tr></table></td></tr></table>";
    oDiv = document.getElementById("tag_collections_div");
	oDiv.style.display = "block";
	var mp = getMousePosition(event);
	oDiv.style.left = (mp.x+10) + 'px';
	oDiv.style.top = mp.y + 'px';
	oDiv.innerHTML = theHtml;
//	Drag.init(oDiv);
    
}

function openLocalWindow(theText)
{
    if (theText == "")
        theText = "- אין הערות עורך -";
    theHtml = "<table dir=rtl class=adm_table  cellspacing='1' cellpadding='0' border='1' width='120px'>";
    theHtml += "<tr><td>";
    theHtml += "<table dir='rtl' border='0' cellpadding='6' cellspacing='0'>";
    theHtml += "<tr class='adm_table_header_nbg'>";
    theHtml += "<td nowrap valign=top>";
    theHtml += "<table>";
    theHtml += "<tr>";
    theHtml += "<td>" + theText + "</td>";
    theHtml += "</tr>"; 
    theHtml += "</table>";
    theHtml += "</td>";
    theHtml += "</tr></table></td></tr></table>";
    oDiv = document.getElementById("tag_collections_div");
	oDiv.style.display = "block";
	var mp = getMousePosition(event);
	oDiv.style.left = (mp.x+10) + 'px';
	oDiv.style.top = mp.y + 'px';
	oDiv.innerHTML = theHtml;    
}

function searchPress(CollectionTable , CollectionTextField , CollectionPointerField , pageID , CollCss)
{
    var currVal = document.getElementById(pageID+"_val_text").value;
    RS.Execute("adm_utils.aspx", "GetCollectionSearch" , currVal , CollectionTable , CollectionTextField , CollectionPointerField , pageID , CollCss , callback_coll_fill, errorCallback);       
}

function closeCollDiv(focusTo)
{
	oClockDiv = document.getElementById("tag_collections_div");
	oClockDiv.innerHTML="";
	if (focusTo != "")
	    document.getElementById(focusTo).focus();
}

function callback_coll_fill(result)
{
    if (result == "")
        return;
    loc = result.indexOf("|");
    
    CollectionTable = result.substr(0 , loc);
    theTable = result.substr(loc + 1 , result.length - loc);
    var coordinates = getAnchorPosition(CollectionTable+"_coll");
   
	oDiv = document.getElementById("tag_collections_div");
	oDiv.style.display = "block";
	oDiv.style.left = coordinates.x + 300;
	oDiv.style.top = coordinates.y + 15;
	oDiv.innerHTML = theTable;
	Drag.init(oDiv);
}

function ShowBanner(path , width , height)
{
    var thePath = "adm_watch_flash.aspx?path=";
    thePath = thePath + path;
    thePath = thePath + "&width="
    thePath = thePath + width;
    thePath = thePath + "&height="
    thePath = thePath + height;
    window.open( thePath, "banner_watcher", 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=' + width + ',height=' + height ) ;
}

function getElementValue(formElement)
{
    if(formElement.length != null) var type = formElement[0].type;
    if((typeof(type) == 'undefined') || (type == 0)) var type = formElement.type;
    
    switch(type)
    {
        case 'undefined': return "";

        case 'radio':
	        for(var x=0; x < formElement.length; x++) 
		        if(formElement[x].checked == true)
	        return formElement[x].value;

        case 'select-multiple':
	        var myArray = new Array();
	        for(var x=0; x < formElement.length; x++) 
		        if(formElement[x].selected == true)
			        myArray[myArray.length] = formElement[x].value;
	        return myArray;

        case 'checkbox': return formElement.checked;
	
        default: return formElement.value;
    }
}

function validateGenericForm()
{
    var bCont = true;
    var i = 0;
    var bOK = true;
    while (bCont == true)
    {
        var sName = i + "_val";
        var sNameMin = i + "_valHour";
        var sNameHour = i + "_valMin";
        var sName2 = i + "_val2";
        var sType = i + "_type";
        var sMust = i + "_must";
        var sID = i + "_id";
        var sInputType = i + "_inputtype";
        var sValidation = i + "_validation";
        if (document.getElementById(sType) == null)
        {
            bCont = false;
        }
        else
        {
            var sTypeVal = document.getElementById(sType).value;
            
            if (sTypeVal == "date" || sTypeVal == "string" || sTypeVal == "int" || sTypeVal == "long_string" || sTypeVal == "file" || sTypeVal == "multi")
            {
                if (document.getElementById(sInputType) == null)
                {
                    theVal = getElementValue(document.getElementById(sName));
                }
                else
                {
                    theVal = getElementValue(document.getElementsByName(sName));
                }
                if (document.getElementById(sMust) != null)
                {
                    if (getElementValue(document.getElementById(sMust)) == "True")
                    {
                        if (theVal == null || theVal == "")
                        {
                            bOK = false;
                            document.getElementById(sName).style.cssText = "border: 1px solid red;color: red;";
                        }
                    }
                }
            }
            if (sTypeVal == "datetime")
            {
                theVal = getElementValue(document.getElementById(sName));
                theValMin = getElementValue(document.getElementById(sNameMin));
                theValHour = getElementValue(document.getElementById(sNameHour));
                
                if (document.getElementById(sMust) != null)
                {
                
                    if (getElementValue(document.getElementById(sMust)) == "True")
                    {
                        if (theVal == "" || theValMin == "" || theValHour == "")
                        {
                            bOK = false;
                            document.getElementById(sName).style.cssText = "border: 1px solid red;color: red;";
                            document.getElementById(sNameMin).style.cssText = "border: 1px solid red;color: red;";
                            document.getElementById(sNameHour).style.cssText = "border: 1px solid red;color: red;";
                        }
                        else
                        {
                            document.getElementById(sName).style.cssText = "border: 1px solid #000000;color: #000000;";
                            document.getElementById(sNameMin).style.cssText = "border: 1px solid #000000;color: #000000;";
                            document.getElementById(sNameHour).style.cssText = "border: 1px solid #000000;color: #000000;";
                        }
                    }
                }
            }
            if (sTypeVal == "time")
            {
                theVal = getElementValue(document.getElementById(sName));
                theVal2 = getElementValue(document.getElementById(sName2));
                
                if (document.getElementById(sMust) != null)
                {
                
                    if (getElementValue(document.getElementById(sMust)) == "True")
                    {
                        if (theVal == "" || theVal2 == "")
                        {
                            bOK = false;
                            document.getElementById(sName).style.cssText = "border: 1px solid red;color: red;";
                            document.getElementById(sName2).style.cssText = "border: 1px solid red;color: red;";
                        }
                        else
                        {
                            document.getElementById(sName).style.cssText = "border: 1px solid #000000;color: #000000;";
                            document.getElementById(sName2).style.cssText = "border: 1px solid #000000;color: #000000;";
                        }
                    }
                }
            }
        }
        i = i + 1;
    }
    return bOK;
}
function submitASPFormWithCheck(thePage)
{
    if( validateGenericForm() )
        submitASPForm(thePage);
    else
    {
        grayOut(true , {'opacity':'35'});
        alert("אנא מלא את כל שדות החובה");
        grayOut(false);
    }
}

function grayOut(vis, options) 
{  
	// Pass true to gray out screen, false to ungray  
	// options are optional.  This is a JSON object with the following (optional) properties  
	// opacity:0-100         
	// Lower number = less grayout higher = more of a blackout   
	// zindex: #             
	// HTML elements with a higher zindex appear on top of the gray out  
	// bgcolor: (#xxxxxx)    
	// Standard RGB Hex color code  
	// grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});  
	// Because options is JSON opacity/zindex/bgcolor are all optional and can appear  
	// in any order.  Pass only the properties you need to set.  
	var options = options || {};   
	var zindex = options.zindex || 50;  
	var opacity = options.opacity || 70;  
	var opaque = (opacity / 100);  
	var bgcolor = options.bgcolor || '#000000';  
	var dark=document.getElementById('darkenScreenObject');  
	if (!dark) 
	{    
		// The dark layer doesn't exist, it's never been created.  So we'll    
		// create it here and apply some basic styles.    
		// If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917    
		var tbody = document.getElementsByTagName("body")[0];    
		var tnode = document.createElement('div');           
		// Create the layer.        
		tnode.style.position='absolute';                 
		// Position absolutely        
		tnode.style.top='0px';                           
		// In the top        
		tnode.style.left='0px';                          
		// Left corner of the page        
		tnode.style.overflow='hidden';                   
		// Try to avoid making scroll bars                    
		tnode.style.display='none';                      
		// Start out Hidden        
		tnode.id='darkenScreenObject';                   
		// Name it so we can find it later    
		tbody.appendChild(tnode);                            
		// Add it to the web page    
		dark=document.getElementById('darkenScreenObject');  
		// Get the object.  
	}  
	if (vis) 
	{    		
		// Calculate the page width and height     
		if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) 
		{        
			var pageWidth = document.body.scrollWidth+'px';        
			var pageHeight = document.body.scrollHeight+'px';    
		} 
		else if( document.body.offsetWidth ) 
		{      
			var pageWidth = document.body.offsetWidth+'px';      
			var pageHeight = document.body.offsetHeight+'px';    
		} 
		else 
		{       
			var pageWidth='100%';       
			var pageHeight='100%';    
		}       
		//set the shader to cover the entire page and make it visible.    
		dark.style.opacity=opaque;
		dark.style.MozOpacity=opaque;                       
		dark.style.filter='alpha(opacity='+opacity+')';     
		dark.style.zIndex=zindex;            
		dark.style.backgroundColor=bgcolor;      
		dark.style.width= pageWidth;    
		dark.style.height= pageHeight;    
		dark.style.display='block';                            
	} 
	else 
	{     
		dark.style.display='none';  
	}
}

function getFile(pURL) 
{
   if (window.XMLHttpRequest) { // code for Mozilla, Safari, etc 
      xmlhttp=new XMLHttpRequest();
      xmlhttp.onreadystatechange=postFileReady;
      xmlhttp.open("GET", pURL, true);
      xmlhttp.send(null);
   } 
   else if (window.ActiveXObject) 
   { //IE 
      xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); 
      if (xmlhttp) 
      {
         xmlhttp.onreadystatechange=postFileReady;
         xmlHttp.setRequestHeader("Content-Type:text/html; charset:ISO-8859-1");
         xmlhttp.open('GET', pURL, true);
         xmlhttp.send();
      }
   }
}

function ChangeActiveStateRow(theTableName , theID , requestedStatus)
{
    RS.Execute("adm_utils.aspx", "ChangeActiveStateRow" , theTableName , theID , requestedStatus , callback_ChangeActiveStateRow, errorCallback);       
}

// function to handle asynchronous call
function postFileReady() 
{
   if (xmlhttp.readyState==4) 
   { 
      if (xmlhttp.status==200) 
      { 
        
         document.getElementById('message_div').innerHTML=xmlhttp.responseText;
         window.scroll(0,0);
         var de = document.documentElement;
         var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
         var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
         leftvar = parseInt((w - 220) / 2);
         rightvar = parseInt((h - 100) / 2);
         var topPlace = rightvar + "px";
         var leftPlace = leftvar + "px";
         document.getElementById('message_div').style.top = topPlace;
         document.getElementById('message_div').style.left = leftPlace;
         OpenID("message_div");
      }
   }
}

