function TestColorCode(id,value,i)
{
    var status;
    var test = value.toString().match(/[^0-9A-F]/gi) ;
    var length = value.length;
    if( test || length !=6)
    {
         value = Settings[i];         
         document.getElementById(id).value = value;
    } 
    else
        Settings[i] = value;
                   
    return value;
}
function HexColorCodeGenerator(id)
{
    var TrixCGId=(isIE)?"ColorCodeGenerator_IE":"ColorCodeGenerator";
    try
    {
        var value = document.getElementById(id).value;
        document.getElementById(TrixCGId).GenerateColorsAndCodes(value,id);
    }
    catch(err){}    
}
function TestInteger(id,value,i,minval,maxval)
{
    var isnumeric = /^\d+$/; 
    if(!isnumeric.test(value))
    {
       value=Settings[i];
       document.getElementById(id).value = value;
    }
    else if(value<minval)
    {
       value=minval;
       document.getElementById(id).value=minval;
    }
    else if(value>maxval)
    {
       value=maxval;
       document.getElementById(id).value=maxval;
    }
    Settings[i]=value;
    return value;
}
function PreviewSelectedColor(value,id)
{
    document.getElementById(id).value = value;
    document.getElementById(id).onchange();
}
function DisplayCode()
{ 
    document.getElementById("KuralCode").value="<script type='text/javascript'>\nvar Kural_FrameBGColor = '#"+Settings[0]+"';\nvar Kural_FrameTextColor = '#"+Settings[1]+"';\nvar Kural_TextAreaBGColor = '#"+Settings[2]+"';\nvar Kural_TextAreaColor = '#"+Settings[3]+"';\nvar Kural_NoOfSeconds = "+Settings[4]+";\nvar Kural_Width = "+Settings[5]+";\nvar Kural_Height = "+Settings[6]+";\nvar Kural_Language = '"+Settings[7]+"';\nvar Kural_TopBar = '"+Settings[8]+"';\nvar Kural_BottomBar = '"+Settings[9]+"';\nvar Kural_Info = '"+Settings[10]+"';\nvar Kural_TamilAlso = '"+Settings[11]+"';\nvar Kural_Meaning = '"+Settings[12]+"';\nvar Kural_Daily = '"+Settings[13]+"';\n</script>\n<script type='text/javascript' src='http://e-infotainment.com/applications/thirukural-widget/v2/js/form.js'></script><noscript>Your browser does not have javascript enabled. To get the code for displaying thirukural in your webpage, go <a href='http://e-infotainment.com/applications/kural'>here</a></noscript>";
}
function FrameBGColorChange()
{
    var fc=TestColorCode("FrameBGColor",document.getElementById("FrameBGColor").value,0);
    document.getElementById("Kuralv2_Form").style.background = "#"+fc;
    document.getElementById("Kural_InputLine1").style.background = "#"+fc;
    document.getElementById("Kural_InputLine2").style.background = "#"+fc;   
    DisplayCode();
}
function FrameTextColorChange()
{
    var fatc=TestColorCode("FrameTextColor",document.getElementById("FrameTextColor").value,1);
    document.getElementById("Kural_InputLine1").style.color = "#"+fatc;
    document.getElementById("Kural_InputLine2").style.color = "#"+fatc;
    document.getElementById("Kural_BackLink").style.color = "#"+fatc;
    DisplayCode();
}
function TextAreaBGColorChange()
{
    var tac=TestColorCode("TextAreaBGColor",document.getElementById("TextAreaBGColor").value,2);
    document.getElementById("Kural_TextArea").style.background = "#"+tac;
    DisplayCode();
}
function TextAreaColorChange()
{
    var tc=TestColorCode("TextAreaColor",document.getElementById("TextAreaColor").value,3);
    document.getElementById("Kural_TextArea").style.color = "#"+tc;
    DisplayCode();
}
function NoOfSecondsChange()
{
    var timeint=TestInteger("NoOfSeconds",document.getElementById("NoOfSeconds").value,4,5,300);
    document.getElementById("Kural_NoOfSecs").value = timeint;
    DisplayCode();
}
function WidthChange()
{
    var wid=TestInteger("Width",document.getElementById("Width").value,5,300,1100);
    var FrameFontSize,KuralFontSize,Height;
    if(wid<350)
    {
        InputFontSize = 8;KuralFontSize=11;Height=18;
    }
    else if(wid < 400)
    {
        InputFontSize = 10;KuralFontSize=12;Height=19;
    }
    else if(wid < 450)
    {
        InputFontSize = 11;KuralFontSize=14;Height=20;
    }
    else if(wid < 500)
    {
        InputFontSize = 12;KuralFontSize=15;Height=22;
    }
    else if(wid < 600)
    {
        InputFontSize = 13;KuralFontSize=17;Height=24;
    }
    else
    {
        InputFontSize = 15;KuralFontSize=18;Height=26;
    }
    
    var formdiv = document.getElementById("Kuralv2_Form");
    var textarea = document.getElementById("Kural_TextArea");
    
    formdiv.style.width = wid + "px";
    textarea.style.width = parseInt(wid-6) + "px";
    
    
    var elems = formdiv.getElementsByTagName("input");
    for(var i=0;i<elems.length;i++)
        elems[i].style.fontSize = InputFontSize + "px";
    
    elems = formdiv.getElementsByTagName("select");
    for(i=0;i<elems.length;i++)
        elems[i].style.fontSize = InputFontSize + "px"; 
    
    textarea.style.fontSize = InputFontSize + "px";    
    document.getElementById("Kural_InputLine1").style.fontSize = KuralFontSize +"px";
    document.getElementById("Kural_InputLine2").style.fontSize = KuralFontSize +"px";    
    
    document.getElementById("Kural_InputLine1").style.height = Height +"px";
    document.getElementById("Kural_InputLine2").style.height = Height +"px";
    
    DisplayCode();
}
function HeightChange()
{
    var ht=TestInteger("Height",document.getElementById("Height").value,6,60,500);
    document.getElementById("Kural_TextArea").style.height = ht +"px";
    DisplayCode();
}
function LanguageChange()
{
    var langs=document.getElementById("Kural_Lang");
    var length = langs.length;
    Settings[7] = document.getElementById("Language").value;
    for(var i=0;i<length;i++)
        if(langs.options[i].text == Settings[6])
        { 
        alert(i+" gud");
            langs.options[i].selected=true;
    }
    DisplayCode();
}
function DailyKuralChange()
{
    Settings[13] = document.getElementById("DailyKural").value;   
    Kural_Daily = Settings[13];
    var value = (Settings[13]=="False")?false:true;
    document.getElementById("Kural_No").disabled = value;
    document.getElementById("Kural_NoOfSecs").disabled = value;
    document.getElementById("Kural_PauseResume").disabled = value;    
    document.getElementById("Kural_Next").disabled = value;
    document.getElementById("Kural_Option").disabled = value;
    DisplayCode();
    Kural_Get(false);
}
function TopbarChange()
{
    var stb=document.getElementById("TopBar").value;
    document.getElementById("Kural_InputLine1").style.display = stb;
    Settings[8]=(stb == "block")?"Yes":"No";
    DisplayCode();
}
function BottombarChange()
{
    var sbb=document.getElementById("BottomBar").value;
    document.getElementById("Kural_InputLine2").style.display = sbb;
    Settings[9]=(sbb == "block")?"Yes":"No";
    DisplayCode();
}
function KuralInfoChange()
{
    Settings[10]=document.getElementById("KuralInfo").value;
    if(Settings[10] == "Yes")
        document.getElementById("Kural_HideOrShowInfo").value = "Show Info";
    else 
        document.getElementById("Kural_HideOrShowInfo").value = "Hide Info";
    Kural_InfoChange();
    DisplayCode();
}
function KuralTamilChange()
{
    Settings[11]=document.getElementById("KuralTamil").value;
    Kural_TamilAlso = Settings[11];
    Kural_Get(true);
    DisplayCode();
}
function KuralMeaningChange()
{
    Settings[12]=document.getElementById("KuralMeaning").value;
    Kural_Meaning = Settings[12];
    Kural_Get(true);
    DisplayCode();
}
function init()
{
    isIE = (navigator.appName == "Microsoft Internet Explorer")?true:false;    
    DisplayCode();
}

var isIE;
var Settings = new Array("000000","FFFFFF","555555","FFFFFF","30","400","140","Tamil","Yes","Yes","Yes","No","Yes","False");
window.onload = init;