// -----------------------------------------------------------------------------
// Script:  SystemButton.js
// Project : NPD/5994 Imperative, Imperative Health Limited.
// Copyright ©2006 Imperative Health Limited. All rights reserved. 
//	
// Description:
//     function to change/restore appearance of button "onclick"
//
//  Modification History
//
//  Version		Date				By			Description
//  V1R1M1      18/12/2006          GROJ        Created
//  V1R1M2      03/01/2006          GROJ        Button fires only one postback when enabled
//                                              and does no postback when disabled                                                                                                       
//-------------------------------------------------------------------------------



// using in a lot of places for creating round corners

// -----------------------------------------------------------------------------
// Description: 
//		This rounds the corners of an object
//   
// IN:	btn             Button object	
// OUT: none
// -----------------------------------------------------------------------------
function roundCorners(btn)  {
     
    settings = {
    tl: { radius: 6 },
    tr: { radius: 6 },
    bl: { radius: 6 },
    br: { radius: 6 },
    antiAlias: true,
    autoPad: true,
    validTags: ['div']  
    }
    var myBoxObject = new curvyCorners(settings, btn); 
    myBoxObject.applyCornersToAll();    
}


// using in excercise paln control - should be moved to its control scripts

// -----------------------------------------------------------------------------
// Description: 
//		This sets the properties of an AJAX button to that of a clicked button
//   
// IN:	btnId               Button object ID
// OUT: none
// -----------------------------------------------------------------------------
function AjaxActivitySystemButtonClicked (btnId) {
    btn=findElementById(btnId+'SystemButtonPanel');
    linkbtn=findElementById(btnId+'Linkbutton');
    //Disable button to avoid registring multiple clicks
    //add activity to user's activity list
    AutoComplete_AddActivity();
    //systemButtonNormal(btn.id,btnId+'Linkbutton',btnId+'SystemButtonPanelLeft',btnId+'SystemButtonPanelRight',true)
}






// using in solution system button and in habit system button - should be removed 

// -----------------------------------------------------------------------------
// Description: 
//		This hides the non-JS button when javascript is enabled.
//   
// IN:	btnClass               Button object ID
// OUT: none
// -----------------------------------------------------------------------------
function systemButtonHideNonJSButton(btnClass) {

    var btn = getElementsByClass(btnClass);
    for(var i = 0, j = btn.length; i < j; i++)
    {
      btn[i].className='hidden';
    }
}

// -----------------------------------------------------------------------------
// Description: 
//		This sets the properties of a button to that of a clicked button
//      Used only in the habit Solution Selector.
//   
// IN:	btnId               Button object ID
// IN:  eventArgs           The event Arguments
// OUT: none
// -----------------------------------------------------------------------------
function systemButtonClickedHabit (btnId, eventArgs) {

 btn=findElementById(btnId);
 //Only do something if the system button is a Linkbutton
 var qualified_id = btn.id;
  
    //Set Linkbutton id for postback
    var qualified_id = btn.id;
    //Replace underscores with $
    while (qualified_id.indexOf("_")>=0)
    {
	   qualified_id=qualified_id.replace("_","$");
	}
	
	if(eventArgs != null)
	{
	    //Disable button for further clicks and do the postback, with args
        __doPostBack(qualified_id,eventArgs);
	}
	else
	{
        //Disable button for further clicks and do the postback
        __doPostBack(qualified_id,'');
    }
}

// -----------------------------------------------------------------------------
// Description: 
//		This sets the properties of a button to that of a clicked button
//      Used only in the Solution Selector.
//   
// IN:	btnId               Button object ID
// IN:  eventArgs           The event Arguments
// OUT: none
// -----------------------------------------------------------------------------
function systemButtonClickedSolution (btnId, eventArgs) {

 btn=findElementById(btnId);
 //Only do something if the system button is a Linkbutton
 var qualified_id = btn.id;
 
    //Set Linkbutton id for postback
    var qualified_id = btn.id;
    //Replace underscores with $
    while (qualified_id.indexOf("_")>=0)
    {
	   qualified_id=qualified_id.replace("_","$");
	}
	
	if(eventArgs != null)
	{
	    //Disable button for further clicks and do the postback, with args
        __doPostBack(qualified_id,eventArgs);
	}
	else
	{
        //Disable button for further clicks and do the postback
        __doPostBack(qualified_id,'');
    }
}

// -----------------------------------------------------------------------------
// Description: 
//		This sets the properties of a button to that of a clicked button
//   
// IN:	btnId               Button object ID
// IN:  lnkBtnId            The link button ID
// IN:  leftPanel           The left curve of the button
// IN:  rightPanel          The right curve of the button
// IN:  eventArgs           The event Arguments
// OUT: none
// -----------------------------------------------------------------------------
function systemButtonClicked (btnId, lnkbtnId, leftPanel, rightPanel, eventArgs) {

 var btn=findElementById(btnId);  
 //Only do something if the system button is enabled
 var qualified_id = lnkbtnId;
 var lnkButton = findElementById(qualified_id);
 var leftCurve = findElementById(leftPanel);
 var rightCurve = findElementById(rightPanel);  
 
 var allowValidation = lnkButton.getAttribute("ShouldValidate");
 
 //Check if input validators are defined on this page
 var clientValidation = true;
 if (typeof(Page_ClientValidate) == 'function')
 { 
    if(allowValidation != null)
    {
        if(allowValidation)
        {
            clientValidation=Page_ClientValidate(); //If input validator is present, check state of page
        }
    }
 }
 //check for profile validation flag
 var profileValidation  = true;  
 if (typeof(profileValidationFlag) == 'boolean')
 { 
        if(profileValidationFlag == true)
        {
            profileValidation = ValidateProfile(qualified_id, eventArgs, btnId, lnkbtnId, leftPanel, rightPanel); 
        }
 }  
 
 //only postback if all validation ok
 var pageIsValid = clientValidation && profileValidation;

 
 if ((isClassInArray(btn.className, systemButtonDownClassNames) || btn.className=='menubutton')&&pageIsValid)
 {    
   var lnkButton = findElementById(lnkbtnId);
   
   //check if is sytembutton or menubutton
   if (isClassInArray(btn.className,systemButtonDownClassNames))
   {    
        lnkButton.className = 'buttonTextDisabled';            
        //roundCorners(btn);  
   }           
   
    //Replace underscores with $
    while (qualified_id.indexOf("_")>=0)
    {
	   qualified_id=qualified_id.replace("_","$");
	}
	
	if(eventArgs != null)
	{
	    //Disable button for further clicks and do the postback, with args
	   // __doPostBack(qualified_id,eventArgs);
        window.location.href = "javascript:__doPostBack('" + qualified_id + "','" + eventArgs + "')";
	}
	else
	{
        //Disable button for further clicks and do the postback
        //__doPostBack(qualified_id,'');
        window.location.href = "javascript:__doPostBack('" + qualified_id + "','')";

    }
 } 
 return true; 
}

// -----------------------------------------------------------------------------
// Description: 
//		This sets the properties of a button to that of a button being hovered over
//   
// IN:	btnId               Button object ID
// IN:  lnkBtnId            The link button ID
// IN:  leftPanel           The left curve of the button
// IN:  rightPanel          The right curve of the button
// OUT: none
// -----------------------------------------------------------------------------
function systemButtonHover (btnId, lnkBtnId, leftPanel, rightPanel) {
 
    var btn=findElementById(btnId);
    var lnkButton = findElementById(lnkBtnId);  
    var leftCurve = findElementById(leftPanel);
    var rightCurve = findElementById(rightPanel); 

    if (isClassInArray(btn.className,systemButtonClassNames)) {
         
            btn.className='buttonClicked';  
            lnkButton.className='buttonTextClicked';
            leftCurve.className='systemButtonLeftClicked';
            rightCurve.className='systemButtonRightClicked';
            lnkButton.blur();             
    }
    else if (isClassInArray(btn.className,systemButtonDownClassNames)) {

            btn.className='buttonClicked';  
            lnkButton.className='buttonTextClicked';
            leftCurve.className='systemButtonLeftClicked';
            rightCurve.className='systemButtonRightClicked';
            lnkButton.blur();              
    }      
} 

// -----------------------------------------------------------------------------
// Description: 
//		This sets the properties of a button to that of an normal, unclicked button
//   
// IN:	btnId               Button object ID
// IN:  lnkBtnId            The link button ID
// IN:  leftPanel           The left curve of the button
// IN:  rightPanel          The right curve of the button
// IN:  forceRedraw         Bool that forces the redraw.
// OUT: none
// -----------------------------------------------------------------------------
function systemButtonNormal (btnId, lnkBtnId, leftPanel, rightPanel,forceRedraw) {

 
  //Only do something if the btn is not displayed as a normal
 //button and if the Linkbutton has been disabled

        var btn=findElementById(btnId);
        var leftCurve = findElementById(leftPanel);
        var rightCurve = findElementById(rightPanel);
        
        if(btn.className == 'buttonClicked') {
                   
                setSystemButtonNormalClass(btn);;
                var lnkButton = findElementById(lnkBtnId);    
                lnkButton.className = 'buttonTextNormal';
                leftCurve.className='systemButtonLeft';
                rightCurve.className='systemButtonRight'; 
           
        }
        else if (isClassInArray(btn.className, systemButtonDownClassNames)) {
        
                // Button down      
                var lnkButton = findElementById(lnkBtnId);    
                lnkButton.className = 'buttonTextNormal';
                leftCurve.className='systemButtonLeft';
                rightCurve.className='systemButtonRight';   
   
        }
 }
 
// -----------------------------------------------------------------------------
// Description: 
//		Determines whether a CSS class is in an array of CSS classes.
//   
// IN:	className           CSS class name
// IN:  classNameArray      array of class names
// OUT: none
// -----------------------------------------------------------------------------
function isClassInArray(className, classNameArray) {

    for (index in classNameArray) {
        if (className == classNameArray[index]) {
            return true;
        }
    }
    
    return false;
}

// -----------------------------------------------------------------------------
// Description: 
//      Variables containing some CSS information for the system button. 
//      These exist so that they can be changed for certain pages. 
//   
// -----------------------------------------------------------------------------
var systemButtonNormalClass = 'button';
var systemButtonClassNames = [systemButtonNormalClass];
var systemButtonDownClass = 'buttonDown';
var systemButtonDownClassNames = [systemButtonDownClass];

// -----------------------------------------------------------------------------
// Description: 
//		Sets the normal class for a system button. This method exists so that
//      it can be changed for certain pages. 
//   
// IN:	btn           A system button object
// OUT: none
// -----------------------------------------------------------------------------
function setSystemButtonNormalClass(btn) {
    btn.className = systemButtonNormalClass;
}

// -----------------------------------------------------------------------------
// Description: 
//		Sets the down class for a system button. This method exists so that
//      it can be changed for certain pages. 
//   
// IN:	btn           A system button object
// OUT: none
// -----------------------------------------------------------------------------
function setSystemButtonDownClass(btn) {
    btn.className = systemButtonDownClass;
}

