
function callbackAjaxError(result)
{
    var message = "There was an error with the remote call : \n" + 
        result._exceptionType + 
        result._message +
        result._stackTrace +
        "\nTime Out: " + result._timedOut;
    window_onerror(message, "0", "0")
}

var _myCakeObject = new cakeObject();

//Cake object to store the selected options of the cake and the cake design
function cakeObject()
{   
    this.C2  = null;
    this.C4  = null;
    this.C5  = null;
    this.C6  = null;
    this.KI  = null;
    this.ED  = null;
    this.FC  = null;
    this.IC  = null;
    this.cakeInscriptionText = null;

}

//Changes the back color of the options and saves the selected option value
function changebackcolor(labelId)
{
   var splitAttribute = labelId.id.split('^^');
   
   if(_myCakeObject[splitAttribute[0]] !=null)
   {
        var previousElementId = splitAttribute[0]+'^^'+_myCakeObject[splitAttribute[0]];
        var previousElement   = document.getElementById(previousElementId);
        previousElement.style.backgroundColor='White';
        _myCakeObject[splitAttribute[0]] = splitAttribute[1];
        labelId.style.backgroundColor='#faf4de';
    }
    else
    {
        _myCakeObject[splitAttribute[0]] = splitAttribute[1];
        labelId.style.backgroundColor='#faf4de';
    
    }
   
   
}

//Adjust the frame height

var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
var FFextraHeight=getFFVersion>=0.1? 16 : 0 //extra height in px to add to iframe in FireFox 1.0+ browsers


function resizeIframe(frameid)
{
    var currentfr=document.getElementById(frameid)
    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;
       
        if (currentfr.addEventListener)
        {
                    currentfr.addEventListener("load", readjustIframe, false)
        }
        else if (currentfr.attachEvent)
        {
            currentfr.detachEvent("onload", readjustIframe) // Bug fix line
            currentfr.attachEvent("onload", readjustIframe)
        }
   }
}

function readjustIframe(loadevt)
{
    var crossevt=(window.event)? event : loadevt
    var iframeroot=(crossevt.currentTarget)? crossevt.currentTarget : crossevt.srcElement
    if (iframeroot)
    {
        resizeIframe(iframeroot.id);
    }
}

function loadintoIframe(iframeid, url)
{
    if (document.getElementById)
        document.getElementById(iframeid).src=url
}


//Initialize the element and add the window handle
function init(elementId)
{   
     var toolbar = document.getElementById(elementId);
     addHandle(toolbar, window);
  
}     

//Display the iframe which displays the cake options
function showCakeDiv(elementId,frameSource)
{
    var frame = document.getElementById(elementId);
    frame.src = _geApplicationRoot + frameSource; 
//    '/Cakes/ChooseCakeOptions.aspx?contentId='+ contentId;

    var top = undefined;
    
    if (document.all) {
        if (event != null) {
            top = document.body.scrollTop + event.clientY;
        }
        else {
            top = parseInt(screen.availHeight / 2);
        }
    }
    else {
        if (event != null) {
            top = event.clientY;
        }
        else {
            top = parseInt(screen.availHeight / 2);
        }
    }
    
    var bottom = top + parseInt(frame.height);
    var screenBottom = document.body.scrollTop + parseInt(document.body.clientHeight);
    if (bottom > screenBottom)
    {
        top -= ((bottom - screenBottom) + 10);
    }
    frame.style.top = top;
    frame.style.left = (parseInt(screen.availWidth)/2) - 200;    
    frame.style.visibility = 'visible';
}

//Close the cakeoptions iframe
function closeCakeDiv(elementId)
{
  var frame = document.getElementById(elementId);
  frame.style.visibility = "hidden";    

}

//Save the selected options and  close the iframe
function closeCakeDivandRedirect()
{
   GiantEagle.Applications.InternetSites.Web.Services.CakeService.SaveCakeSelection(_myCakeObject.C2,_myCakeObject.C4,_myCakeObject.C5,_myCakeObject.C6,callbackcloseCakeDivandRedirect,callbackAjaxError);
}

//Call back function for the above
function callbackcloseCakeDivandRedirect(result)
{
    if(result.OperationSucceeded)
    {
     //Redirect to the next page
       parent.location.href= _geApplicationRoot+'/Cakes/ChooseCakeDesign.aspx';
    }
    else
    {
      //Display an error message
       alert('Sorry...There was an error saving the cake selection please submit the choices again\n');
    }

}

//Save the cake design options 
function closeCakeDesignDivandRedirect()
{
  var cakeInscription = document.getElementById('cakeInscriptionText');
    
  _myCakeObject.cakeInscriptionText = cakeInscription.value;
 
  GiantEagle.Applications.InternetSites.Web.Services.CakeService.SaveCakeDesignSelection(_myCakeObject.cakeInscriptionText,
                        _myCakeObject.KI,_myCakeObject.FC,_myCakeObject.ED,
                        _myCakeObject.IC,callbackcloseCakeDesignDivandRedirect,callbackAjaxError);
}

//Call back function after saving the design options
function callbackcloseCakeDesignDivandRedirect(result)
{
    if(result.OperationSucceeded)
    {
     //Redirect to the next page
       parent.location.href = _geApplicationRoot+'/Cakes/OrderCake.aspx';
    }
    else
    {
      //Display an error message
       alert('Sorry...There was an error saving the cake selection please submit the choices again\n');
    }

}

function FormatTextBoxName(textBoxElement)
{
    if(textBoxElement.value == 'Enter Text')
    {
       textBoxElement.value = '';
    }   
}


//var _previewMediaOff = true;

//function previewOff()
//{
//    if (_previewMediaOff)
//    {
//        document.getElementById('SelectCake').style.display = 'none';
//    }
//}

//function stub()
//{

//}
////This function previews the div at the element click 
//function previewCakeDiv(showHide, elementName, contentId, attributeId, imagePath, cakeImageElement) 
//{

//    var previewElement = document.getElementById('SelectCake');
//    if (!showHide)
//    {
//        _previewMediaOff = true;
//        return;
//    }
//  
//    var  nameDiv = document.getElementById('divcakename');
//    nameDiv.innerHTML = elementName+ ' Cake Details';
//    
//    _myCakeObject.cakeContentId = contentId;
//    _myCakeObject.cakeName = elementName;
//    _myCakeObject.cakeImagePath = imagePath;
//    _myCakeObject.cakeAttributeId = attributeId;
//           
//    setTimeout('stub()', 100);      
//    GiantEagle.Applications.InternetSites.Web.Services.CakeService.GetCakeDesc(contentId,callbackCakeDesc,callbackAjaxError);
//    setTimeout('stub()', 100);  
//    GiantEagle.Applications.InternetSites.Web.Services.CakeService.GetDomainValues(contentId,attributeId,'C2',callbackDomainValues,callbackAjaxError,'CakeSizeDiv');
//    setTimeout('stub()', 100);  
//    GiantEagle.Applications.InternetSites.Web.Services.CakeService.GetDomainValues(contentId,attributeId,'C4',callbackDomainValues,callbackAjaxError,'CakeFlavorsDiv');
//    setTimeout('stub()', 100);  
//    GiantEagle.Applications.InternetSites.Web.Services.CakeService.GetDomainValues(contentId,attributeId,'C5',callbackDomainValues,callbackAjaxError,'CakeIcingDiv');
//    setTimeout('stub()', 100);  
//    GiantEagle.Applications.InternetSites.Web.Services.CakeService.GetDomainValues(contentId,attributeId,'C6',callbackDomainValues,callbackAjaxError,'CakeFillingsDiv');
//    
//    var cakeImageDiv = document.getElementById('divcakeimage');
//    cakeImageDiv.src = imagePath;
//    
//    var test = document.getElementById(cakeImageElement);
//    
//    var clickX = clickY = 0;
//	if (test.offsetParent) 
//	{
//		clickX = test.offsetLeft
//		clickY = test.offsetTop
//		while (test = test.offsetParent)
//	    {
//			clickX += test.offsetLeft
//			clickY += test.offsetTop
//		}

//	}
//   var ClientX = clickX;
//   var ClientY = clickY;
//   
//   previewElement.style.left = 350 +'px';
//   previewElement.style.top = ClientY  ;

//    
//    if (!_previewMediaOff)
//    {
//        _previewMediaOff = false;
//        showPreviewMedia();
//    }
//    else
//    {
//        _previewMediaOff = false;
//        setTimeout('showPreviewMedia()', 500);  
//    }    
//}

//function callbackCakeDesc(result)
//{
//    if (result.OperationSucceeded)
//    {
//         var descDiv = document.getElementById('cakedesc');    
//         descDiv.innerHTML = result.ClientData; 
//         
//    }

//}

//function callbackDomainValues(result,divElement)
//{
//    if (result.OperationSucceeded)
//    {
//         var descDiv = document.getElementById(divElement); 
//         if(result.ClientData !=null)
//         {
//         
//             var subAttributes = result.ClientData.split('$$');
//             
//             var html = '';
//             for(var i=0;i<subAttributes.length;i++)
//             { 
//                var values = subAttributes[i].split('~~');
//                
//                html += '<div id=\''+values[0]+'^^'+values[1]+'\' style=\'height:25px;cursor:pointer;\' onclick=\'changebackcolor(this)\' >';
//                html += values[1]+'</div>';
//             }
//               
//             descDiv.innerHTML = html;
//        }   

//    }

//}


//function showPreviewMedia()
//{
//    if (_previewMediaOff)
//    {
//        return;
//    }
//    var previewElement = document.getElementById('SelectCake');
//    previewElement.style.display = 'block';
//}

//function previewPaneMouseOver()
//{
//    _previewMediaOff = false;
//}
//  
//function closeDiv(elementId)
//{
//    var previewElement = document.getElementById(elementId);
//    previewElement.style.display = 'none';

//}

//function saveCakeSelection()
//{     
//    GiantEagle.Applications.InternetSites.Web.Services.CakeService.SaveCakeSelection(_myCakeObject.C2,_myCakeObject.C4,_myCakeObject.C5,_myCakeObject.C6,callbackSaveSelection,callbackAjaxError);
//}

//function callbackSaveSelection(result)
//{
//    if (!result.OperationSucceeded)
//    {
//         alert('Sorry...There was an error saving the cake selection please submit the choices again\n');
//    }
//}


/* Add window.event to Firefox  */

if(!window.event && window.captureEvents) {
  // set up event capturing for mouse events (add or subtract as desired)
  window.captureEvents(Event.MOUSEOVER|Event.MOUSEOUT|Event.CLICK|Event.DBLCLICK);
  // set window event handlers (add or subtract as desired)
  window.onmouseover = WM_getCursorHandler;
  window.onmouseout = WM_getCursorHandler;
  window.onclick = WM_getCursorHandler;
  window.ondblclick = WM_getCursorHandler;
  // create an object to store the event properties 
  window.event = new Object;
}
function WM_getCursorHandler(e) {
  // set event properties to global vars (add or subtract as desired)
  window.event.clientX = e.pageX;
  window.event.clientY = e.pageY;
  window.event.x = e.layerX;
  window.event.y = e.layerY;
  window.event.screenX = e.screenX;
  window.event.screenY = e.screenY;
  // route the event back to the intended function
  /*if ( routeEvent(e) == false ) {
    return false;
  } else {
    return true;
  }*/
}

