// TODO: make validation look like .NET validation controls, instead of ugly alerts...
function validateCompEntry(thisform)
{
    var temp_Forename = "";
    var temp_Surname = "";
    var temp_Telephone = "";
    var temp_ValidTelephone = "";
    var temp_Email = "";
    var temp_ValidEmail = "";
    var temp_Gender = "";
    var temp_Answer = "";
    var temp_Terms = "";
    var temp_DOB = "";

    var temp_Form;

    with (thisform)
    {
        if (validateRequiredField(forename)==false)
        {
            document.getElementById('txfCompError').style.display = 'block';
            document.getElementById('txfCompErrorForename').style.display = 'inline';
            temp_Forename = "x";
        }
        else
        {
            document.getElementById('txfCompErrorForename').style.display = 'none';
            temp_Forename = "";
        }
       
        if((document.getElementById('dob_d')!=null) && (validateRequiredField(dob_d)==false || validateRequiredField(dob_m)==false || validateRequiredField(dob_y)==false) )
        {
            document.getElementById('txfCompError').style.display = 'block';
            document.getElementById('txfCompErrorDOB').style.display = 'inline';
            temp_DOB = "x";
        }
        else
        {
            document.getElementById('txfCompErrorDOB').style.display = 'none';
            temp_DOB = "";
        }
        
        

        if (validateRequiredField(surname)==false)
        {
            document.getElementById('txfCompError').style.display = 'block';
            document.getElementById('txfCompErrorSurname').style.display = 'inline';
            temp_Surname = "x";
        }
        else
        {
            document.getElementById('txfCompErrorSurname').style.display = 'none';
            temp_Surname = "";
        }

        if (validateRequiredField(phone)==false)
        {
            document.getElementById('txfCompError').style.display = 'block';
            document.getElementById('txfCompErrorTelephone').style.display = 'inline';
            temp_Telephone = "x";
        }
        else
        {
            document.getElementById('txfCompErrorTelephone').style.display = 'none';
            temp_Telephone = "";

            // must check if there's a number in telephone
            if (!(validateTelephone(phone)))
            {
                document.getElementById('txfCompError').style.display = 'block';
                document.getElementById('txfCompErrorValidTelephone').style.display = 'inline';
                temp_ValidTelephone = "x";
            }
            else
            {
                document.getElementById('txfCompErrorValidTelephone').style.display = 'none';
                temp_ValidTelephone = "";
            }
        }


        if (validateRequiredField(email)==false)
        {
            document.getElementById('txfCompError').style.display = 'block';
            document.getElementById('txfCompErrorEmail').style.display = 'inline';
            temp_Email = "x";
        }
        else
        {
            document.getElementById('txfCompErrorEmail').style.display = 'none';
            temp_Email = "";

            // check that it's a valid email address
            if (validateEmail(email)==false)
            {
                document.getElementById('txfCompError').style.display = 'block';
                document.getElementById('txfCompErrorValidEmail').style.display = 'inline';
                temp_ValidEmail = "x";
            }
            else
            {
                document.getElementById('txfCompErrorValidEmail').style.display = 'none';
                temp_ValidEmail = "";
            }
        }
        
        if (validateRadio(thisform.gender) == false) 
        {
            document.getElementById('txfCompError').style.display = 'block';
            document.getElementById('txfCompErrorGender').style.display = 'inline';
            temp_Gender = "x";
        }
        else
        {
            document.getElementById('txfCompErrorGender').style.display = 'none';
            temp_Gender = "";
        }

        if (validateRequiredField(answer) == false)
        {
            document.getElementById('txfCompError').style.display = 'block';
            document.getElementById('txfCompErrorAnswer').style.display = 'inline';
            temp_Answer = "x";
        }
        else
        {
            document.getElementById('txfCompErrorAnswer').style.display = 'none';
            temp_Answer = "";
        }

        if (validateCheckbox(thisform.optin) == false) 
        {
            document.getElementById('txfCompError').style.display = 'block';
            document.getElementById('txfCompErrorTerms').style.display = 'block';
            document.getElementById('txfCompErrorTerms').style.clear = 'both';
            temp_Terms = "x";
        }
        else
        {
            document.getElementById('txfCompErrorTerms').style.display = 'none';
            temp_Terms = "";
        }
    }

    temp_Form = temp_Forename + temp_Surname + temp_Telephone + temp_Telephone + temp_ValidTelephone + temp_ValidEmail + temp_Gender + temp_Answer + temp_Terms + temp_DOB;

    if (temp_Form == "")    
    {
        //document.getElementById('txfCompError').style.display = 'none';
        return true;
    }
    else
    {
        return false;
    }
}


function validateRequiredField(field)
{
    with (field)
    {
        if (value==null||value=="")
        {
            return false;
        }
        else
        {
            return true
        }
    }
}


function validateEmail(field)
{
    with (field)
    {
        apos=value.indexOf("@");
        dotpos=value.lastIndexOf(".");
        if (apos<1||dotpos-apos<2) 
        {
            return false;
        }
        else 
        {
            return true;
        }
    }
}


function validateRadio(btn)
{
    var cnt = -1;

    for (var i=btn.length-1; i > -1; i--)
    {
        if (btn[i].checked)
        {
            cnt = i; i = -1;
        }
    }

    if (cnt == -1)
    {
        return false;
    }
    else
    {
        return true;
    }
}


function validateTelephone(field)
{
    var patt1 = new RegExp("[0-9]");
    var result = patt1.test(field.value);
    return result;
}


function validateCheckbox(chk)
{
    if(!chk.checked)
    {
        return false;
    }
    else
    {
        return true;
    }
}



function insertRandomChars(strInput)
{
    var strOutput = ""
    var strCharPart = ""
    var lngRandom = 0;
    for (i=0; i<strInput.length; i++){
        strCharPart = strInput.charAt(i);
        if (strCharPart == "*")
        {
            lngRandom = Math.ceil(Math.random() * 10) - 1;
            strOutput = strOutput + lngRandom.toString();
        } 
        else 
        {
            strOutput = strOutput + strCharPart;
        }
    }
    return strOutput; 
}

function defaultButtonSubmit(Id)
{    
    __doPostBack(Id, '');
}

function ismaxlength(obj)
{
    var mlength=obj.getAttribute? parseInt(obj.getAttribute("maximumlength")) : ""
    if (obj.getAttribute && obj.value.length>mlength)
    obj.value=obj.value.substring(0,mlength)
}

function maxLength(field,maxChars,ordinal)
{
      var elm=document.getElementById('spnMaxLength' + ordinal);
       if(field.value.length > maxChars)
       {
            elm.className='max';
       }
       else
       {
            elm.className='normal';
       }
}  

function playlistGotoFeature(i)
{
    document.getElementById("txfItemExpansionVideoMediaInner").playlistGotoFeature(i);
}
function playlistGoto(i)
{
    document.getElementById("txfItemExpansionVideoMediaInner").playlistGoto(i);
}

function logAggView(aggId)
{
    $j.get("/_contentelements/program/handlers/logviews.ashx", { item: aggId } );
}

function logAggRating(aggId, thumb)
{
    $j.get("/_contentelements/program/handlers/rate.ashx", { item: aggId, thumb: thumb} );
}

function InitBlogToggle()
{
    $("#slide2016").hide(); $("#slide2015").hide(); $("#slide2014").hide(); $("#slide2013").hide(); $("#slide2012").hide(); $("#slide2011").hide(); $("#slide2010").hide(); $("#slide2009").hide(); $("#slide2008").hide(); $("#slide2007").hide(); $("#slide2006").hide(); $("#slide2005").hide();

    $("li.yearLink a").click(
        function() {
            var yearLink = $(this).attr("href");
            var yearRel = $(this).attr("rel");

            if ((yearLink == "#expand2016") && (yearRel == "show"))
            {
                $("ul#slide2016").slideDown("fast"); $(this).attr({rel: "hide"}); $(this).attr({title: "hide months"});
            }

            if ((yearLink == "#expand2016") && (yearRel == "hide"))
            {
                $("ul#slide2010").slideUp("fast"); $(this).attr({rel: "show"}); $(this).attr({title: "show months"});
            }

            if ((yearLink == "#expand2015") && (yearRel == "show"))
            {
                $("ul#slide2015").slideDown("fast"); $(this).attr({rel: "hide"}); $(this).attr({title: "hide months"});
            }

            if ((yearLink == "#expand2015") && (yearRel == "hide"))
            {
                $("ul#slide2015").slideUp("fast"); $(this).attr({rel: "show"}); $(this).attr({title: "show months"});
            }

            if ((yearLink == "#expand2014") && (yearRel == "show"))
            {
                $("ul#slide2014").slideDown("fast"); $(this).attr({rel: "hide"}); $(this).attr({title: "hide months"});
            }

            if ((yearLink == "#expand2014") && (yearRel == "hide"))
            {
                $("ul#slide2014").slideUp("fast"); $(this).attr({rel: "show"}); $(this).attr({title: "show months"});
            }

            if ((yearLink == "#expand2013") && (yearRel == "show"))
            {
                $("ul#slide2013").slideDown("fast"); $(this).attr({rel: "hide"}); $(this).attr({title: "hide months"});
            }

            if ((yearLink == "#expand2013") && (yearRel == "hide"))
            {
                $("ul#slide2013").slideUp("fast"); $(this).attr({rel: "show"}); $(this).attr({title: "show months"});
            }

            if ((yearLink == "#expand2012") && (yearRel == "show"))
            {
                $("ul#slide2012").slideDown("fast"); $(this).attr({rel: "hide"}); $(this).attr({title: "hide months"});
            }

            if ((yearLink == "#expand2012") && (yearRel == "hide"))
            {
                $("ul#slide2012").slideUp("fast"); $(this).attr({rel: "show"}); $(this).attr({title: "show months"});
            }

            if ((yearLink == "#expand2011") && (yearRel == "show"))
            {
                $("ul#slide2011").slideDown("fast"); $(this).attr({rel: "hide"}); $(this).attr({title: "hide months"});
            }

            if ((yearLink == "#expand2011") && (yearRel == "hide"))
            {
                $("ul#slide2011").slideUp("fast"); $(this).attr({rel: "show"}); $(this).attr({title: "show months"});
            }

            if ((yearLink == "#expand2010") && (yearRel == "show"))
            {
                $("ul#slide2010").slideDown("fast"); $(this).attr({rel: "hide"}); $(this).attr({title: "hide months"});
            }

            if ((yearLink == "#expand2010") && (yearRel == "hide"))
            {
                $("ul#slide2010").slideUp("fast"); $(this).attr({rel: "show"}); $(this).attr({title: "show months"});
            }

            if ((yearLink == "#expand2009") && (yearRel == "show"))
            {
                $("ul#slide2009").slideDown("fast"); $(this).attr({rel: "hide"}); $(this).attr({title: "hide months"});
            }

            if ((yearLink == "#expand2009") && (yearRel == "hide"))
            {
                $("ul#slide2009").slideUp("fast"); $(this).attr({rel: "show"}); $(this).attr({title: "show months"});
            }

            if ((yearLink == "#expand2008") && (yearRel == "show"))
            {
                $("ul#slide2008").slideDown("fast"); $(this).attr({rel: "hide"}); $(this).attr({title: "hide months"});
            }

            if ((yearLink == "#expand2008") && (yearRel == "hide"))
            {
                $("ul#slide2008").slideUp("fast"); $(this).attr({rel: "show"}); $(this).attr({title: "show months"});
            }

            if ((yearLink == "#expand2007") && (yearRel == "show"))
            {
                $("ul#slide2007").slideDown("fast"); $(this).attr({rel: "hide"}); $(this).attr({title: "hide months"});
            }

            if ((yearLink == "#expand2007") && (yearRel == "hide"))
            {
                $("ul#slide2007").slideUp("fast"); $(this).attr({rel: "show"}); $(this).attr({title: "show months"});
            }

            if ((yearLink == "#expand2006") && (yearRel == "show"))
            {
                $("ul#slide2006").slideDown("fast"); $(this).attr({rel: "hide"}); $(this).attr({title: "hide months"});
            }

            if ((yearLink == "#expand2006") && (yearRel == "hide"))
            {
                $("ul#slide2006").slideUp("fast"); $(this).attr({rel: "show"}); $(this).attr({title: "show months"});
            }
            
            if ((yearLink == "#expand2005") && (yearRel == "show"))
            {
                $("ul#slide2005").slideDown("fast"); $(this).attr({rel: "hide"}); $(this).attr({title: "hide months"});
            }

            if ((yearLink == "#expand2005") && (yearRel == "hide"))
            {
                $("ul#slide2005").slideUp("fast"); $(this).attr({rel: "show"}); $(this).attr({title: "show months"});
            }
            
            if ((yearLink == "#expand2004") && (yearRel == "show"))
            {
                $("ul#slide2004").slideDown("fast"); $(this).attr({rel: "hide"}); $(this).attr({title: "hide months"});
            }

            if ((yearLink == "#expand2004") && (yearRel == "hide"))
            {
                $("ul#slide2004").slideUp("fast"); $(this).attr({rel: "show"}); $(this).attr({title: "show months"});
            }
        }
    )
}


function DefaultToggle(selectedYear)
{
    var ulString = "ul#slide" + selectedYear;
    var liString = "li#yearLink" + selectedYear+" a";
    $(ulString).show(); $(liString).attr({rel: "hide"}); $(liString).attr({title: "hide months"});
}