function Kural_DrawForm()
{
   
    var strPat = /^#[0-9a-fA-F]{6}$/i;
    var InputFontSize,KuralFontSize,Height,InputHeight;
    var isnumeric = /^\d+$/; 

    if(typeof(Kural_FrameBGColor)=="undefined" || !strPat.test(Kural_FrameBGColor)) Kural_FrameBGColor = "#000000";
    if(typeof(Kural_FrameTextColor)=="undefined" || !strPat.test(Kural_FrameTextColor)) Kural_FrameTextColor = "#FFFFFF";
	if(typeof(Kural_TextAreaBGColor)=="undefined" || !strPat.test(Kural_TextAreaBGColor)) Kural_TextAreaBGColor = "#555555";
	if(typeof(Kural_TextAreaColor)=="undefined" || !strPat.test(Kural_TextAreaColor)) Kural_TextAreaColor = "#FFFFFF";

	if(typeof(Kural_NoOfSeconds)=="undefined") Kural_NoOfSeconds = 30;
	else
	{ 
	    Kural_NoOfSeconds = parseInt(Kural_NoOfSeconds);
	    if(!isnumeric.test(Kural_NoOfSeconds)) Kural_NoOfSeconds = 30;
	    else 
	        Kural_NoOfSeconds = (Kural_NoOfSeconds < 5)?5:((Kural_NoOfSeconds>300)?300:Kural_NoOfSeconds);
	}

	if(typeof(Kural_Width)=="undefined") Kural_Width = 400;
	else 
	{
	    Kural_Width = parseInt(Kural_Width);
	    if(!isnumeric.test(Kural_Width)) Kural_Width = 400;
	    else
	       Kural_Width = (Kural_Width < 300)?300:((Kural_Width>1100)?1100:Kural_Width);
	}
	
	if(Kural_Width<350)
    {
        InputFontSize = 8;KuralFontSize=11;Height=18;TAHeight=60;
    }
    else if(Kural_Width < 400)
    {
        InputFontSize = 10;KuralFontSize=12;Height=19;TAHeight=70;
    }
    else if(Kural_Width < 450)
    {
        InputFontSize = 11;KuralFontSize=14;Height=20;TAHeight=80;
    }
    else if(Kural_Width < 500)
    {
        InputFontSize = 12;KuralFontSize=15;Height=22;TAHeight=90;
    }
    else if(Kural_Width < 600)
    {
        InputFontSize = 13;KuralFontSize=17;Height=24;TAHeight=100;
    }
    else
    {
        InputFontSize = 15;KuralFontSize=18;Height=26;TAHeight=120;
    }
    
    if(typeof(Kural_Height)=="undefined") Kural_Height = TAHeight;
	else 
	{
	    Kural_Height = parseInt(Kural_Height);
	    if(!isnumeric.test(Kural_Height)) Kural_Height = TAHeight;
	    else if(Kural_Height < 60)
	       Kural_Height = 60;
	}
	
	if(typeof(Kural_Language)=="undefined" || (Kural_Language != "Tamil" && Kural_Language !="English" && Kural_Language !="Hindi" && Kural_Language !="Kannada" && Kural_Language !="French" && Kural_Language !="Arabic")) Kural_Language = "Tamil";
	if(typeof(Kural_Daily)=="undefined" || (Kural_Daily != "True" && Kural_Daily !="False")) Kural_Daily = "False";
	if(typeof(Kural_TopBar)=="undefined" || (Kural_TopBar!= "Yes" && Kural_TopBar != "No")) Kural_TopBar = "Yes";
	if(typeof(Kural_BottomBar)=="undefined" || (Kural_BottomBar!= "Yes" && Kural_BottomBar != "No")) Kural_BottomBar = "Yes";
	if(typeof(Kural_Info)=="undefined" || (Kural_Info!= "Yes" && Kural_Info != "No")) Kural_Info = "Yes";
	if(typeof(Kural_TamilAlso)=="undefined" || (Kural_TamilAlso!= "Yes" && Kural_TamilAlso != "No")) Kural_TamilAlso = "Yes";

	document.write("<form id='Kuralv2_Form' style='width:" + Kural_Width + "px; background:" + Kural_FrameBGColor + "; margin:0px auto;'><div id='Kural_OuterDiv' style='font-size:" + KuralFontSize + "px; margin:0px 3px; padding:3px 0px;'><div id='Kural_InputLine1' style='height:" + Height + "px; " + ((Kural_TopBar == 'No') ? 'display:none; ' : '') + "background: " + Kural_FrameBGColor + "; color: " + Kural_FrameTextColor + "; padding-bottom:2px;'><div class='Kural_NoOfSecsContainer' style='width: 20%; float: left; '>Secs <input id='Kural_NoOfSecs' type='text' size='2' value='" + Kural_NoOfSeconds + "' onchange='Kural_SecondsChange(this)' style='font-size:" + InputFontSize + "px;' /></div><div class='Kural_LangContainer' style='width: 22%; float: left; '><select id='Kural_Lang' onchange='Kural_Get(true)' style='font-size:" + InputFontSize + "px;'><option value='" + Kural_Language + "'>" + Kural_Language + "</option></select></div><div class='Kural_OptionContainer' style='width: 26%; float: left; '><select id='Kural_Option' style='font-size:" + InputFontSize + "px;'><option value='0'>Random</option><option value='1'>Increment</option><option value='2'>Decrement</option></select></div><div class='Kural_NoContainer' style='width: 31%; float: right;'>Kural No <input id='Kural_No' type='text' size='4' onchange='Kural_Get(true)' style='font-size:" + InputFontSize + "px;'/></div></div><div style='background: #555555'><div id='Kural_TextArea' readonly='readonly' style='background: " + Kural_TextAreaBGColor + "; margin:0px; padding:0px; border:none; color: " + Kural_TextAreaColor + "; font-size:" + InputFontSize + "px; overflow:auto; text-align:left; width:" + parseInt(Kural_Width - 6) + "px; height:" + Kural_Height + "px; '></div></div><div id='Kural_InputLine2' style='height:" + Height + "px; " + ((Kural_BottomBar == 'No') ? 'display:none; ' : '') + "background: " + Kural_FrameBGColor + "; color: " + Kural_FrameTextColor + "; padding-top:2px; font-size:" + KuralFontSize + "px' ><div class='Kural_BackLinkContainer' style='width:21%; float: left; '><a id='Kural_BackLink' href='http://e-infotainment.com' target='_blank' style='color:" + Kural_FrameTextColor + "'>Kural v2</a></div><div class='Kural_NextContainer' style='width:15%; float: right;'><input type='button' id='Kural_Next' onclick='Kural_Get(false)' value='Next' style='font-size:" + InputFontSize + "px;' /></div><div class='Kural_PauseResumeContainer' style='width:22%; float: right;'><input id='Kural_PauseResume' type='button' onclick='Kural_Toggle()'  value='Pause' style='font-size:" + InputFontSize + "px;'/></div><div class='Kural_HideOrShowInfoContainer' style='width:22%; float: right;'><input id='Kural_HideOrShowInfo' type='button' value='Hide Info' onclick='Kural_InfoChange()' style='font-size:" + InputFontSize + "px;'/></div></div></div></form>");
}
function Kural_UpdateList()
{
    var langs = ["Tamil","English","Hindi","Kannada","French","Arabic"];
    var select = document.getElementById('Kural_Lang');
    var length = langs.length;
    var i,j;
    var defaultlang = select.options[0].text    
    for(i=0,j=1;i<length ;i++) 
       if(langs[i]!= defaultlang)
       {
           select.options[j] = new Option(langs[i],langs[i]);    
           j++;
       }
}

function Kural_SecondsChange(elem)
{
    var seconds = elem.value;
    var isnumeric = /^\d+$/; 
    if(!isnumeric.test(seconds))
        elem.value = Kural_NoOfSeconds;
    else if(seconds>300)
    {
        Kural_NoOfSeconds = 300;
        elem.value = 300;
    }        
    else if(seconds<5)
    {
        Kural_NoOfSeconds = 5;
        elem.value = 5;
    }
    else
        Kural_NoOfSeconds = seconds;
}
function Kural_InfoChange()
{
    var btnkuralinfo = document.getElementById("Kural_HideOrShowInfo");
    if(btnkuralinfo.value == "Show Info")
    {
        Kural_Info = "Yes"    
        btnkuralinfo.value = "Hide Info";
    }
    else
    {
        Kural_Info = "No"    
        btnkuralinfo.value = "Show Info";
    }
    Kural_Get(true);
}
function Kural_Toggle()
{
    var btntoggle = document.getElementById("Kural_PauseResume");
    if(btntoggle.value == "Resume")
    {
        Kural_Timer = setTimeout("Kural_Get(false)",15000);
        btntoggle.value = "Pause";
    }
    else
    {
        clearInterval(Kural_Timer);
        btntoggle.value = "Resume";
    }
}
function Kural_Random()
{
    kural_no = Math.floor(1229*Math.random())+1;
    kural_getInfo(Kural_InputNo);
}

function Kural_Get(isUserInput)
{
    var isnumeric = /^\d+$/; 
    var opt = document.getElementById('Kural_Option').value;
    var kuralno = document.getElementById('Kural_No').value;
    
    if(document.getElementById("Kural_PauseResume").value == "Resume")
        document.getElementById("Kural_PauseResume").value = "Pause";
    
    clearTimeout(Kural_Timer);
    if(!isnumeric.test(Kural_NoOfSeconds) || Kural_NoOfSeconds == "")
        Kural_NoOfSeconds = 30;
    else if(Kural_NoOfSeconds < 5)
        Kural_NoOfSeconds = 5;
    else if(Kural_NoOfSeconds > 300)
        Kural_NoOfSeconds = 300; 
        
    
    if(kuralno=="")
        kuralno = Math.floor(1229*Math.random())+1;
    else if(!isnumeric.test(kuralno) || kuralno<0 || kuralno > 1330)
    {
        document.getElementById('Kural_No').value = "";
        return;
    }    
    if(!isUserInput)
    {
        if(opt == 1)
        {
            kuralno++;                    
            if(kuralno > 1330) kuralno = 1;
        }
        else if(opt == 2)
        {
            kuralno--;
            if(kuralno < 1) kuralno = 1330;
        }
        else 
            kuralno = Math.floor(1229*Math.random())+1;
    }
    Kural_RequestScript = document.createElement("script");
    Kural_RequestScript.src = "http://e-infotainment.com/applications/thirukural-widget/v2/kuralprocess.php?&val="+encodeURIComponent(kuralno)+"&lang="+encodeURIComponent(document.getElementById("Kural_Lang").value)+"&skt="+encodeURIComponent(Kural_TamilAlso)+"&ski="+encodeURIComponent(Kural_Info)+"&skm="+encodeURIComponent(Kural_Meaning)+"&dk="+encodeURIComponent(Kural_Daily)+"&rand="+Math.floor(1000000*Math.random());
    document.getElementsByTagName("head")[0].appendChild(Kural_RequestScript); 
    
    if(Kural_Daily == "False")
        Kural_Timer = setTimeout("Kural_Get(false);",1000*Kural_NoOfSeconds);
      
}
function Kural_RequestReceive(dat,kuralno,lang)
{
    if(lang == "Arabic")
        document.getElementById("Kural_TextArea").dir = "rtl";
    else 
        document.getElementById("Kural_TextArea").dir = "ltr";
    document.getElementsByTagName("head")[0].removeChild(Kural_RequestScript);
    document.getElementById("Kural_TextArea").innerHTML=dat;
    document.getElementById("Kural_No").value = kuralno;
}

function Kural_Init()
{
    Kural_DrawForm();
    Kural_UpdateList();    
    if(Kural_Daily == "True")
    {
        document.getElementById("Kural_PauseResume").disabled = true;
        document.getElementById("Kural_Next").disabled = true;
        document.getElementById("Kural_NoOfSecs").disabled = true;
        document.getElementById("Kural_No").disabled = true;
        document.getElementById("Kural_Option").disabled = true;        
    }
    
    Kural_Get(false);
}

var Kural_RequestScript;
var Kural_Timer=-1;
Kural_Init();
