﻿/****************************************************************************
 * Utils
 * contains utility functions in:
 * - help screen creature display
 * - cookies
 * - navigation tree control
 ****************************************************************************/

/****************************************************************************/
// PART 1 - HELP SCREEN CREATURE DISPLAY
/****************************************************************************/

/****************************************************************************/
// FireHelpCreauture()
// Displays the help character on the screen in a flash object written onto
// a DIV.
/****************************************************************************/
function FireHelpCreauture() {
	document.write('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"');
	document.write('codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"');
	document.write('WIDTH="106" HEIGHT="600" id="swfCreature" ALIGN="left" VIEWASTEXT><PARAM NAME=movie VALUE="/images/layout/firegg4.swf"');
	document.write('<PARAM NAME=menu VALUE=false>');
	document.write('<PARAM NAME=quality VALUE=high>');
	document.write('<PARAM NAME=scale VALUE=exactfit>'); 
	document.write('<PARAM NAME=Loop VALUE=true>');
	document.write('<PARAM NAME=wmode VALUE="transparent">');
	document.write('<EMBED src="/images/layout/firegg4.swf" wmode="transparent" menu=false quality=high scale=exactfit loop =true WIDTH="106" HEIGHT="600" NAME="firegg4.swf" ALIGN="left"');
	document.write('TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>');						
	document.write('</OBJECT>');

}

/****************************************************************************/
// setDragElement()
// sets coordinates to use when dragging the help DIV around
/****************************************************************************/	
function setDragElement(drag,e)
{
	if(typeof e=="undefined")e=window.event;
	target=(typeof e.target=="undefined")? e.srcElement: e.target;

	if(target.className!="dragHandle")
		return false;
	
	mouseover=true
	pleft=document.getElementById('divHelp').style.pixelLeft
	ptop=document.getElementById('divHelp').style.pixelTop
	xcoor=e.clientX
	ycoor=e.clientY
	document.onmousemove=moveImage

}

/****************************************************************************/
// moveImage()
// moves the image around the window while dragging the mouse.
/****************************************************************************/	
function moveImage()
{
	if (mouseover&&event.button==1)
	{
		document.getElementById('divHelp').style.pixelLeft=pleft+event.clientX-xcoor
		document.getElementById('divHelp').style.pixelTop=ptop+event.clientY-ycoor
		return false
	}
}

/****************************************************************************/
// stopDrag()
// turns off dragging mode.
/****************************************************************************/	
function stopDrag()
{
	mouseover=false
}

/****************************************************************************/
// closeHelpWindow()
/****************************************************************************/	
function closeHelpWindow() {

    var helpscreen = document.getElementById('divHelp');
    var cbTurnOffHelp = document.getElementById('cbDisable');

    helpscreen.style.display = "none";

    //Turn off help windows
    if (cbTurnOffHelp.checked)
        createCookie(helpCookie, 'false', 1000); // name,value,days 

    // show select boxes
    var sel = document.getElementsByTagName("SELECT");
    for (i = 0; i < sel.length; i++) {
        if (sel[i].style.visibility != "visible") { sel[i].style.visibility = "visible"; }
    }
}

/****************************************************************************/
// PART 2 - COOKIE FUNCTIONS
/****************************************************************************/

//Help cookie - The user can turn off the help screens and vise versa
var helpCookie = 'toggleHelp' ;
var cookieHelpMenu='HelpMenu';

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i<ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

/*Tree utils  */
var maxGroups = 100;

function readCookies()
{
	if (readCookie(cookieHelpMenu) != null)
	{
		var objectID=readCookie(cookieHelpMenu);
		var objCat = document.getElementById(objectID+"_items");
		var imgCat=document.getElementById(objectID+"_img_toggle");
		
		objCat.style.display = "block";
		imgCat.src = "/Images/layout/plus_c.gif";
	}
}

function ToggleCategoryItems(id)
{
	var theItemsTr = document.getElementById(id + "_items");
	var toggleImg  = document.getElementById(id + "_img_toggle");
	
	if (theItemsTr.style.display == 'block')
		{
			theItemsTr.style.display = 'none';
			toggleImg.src = '/Images/layout/plus.gif';
			eraseCookie(cookieHelpMenu);
		}			
	else
		{
			theItemsTr.style.display = 'block';	
			toggleImg.src = '/Images/layout/plus_c.gif';
			createCookie(cookieHelpMenu, id, 100);
		}	
}
//Expand or collapse the tree
function ToggleCollapse()
{
	var isCollapsed = document.getElementById('IsCollapsed');
	var imgToggle = document.getElementById('img_ToggleCollapse');
	var divItems ;
	var trCatefory;
	var imgSignPath ;
	var strDisplay;
	
	if (isCollapsed.value == 'true')
		{		
			isCollapsed.value = 'false';
			strDisplay = 'block';
			imgSignPath = '/Images/layout/plus_c.gif' ;	
		}
		else
		{
			isCollapsed.value = 'true';
			strDisplay = 'none';
			imgSignPath = '/Images/layout/plus.gif' ;		
		}
		
		//Toggle the plus sign
		imgToggle.src = imgSignPath;
		//Exapnd or collpase all nodes
		for(i=0; i< maxGroups; i++)
		{
			divItems = document.getElementById('cat' + i+ '_items');
			trCatefory = document.getElementById('cat' + i+ '_img_toggle');
			if (trCatefory != null)
				trCatefory.src = imgSignPath;
			if (divItems != null)
				divItems.style.display = strDisplay;
		}
}

/***************************************************/
//Albums
/***************************************************/

function GetUrlParam( paramName )
{
	var oRegex = new RegExp( '[\?&]' + paramName + '=([^&]+)', 'i' ) ;
	var oMatch = oRegex.exec( window.top.location.search ) ;
	
	if ( oMatch && oMatch.length > 1 )
		return oMatch[1] ;
	else
		return '' ;
}

/***************************************************/
// Forums
/***************************************************/

// delete a specific post
function confirmDeletePost(postId, forumId) {
	if (confirm('?האם אתה בטוח שברצונך למחוק את ההודעה וכל התגובות אליה'))
		location.href='/Manage/Forums_DeletePost.aspx?PostID='+postId+'&ForumID='+forumId;
	
}


function CheckSurvey()
{
	var flag=0; 
	var inputObj = document.getElementsByTagName('input'); 
	for (i=0; i<inputObj.length; i++) 
	{ 
		if (inputObj[i].type == 'radio' && inputObj[i].name.indexOf('rdAnswers') > 0) 
		{
			 if (inputObj[i].checked == true)   flag = 1;  
		}
	 } 
	 if (flag == 0) 
	 {
		var obj = document.getElementById('lblResult'); 
		obj.style.display = 'block';
		return false;
	} 
	else 
		return true;
}

function CheckListChoise (obj)
{
	if ((obj.value == 'rbUsersSkins')) 
	{
		 document.getElementById('sharedList').style.display = 'block';
	}
	else
		 document.getElementById('sharedList').style.display = 'none';
}

/***************************************************/
// CheckCellPhone()
/***************************************************/
function CheckCellPhone()
{
    var ddl = document.getElementById('lst_Code');
    var txt = document.getElementById('txt_Cellephone');
    var lbl =  document.getElementById('lbl_CellPhone');
    var exp = /[0-9]{7}/;
    
    if (txt.value == '' && ddl.value == '0')
    {
        lbl.innerText = '';
        return true;
    } 
       
    if (txt.value == '' && ddl.value != '0')
    {
       lbl.innerText = 'נא להזין מספר בן 7 ספרות';
       return false; 
     }  
        
    if (txt.value != '' && ddl.value == '0')
    {
        lbl.innerText = 'נא לבחור קידומת';
       return false; 
    }
    
    if (txt.value != '' && ddl.value != '0')
    {
       if (!exp.test(txt.value))
       {
            lbl.innerText= 'מספר סלולארי חייב להיות בן 7 ספרות בלבד';
            return false; 
       }
       else
       {
            lbl.innerText = '';                
            return true;
       } 
    }
}

/*
ShowFlash
*/
function ShowFlash (flash)
{
	document.write(flash);
}

/*
write flash 
*/
function writeFlash(flash)
{
	document.write(flash);
}

/*
opens a popup window with a random name
*/
function OpenPopUp(file, width, height)
{
	var maxY = screen.availHeight;
	var maxX = screen.availWidth;  
	var x = (maxX - width) / 2;
	var y = (maxY - height) / 2;
	var winName = 'win_' + Math.floor(Math.random()*9999999999);
    var w = window.open(file,winName,'status=yes,scrollbars=no,width='+width+',height='+height+',screenX='+x+',screenY='+y+',top='+y+',left='+x);
    if (w != null)
	    w.focus();
	    
	return w;
}

/*
Function open popup window
*/
function OpenPopUpScroll(file, width, height)
{
	var maxY = screen.availHeight;
	var maxX = screen.availWidth;  
	var x = (maxX - width) / 2;
	var y = (maxY - height) / 2;
	var w = window.open(file,'','status=yes,scrollbars=yes,width='+width+',height='+height+',screenX='+x+',screenY='+y+',top='+y+',left='+x);
	if (w != null)
		w.focus();

	return w;
}

/*
 * open popup window with name and optional scroll
 * omit scroll parameter to not use scrollbars
 */
function OpenNamePopUp(file, name, width, height, scroll)
{
	var maxY = screen.availHeight;
	var maxX = screen.availWidth;  
	var x = (maxX - width) / 2;
	var y = (maxY - height) / 2;
	var w = window.open(file, name, 'status=yes,scrollbars='+(scroll==''?'no':scroll)+',width='+width+',height='+height+',screenX='+x+',screenY='+y+',top='+y+',left='+x);
	if (w != null)
		w.focus();

	return w;
}

/*
 * popupCalendar(u)
 * pops up the calendar window
 */
function popupCalendar(u, event) {
	var width=243, height=176;
	var x = event.screenX-width-20;
	var y = event.screenY;
	var w = window.open(u,'date_chooser','status=yes,scrollbars=no,width='+width+',height='+height+',screenX='+x+',screenY='+y+',top='+y+',left='+x);
	w.focus();
}

/*Browsercheck object (coolmenus)*/
function cm_bwcheck(){
	//In theory we should use object detection, but this script needs work-arounds for almost every browser...
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent.toLowerCase()
	this.dom=document.getElementById?1:0
	this.ns4=(!this.dom && document.layers)?1:0;
	this.op=window.opera 
	this.moz=(this.agent.indexOf("gecko")>-1 || window.sidebar)
	this.ie=this.agent.indexOf("msie")>-1 && !this.op
	if(this.op){
		this.op5=(this.agent.indexOf("opera 5")>-1 || this.agent.indexOf("opera/5")>-1)
		this.op6=(this.agent.indexOf("opera 6")>-1 || this.agent.indexOf("opera/6")>-1)
		this.op7=this.dom&&!this.op5&&!this.op6 //So all higher opera versions will use it
	}else if(this.moz) this.ns6 = 1
	else if(this.ie){
		this.ie4 = !this.dom && document.all
  	this.ie5 = (this.agent.indexOf("msie 5")>-1)
  	this.ie55 = (this.ie5 && this.agent.indexOf("msie 5.5")>-1)
  	this.ie6 = this.dom && !this.ie4 && !this.ie5 && ! this.ie55
	}
	this.mac=(this.agent.indexOf("mac")>-1)
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.op5 || this.op6 || this.op7)
  this.usedom= this.ns6||this.op7//Use dom creation
  this.reuse = this.ie||this.op7||this.usedom //Reuse layers
  this.px=this.dom&&!this.op5?"px":""
	return this
}
var bw=new cm_bwcheck()

// Get the top & left of an html element
function findPos(objId){
  if(bw.ns4){   //Netscape 4
    x = document.layers.layerMenu.pageX
    y = document.layers.layerMenu.pageY
  }else{ //other browsers
    x=0; y=0; var el,temp
    el = bw.ie4?document.all[objId]:document.getElementById(objId);
    if(el.offsetParent){
      temp = el
      while(temp.offsetParent){ //Looping parent elements to get the offset of them as well
        temp=temp.offsetParent; 
        x+=temp.offsetLeft
        y+=temp.offsetTop;
      }
    }
    x+=el.offsetLeft
    y+=el.offsetTop
  }
  //Returning the x and y as an array
  return [x,y]
}

// utility function that gets the mouse event coords relative to the document
function getMouseCoords(e) {
    var posx = 0;
    var posy = 0;
    if (!e) var e = window.event;
    if (e.pageX || e.pageY) {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY) {
        posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
        posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
    }

    //Returning the x and y as an array
    return [posx, posy];
}

// on catalog item edit screen, chooses between catalog extra options
function setExtraGroupOptions(type, idx, ddlId) {
	var checkbox = document.getElementById('divExCheckbox_' + idx);
	var radio = document.getElementById('divExRadio_' + idx);
	var dropDownList = document.getElementById(ddlId);
	
	if (type == 'radio' || type == 'ddl') {
		radio.style.backgroundColor = '#FFFFAD';
		checkbox.style.backgroundColor = '';
		dropDownList.style.backgroundColor = '#FFFFAD';
	} else {
		radio.style.backgroundColor = '';
		checkbox.style.backgroundColor = '#FFFFAD';
		dropDownList.selectedIndex = 0;
		dropDownList.style.backgroundColor = '';	
	}
}

// selects or unselects all checkboxes on the page
function selectAll(status) {
    var inputs = document.getElementsByTagName("input");
    for (i = 0; i < inputs.length; i++) {
        if (inputs[i].type == "checkbox")
            inputs[i].checked = status;
    }
}

// shows the catalog settings help screen
function toggleCatalogSettingsHelp(screenId) {
    var e = document.getElementById('divCatalogSettingsHelp' + screenId);
    if (e.style.display == 'block')
        e.style.display = 'none';
    else
        e.style.display = 'block';
}


// toggles and element's display
function toggleElementDisplay(elementId) {
    var e = document.getElementById(elementId);
    if (e.style.display == 'block')
        e.style.display = 'none';
    else
        e.style.display = 'block';
}

// adds a function to body onload
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

// adds a function to body onresize
function addResizeEvent(func) {
    var oldonload = window.onresize;
    if (typeof window.onresize != 'function') {
        window.onresize = func;
    } else {
        window.onresize = function() {
            oldonload();
            func();
        }
    }
}

// toggles an element's display and the +/- image of similar ID
function ToggleDisplay(ID) {
    var tableRow = document.getElementById(ID);
    var img = document.getElementById('img_' + ID);

    if (tableRow.style.display == 'none') {
        tableRow.style.display = 'block';
        img.src = "/Images/layout/plus_c.gif";
    }
    else {
        tableRow.style.display = 'none';
        img.src = "/Images/layout/plus.gif";
    }
}