//This is for disable the document.write in the API render function
var kasamba_SB_Disable_Render='1';

function PageLoad_SearchBoxStep1 (ddlID)
{
    try
    {
        var ddl = document.getElementById(ddlID);
        if (ddl.value == "-1")
        { 
            document.getElementById("divHeaderTextBox").style.display = "inline";  
            document.getElementById("divModifyFont").style.display = "none";  
            document.getElementById("divModifyFontCtrl").style.display = "none";
        }
    }
    catch(e)
    {
        WriteErrorLog(e, "PageLoad_SearchBoxStep1");
    }
}

function SelectedHeaderChange(ddl)
{
    try
    {
        if (ddl.value == "-1")
        {
            document.getElementById("divHeaderTextBox").style.display = "inline";  
            document.getElementById("divModifyFont").style.display = "none";  
            document.getElementById("divModifyFontCtrl").style.display = "none";
            kasamba_SB_header = "";
        }
        else
        {
            document.getElementById("divHeaderTextBox").style.display = "none";  
            document.getElementById("divModifyFont").style.display = "inline"; 
            document.getElementById("divModifyFontCtrl").style.display = "none"; 
            kasamba_SB_header = ddl.value;
            SBRender();
        }
    }
    catch(e)
    {
        WriteErrorLog(e, "SelectedHeaderChange");
    }
}

function ModifyFontClick (obj)
{
    try
    {
        if (document.getElementById("divModifyFontCtrl").style.display=="none")   
        {
            obj.innerHTML = "Default Font";
            document.getElementById("divModifyFontCtrl").style.display="inline";   
        }
        else
        {
            obj.innerHTML = "Modify Font";
            document.getElementById("divModifyFontCtrl").style.display="none";
            kasamba_SB_headerFontFamily = kasamba_SB_def_headerFontFamily;
            kasamba_SB_headerFontSize = kasamba_SB_def_headerFontSize;
            kasamba_SB_headerBold = "normal";
            kasamba_SB_headerItalic = "normal";
            kasamba_SB_headerUnderline = "none";
        }
        UpdateFontControl();
        SBRender();
    }
    catch(e)
    {
        WriteErrorLog(e, "ModifyFontClick");
    }
}

function IsHeaderTextEmpty(sender, args)
{
    try
    {
        var result = true;
        var text = Trim(args.Value);
        if (document.getElementById("divHeaderTextBox").style.display!="none")   
        {
            if ((text == "Type your text here...") || (text ==""))
            {
                result= false;
                sender.innerHTML= "<br/>No text has been entered.<br/>Please type your customized<br/> text or choose one of proposed<br/> Headers in the list above.";
            }
            else
            {
                if (!ValidateText(text))
                {
                    result= false;
                    sender.innerHTML="One or more characters<br/>you have typed are forbidden to use,<br/>please remove them and try again.";
                }  
            }
        }
        args.IsValid = result;
    }
    catch(e)
    {
        WriteErrorLog(e, "IsHeaderTextEmpty");
        return false;
    }
}

function ValidateText(text)
{
    try
    {   
        //Check forbidden Chars
        var forbiddenChars = new Array('>','<','&');
        for (i=0;i<forbiddenChars.length;i++)
        {
            if (text.indexOf(forbiddenChars[i],0) > -1 )
            {
	            return false;
            }
        }
    }
    catch(e)
    {
        WriteErrorLog(e, "ValidateText");
        return false;
    }
    return true;
}

function SaveToSession(headerText)
{   
    try
    {
        if (Validate(headerText))
        {
            var createHeader = "0";
            if (document.getElementById("divHeaderTextBox").style.display=="inline")
            {
                createHeader = "1";
            }
            document.getElementById("hiddenHeaderText").value = kasamba_SB_header;
            document.getElementById("hiddenHeaderFontFamily").value = kasamba_SB_headerFontFamily;
            document.getElementById("hiddenHeaderFontSize").value = kasamba_SB_headerFontSize;
            document.getElementById("hiddenHeaderBold").value = ""+kasamba_SB_headerBold;
            document.getElementById("hiddenHeaderItalic").value = ""+kasamba_SB_headerItalic;
            document.getElementById("hiddenHeaderUnderline").value = ""+kasamba_SB_headerUnderline;
            document.getElementById("hiddenTextBoxLenght").value = ""+kasamba_SB_textBoxLenght;
            document.getElementById("hiddenBorderColor").value = kasamba_SB_borderColor;
            document.getElementById("hiddenBackgroundColor").value = kasamba_SB_backgroundColor;
            document.getElementById("hiddenTextColor").value = kasamba_SB_textColor;
            document.getElementById("hiddenButtonColor").value = kasamba_SB_buttonColor;
            document.getElementById("hiddenCreateHeader").value = createHeader;
           
            return true;
        }
    }
    catch(e)
    {
        WriteErrorLog(e, "SaveToSession");
    }
    return false;
}

function SBRender()
{
    try
    {
        Validate();
        var outputDiv = document.getElementById("OutputSearchBox");
        
        //Call the kasamba API SBRender function
        outputDiv.innerHTML =  kasamba_SB_Render();
        
        var size = outputDiv.offsetWidth + " X " + outputDiv.offsetHeight;
        document.getElementById("BannerSizeTop").innerHTML = size;
        document.getElementById("BannerSizeDown").innerHTML = size;
    }
    catch(e)
    {
        WriteErrorLog(e, "SBRender");
    }
    
    return false;
}

function Validate(headerText)
{
    try
    {
        var result = true;
        if ((kasamba_SB_textBoxLenght != "") && (!isNaN(kasamba_SB_textBoxLenght)) )
        {
            if ((parseInt(kasamba_SB_textBoxLenght) < parseInt(kasamba_SB_def_textBoxLenghtMin)) || (parseInt(kasamba_SB_textBoxLenght) > parseInt(kasamba_SB_def_textBoxLenghtMax)) )
            {
                kasamba_SB_textBoxLenght = kasamba_SB_def_textBoxLenght;
            }
        }
        else
        {
            kasamba_SB_textBoxLenght = kasamba_SB_def_textBoxLenght;
        } 
        
        if (typeof(headerText) != 'undefined')
        {
            var text = Trim(headerText.value);
            if (document.getElementById("divHeaderTextBox").style.display!="none")   
            {
                //Check if text is empty
                if ((text == "Type your text here...") || (text ==""))
                {
                    result= false;
                    document.getElementById("divHeaderTextBox").style.display!="inline";
                }
                else
                {
                    if (!ValidateText(text))
                    {
                        result= false;
                        document.getElementById("divHeaderTextBox").style.display!="inline";
                    }  
                }
            }
        }
        return result;
    }
    catch(e)
    {
        WriteErrorLog(e, "Validate");
        return false;
    }
}  

function GetColors()
{
    try
    {
       kasamba_SB_borderColor = document.getElementById("field1").value;
       kasamba_SB_backgroundColor = document.getElementById("field2").value;
       kasamba_SB_textColor = document.getElementById("field3").value;
       kasamba_SB_buttonColor = document.getElementById("field4").value;
       SBRender();
    }
    catch(e)
    {
        WriteErrorLog(e, "GetColors");
    }
}  


function TextBox_OnClick (textBox)
{
    if (textBox.value === "Type your text here...")
    {
        textBox.value = "";    
    }
}             


// Callback function invoked on successful 
// completion of the page method.
function OnSucceeded(result, userContext, methodName) 
{
 
}

// Callback function invoked on failure 
// of the page method.
function OnFailed(error, userContext, methodName) 
{
    if(error !== null) 
    {
        alert(error.get_message());
    }
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
   