// TODO: make validation look like .NET validation controls, instead of ugly alerts...
function validateCompEntry(thisform)
{
    var temp_First_Name = "";
    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_Address1 = "";
    var temp_Postcode = "";

    var temp_Form;

    with (thisform)
    {
        if (validateRequiredField(first_name)==false)
        {
            document.getElementById('txfCompError').style.display = 'block';
            document.getElementById('txfCompErrorFirst_Name').style.display = 'inline';
            temp_First_Name = "x";
        }
        else
        {
            document.getElementById('txfCompErrorFirst_Name').style.display = 'none';
            temp_First_Name = "";
        }
       
        if((document.getElementById('day')!=null) && (validateRequiredField(day)==false || validateRequiredField(month)==false || validateRequiredField(year)==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((document.getElementById('address1')!=null) && (validateRequiredField(address1)==false))
        {
            document.getElementById('txfCompError').style.display = 'block';
            document.getElementById('txfCompErrorAddress1').style.display = 'inline';
            temp_Address1 = "x";
        }
        else
        {
            document.getElementById('txfCompErrorAddress1').style.display = 'none';
            temp_Address1 = "";
        }
        

        if((document.getElementById('postcode')!=null) && (validateRequiredField(postcode)==false))
        {
            document.getElementById('txfCompError').style.display = 'block';
            document.getElementById('txfCompErrorPostcode').style.display = 'inline';
            temp_Postcode = "x";
        }
        else
        {
            document.getElementById('txfCompErrorPostcode').style.display = 'none';
            temp_Postcode = "";
        }
        

        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(telephone)==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(telephone)))
            {
                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.tandc) == 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_First_Name + temp_Surname + temp_Telephone + temp_Telephone + temp_ValidTelephone + temp_ValidEmail + temp_Gender + temp_Answer + temp_Terms + temp_DOB + temp_Address1 + temp_Postcode;

    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} );
}






    


