﻿
//
// Copyright (c) 2008  Kivanc Toker - http://www.kivanctoker.com
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing permissions and limitations under the
// License.
//

// Scripts for the window size and centering
window.size = function()
{
	var w = 0;
	var h = 0;

	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))
		{
		
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else
		{
		
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else
	{
	
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
}

window.center = function()
{
	var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};

	var _x = 0;
	var _y = 0;
	var offsetX = 0;
	var offsetY = 0;

	//IE
	if(!window.pageYOffset)
	{
		//strict mode
		if(!(document.documentElement.scrollTop == 0))
		{		
		    offsetY = document.documentElement.scrollTop;
			offsetX = document.documentElement.scrollLeft;
		}
		//quirks mode
		else
		{
			offsetY = document.body.scrollTop;
			offsetX = document.body.scrollLeft;
		}
		
	}
	//w3c
	else
	{
		offsetX = window.pageXOffset;
		offsetY = window.pageYOffset;
		
	}

	_x = ((this.size().width-hWnd.width)/2)+offsetX;
	_y = ((this.size().height-hWnd.height)/2)+offsetY;
	
	return{x:_x,y:_y};
}

// Master class
var Master = {

    highlight       : function(text) {

        // Set the highlight box
        $('#spnHighlight').html(text);

        // Show the highlight box
        $('#divHighlight').show();

    }

}

// Scripts for the pop-up windows
function ShowPopUp(obj, offsetY, offsetX){
    
    // Get the object
	var el = document.getElementById(obj);	
	
	// Get the content div
	var contentDiv = $get('ContentDiv');
	
	if (contentDiv != null){
	
	    // Get the location of the content div
	    var locContentDiv = Sys.UI.DomElement.getLocation(contentDiv);

	    // Set the position
	    el.style.position = 'absolute';	
	    el.style.left = ((window.center().x - locContentDiv.x) + offsetX) + 'px';
	    el.style.top = ((window.center().y - locContentDiv.y) + offsetY) + 'px';	    
	
	}else{
	    
	    el.style.position = 'absolute';	
	    el.style.left = ((window.center().x) + offsetX) + 'px';
	    el.style.top = ((window.center().y) + offsetY) + 'px';
	}
	
	// Show the pop up
	el.style.display = 'block';
			
}

function HidePopUp(obj){

    // Get the object
	var el = document.getElementById(obj);
	
	// Show the pop up
	el.style.display = 'none'

}

// Shared scripts for the user interface

function SwitchMenu(obj){
	
	var el = document.getElementById(obj);
		
	if(el.style.visibility == 'hidden'){
		
		el.style.visibility = 'visible';
	
	}else{
	
		el.style.visibility = 'hidden';
	
	}				
}

function SwitchMenu2(obj){
       
       var el = document.getElementById(obj);
             
       if(el.style.visibility == 'hidden'){
             
             el.style.visibility = 'visible';
             el.style.display = 'block';
       
       }else{
       
             el.style.visibility = 'hidden';
           el.style.display = 'none';
       }                          
}

function SlideDown(elementName, buttonToChange){

    var el = document.getElementById(elementName);
    
    if (el.style.display == 'none'){	
		
		Effect.SlideDown(elementName, {duration: 0.5});
		
		if (buttonToChange != null){
            
            buttonToChange.src = '/Images/Icons/minusButton.gif';            
        }
    }       
}

function SlideUp(elementName, buttonToChange){

    var el = document.getElementById(elementName);
    
     if(el.style.display == ''){		
	
		Effect.SlideUp(elementName, {duration: 0.5});
		
		if (buttonToChange != null){
            
            buttonToChange.src = '/Images/Icons/plusButton.gif';            
        }
		
    }   
}

function SlideItem(elementName, buttonToChange){

    var el = document.getElementById(elementName);
    
    if(el.style.display == ''){		
	
		Effect.SlideUp(elementName, {duration: 0.5});
		
		if (buttonToChange != null){
            
            buttonToChange.src = '/Images/Icons/plusButton.gif';            
        }
		
    }else if (el.style.display == 'none'){	
		
		Effect.SlideDown(elementName, {duration: 0.5});
		
		if (buttonToChange != null){
            
            buttonToChange.src = '/Images/Icons/minusButton.gif';            
        }
    }       
}

function SlideHorizontal(eventSource, elementName, direction, pixels, limit){

    var el = document.getElementById(elementName);
    
    if (direction == 'r') {
    
    }else{ 
    
        pixels *= -1;
    
    }
       	
	new Effect.Move(el, { x: pixels, y: 0 }); 

}

function SlideVertical(eventSource, elementName, direction, pixels, limit){

    // Get the element to slid
    var el = document.getElementById(elementName);
   
    if (direction == 'd') {
    
    }else{ 
    
        pixels *= -1;
    
    }   
    // Move the element
    new Effect.Move(el, { x: 0, y: pixels });   

}


function ToggleItem(target)
  {
      if ($(target).is(":hidden")) {
          $(target).slideDown("slow");
      }
      else {
          $(target).slideUp();
      }

    return true;
  };
  
  function SlideItem(target,way,container,speed)
  {
    var position = $(target).position();
    
    var height_item = $(target).height();
    var width_item = $(target).width();
    
    var height_cont = $(container).height();
    var width_cont = $(container).width();
    
    if(way=='left')
    {
        if(position.left<=0)
            $(target).animate({"left": "+="+speed+"px"},20, function(){SlideItem(target,way,container,speed);});
    }
    else if(way=='right')
    {
        if(position.left+width_item>=width_cont)
            $(target).animate({"left": "-="+speed+"px"}, 20, function(){SlideItem(target,way,container,speed);});
    }
    else if(way=='up')
    {
        if(position.top<=0)
            $(target).animate({"top": "+="+speed+"px"}, 20, function(){SlideItem(target,way,container,speed);});
    }
    else if(way=='down')
    {
        if(position.top+height_item>=height_cont)
            $(target).animate({"top": "-="+speed+"px"}, 20, function(){SlideItem(target,way,container,speed);});
    }
    else
    {
        
    }
  };
  
  function StopItem(target)
  {
    $(target).stop();
  };
      
function checkRequiredSelection(obj){
	
	var radioList = document.getElementsByName(obj);
	
	var error = 1;
	for (var i = 0; i < radioList.length; i++) {
	   if (radioList[i].checked){
		    error = 0;		    
		}
	}
	
	if (error == 1)	{
	
	    alert('You did not choose any entry!');
	
	    return false;
	
	}
	
	return true;
	
}

function isNumeric(sText)
{
   var ValidChars = "0123456789.,";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   
   return IsNumber;
   
}


function addListItem(fromListName, toListName) {

	var fromListElement = document.getElementById(fromListName);
	var toListElement = document.getElementById(toListName);
	
	var error = 1;
	for (var i = 0; i < fromListElement.length; i++) {
		if (fromListElement.options[i].selected) error = 0;
	}
	if (error == 0) {
	
	    var removedIndexes = '';
	
	    for (var i=0; i < fromListElement.length; i++){
	    
	        if (fromListElement.options[i].selected){
	        
	            var itemName = fromListElement.options[i].text;
		        var itemID = fromListElement.options[i].value;
		        var isFound = false;
		        
		        if (toListElement.length != 0) {
			        for(var j=0; j < toListElement.length; j++) {
				        if (toListElement.options[j].text == itemName || toListElement.options[j].value == itemID) isFound = true;
			        }
		        }
		        
		        if (!isFound) {
			        toListElement.length++;
			        toListElement.options[toListElement.length-1].text = itemName;
			        toListElement.options[toListElement.length-1].value = itemID;
		        }
		        
		        // Add the item to the list of items to be removed
		        if (removedIndexes != '') removedIndexes += ',';
		        removedIndexes += i.toString();
	        }
	    }
	    
	    // Remove the items from the source list
	    var itemsToRemove = removedIndexes.split(',');
	    for (var j = 0; j < itemsToRemove.length; j++){
	        fromListElement.remove(fromListElement.options[itemsToRemove[j]]);	     
	    }
	}
}
		
function removeListItem(listName) {

	var listElement = document.getElementById(listName);
	
	var error = 1;
	for (var i = 0; i < listElement.length; i++) {
		if (listElement.options[i].selected) error = 0;
	}
	
	if (error == 0) {
        for (var i=0; i < listElement.length; i++){
            if (listElement.options[i].selected){
                   
                listElement.remove(i);    
            }
        }
	}
	
}
		
function selectAllItems(listName) {

	var listElement = document.getElementById(listName);
	
	for (var i = 0; i < listElement.length; i++) {
		listElement.options[i].selected = true;
	}
}

function invertThisChkBox(obj)
{
    var item;
    item=document.getElementById(obj);
    item.checked=!item.checked;
}


function invertAllChkBox()
{
    var chks = new Array();
    chks=document.getElementsByName("chkDelete");
    if(checkAllChkBox()==1)
    {
        deSelectAllChkBox()
    }
    else if(checkAllChkBox()==0)
    {
        selectAllChkBox()
    }
}


function deSelectAllChkBox()
{
    var chks = new Array();
    chks=document.getElementsByName("chkDelete");
    for(var i=0; i<chks.length; i++)
    {
        if(chks[i].checked==true)
            chks[i].checked=false;
    }
}


function selectAllChkBox()
{
    var chks = new Array();
    chks=document.getElementsByName("chkDelete");
    for(var i=0; i<chks.length; i++)
    {
        if(chks[i].checked==false)
            chks[i].checked=true;
    }
}


function checkAllChkBox()
{
    var chks = new Array();
    chks=document.getElementsByName("chkDelete");
    var ok = 1;
    for(var i=0; i<chks.length; i++)
    {
        if(chks[i].checked==false)
            ok=0;
    }
    return ok;
}

// ACTIVATION

function fillCuisineList(cuisineString){

    // Create the main cuisines array
    var cuisineInformationArray = cuisineString.split(',');
    
    // Get the cuisine options
    var cuisineOptions = document.getElementsByName('Cuisines');
    
    for(var i=0; i < cuisineInformationArray.length ; i++){
        for(var j=0; j < cuisineOptions.length; j++){         
            if (cuisineInformationArray[i] == cuisineOptions[j].value){
                cuisineOptions[j].checked = true;
            }
        }
    }        
}

function showSubCuisines(id){
	
	// Hide the other lists
	for (var i=1; i < 9; i++){
	    if (i != id) document.getElementById('subMenu_' + i).style.visibility = 'hidden';	
	}
	
	// Show the actual list
	if (id != 0){
	    document.getElementById('subMenu_' + id).style.visibility = 'visible';
	}			
}


// EVENT SECTION

function fillMemberList(memberListString){

    var friendList = document.getElementById('friendList');
    
    // Create the main memberLists array
    var memberListArray = memberListString.split('#');
    
    for(var i = 0; i < memberListArray.length; i++)
    {   
        var subMemberListArray = memberListArray[i].split(';');
        
        if (memberListArray[i] != '' && subMemberListArray[i] != '')
        {
            // Create a new option element
            var option = document.createElement('OPTION');
            option.text = subMemberListArray[0];
            option.value = subMemberListArray[1];
            
            // Add the option element to the list
            try {
                friendList.add(option, null); // standards compliant; doesn't work in IE
            }
            catch(ex) {
                friendList.add(option); // IE only
            } 
        }     
    }        
}

function saveInvitedFriendList(hdnInvitedPeople){

    // Create a place holder for the ingredient input
    var invited = '';

    /// Get the cuisine list objects
    var invitedFriendList = document.getElementById('invitedFriendList');
    
    // Get the selected main cuisines
    for(var i = 0; i < invitedFriendList.options.length; i++)
    {
        invited += invitedFriendList.options[i].value + ',';
    }
    
    // Save the invited members to the hidden field
    hdnInvitedPeople.value = invited.substring(0, invited.length - 1);
}

// AJAX Scripts for the user interface
function onAjaxRequestFailed(error){

    alert('Stack trace:' + error.get_stackTrace() + '/r/n' + error.get_message() + '/r/n' + error.get_exceptionType());

}

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
