﻿// JScript File
var VALIDATE_COLOUR = "#FFbbFF";
var PAID = false;

function HandlePayPal(obj)
{
    if (obj.checked)
	    divTickets.style.display = "block";
	else
	    divTickets.style.display = "none";
	    
	//PAID = true;
}

function UntickOthers(ref)
{
    var chkLocal = GetControlByID('input','chkLocal')
    var chkNational = GetControlByID('input','chkNational')
    var chkHasAdvert = GetControlByID('input','chkHasAdvert')
    var chkHasAdvertGlobal = GetControlByID('input','chkHasAdvertGlobal')
    
    if (ref.checked)
    {
        PAID = true;
        if (ref != chkLocal) chkLocal.checked = false;
        if (ref != chkNational) chkNational.checked = false;
        if (ref != chkHasAdvert) chkHasAdvert.checked = false;
        if (ref != chkHasAdvertGlobal) chkHasAdvertGlobal.checked = false;
    }
}

function UntickOthers(ref)
{
    var chkLocal = GetControlByID('input','chkLocal')
    var chkNational = GetControlByID('input','chkNational')
    var chkHasAdvert = GetControlByID('input','chkHasAdvert')
    var chkHasAdvertGlobal = GetControlByID('input','chkHasAdvertGlobal')
    
    if (ref.checked)
    {
        PAID = true;
        if (ref != chkLocal) chkLocal.checked = false;
        if (ref != chkNational) chkNational.checked = false;
        if (ref != chkHasAdvert) chkHasAdvert.checked = false;
        if (ref != chkHasAdvertGlobal) chkHasAdvertGlobal.checked = false;
    }
}

function UntickOthersEdit(ref)
{
    var chkNational = GetControlByID('input','chkNational')
    var chkHasAdvert = GetControlByID('input','chkHasAdvert')
    var chkHasAdvertGlobal = GetControlByID('input','chkHasAdvertGlobal')
    
    if (ref.checked)
    {
        //PAID = true;
        if(chkNational)
            if (ref != chkNational) chkNational.checked = false;
        
        if (chkHasAdvert)
            if (ref != chkHasAdvert) chkHasAdvert.checked = false;
        
        if (chkHasAdvertGlobal)
            if (ref != chkHasAdvertGlobal) chkHasAdvertGlobal.checked = false;
    }
}

function HandleHasAdvert(obj)
{
    var txtFeature = GetControlByID("span","lblHasAdvertPrice");
    var imgFeatured = GetControlByID("img","imgKeyFeatured");
    if (obj.checked)
    {
	    txtFeature.innerText = " ( Let us create a Box Advert to appear on all pages across all regional sites - Just £15 )";
	    imgFeatured.src = "images/KeyGlobalFeatured.gif";
	}
	else
	{
	    txtFeature.innerText = " ( Let us create a Box Advert to appear on all pages of your local regional sites - Just £10 )";
	    imgFeatured.src = "images/KeyLocalFeatured.gif";
	}
}

function CheckCap()
{
    var cap = GetControlByName('input','CaptchaControl1')
    if (cap.value == '')
    {
        alert('Please go back and input validation code.');
        return false;
    }
    return true;
}

function ValidateForm(ignoreCap)
{
    SUCCESS = false;

    try
    {
        //if (ignoreCap == false && CheckCap() == false) 
        //    errorString+="Please input Validation Code";

        var errorString = "";
        
        if (!ignoreCap)
            errorString = ValidateItem (errorString, GetControlByName('input','CaptchaControl1'), "Validation Code");
        
        var cboDay = GetControlByID('select','cboDay');
        var cboMonth = GetControlByID('select','cboMonth');
        var cboYear = GetControlByID('select','cboYear');
        
        errorString = ValidateItem (errorString, GetControlByID('input','txtName'), "Contact Name" );
        
        var txtDesc = GetControlByID('textarea','txtDesc');
        errorString = ValidateItem (errorString, txtDesc, "Event Description");
        
        var txtEmail = GetControlByID('input','txtEmail');
        errorString = ValidateEmail(errorString, txtEmail, "");
        
        if (GetControlByID('input','chkPayPal').checked == true)
        {
            var txtPPEmail = GetControlByID('input','txtPPEmail');
            errorString = ValidateEmail(errorString, txtPPEmail, "PayPal");
            
        }
        
        errorString = ValidateAmount(errorString, GetControlByID('input','txtPPPrice') );
        
        errorString = ValidateItem(errorString,  GetControlByID('textarea','txtAddress'), "Venue Address");
        errorString = ValidateItem(errorString,  GetControlByID('input','txtPassword'), "Password");
        errorString = ValidateItem (errorString, GetControlByID('input','txtEventTitle'), "Event Title" );
        errorString = ValidateItem (errorString, GetControlByID('select','cboCountry'), "Country" );
        errorString = ValidateItem (errorString, GetControlByID('select','cboCategory'), "Event Category" ); 
        errorString = ValidateItem (errorString, GetControlByID('select','cboRanking'), "Event Display Position" ); 
        errorString = ValidateItem (errorString, GetControlByID('select','cboEventType'), "Event Type" ); 
        errorString = ValidateItem (errorString, GetControlByID('select','cboHours'), "Event Start Hour" ); 
        errorString = ValidateItem (errorString, GetControlByID('select','cboMinutes'), "Event Start Minutes" ); 
        
        var cboRegion = GetControlByID('select','cboRegion');
        if (cboRegion)
        {
            errorString = ValidateItem (errorString, cboRegion, "Please Supply Region" ); 
        }
                    
        cboDay.parentNode.style.backgroundColor = '#ffffff';
        if (cboDay.value == "" || cboMonth.value == "" || cboYear.value == "")
        {
            errorString+= "Please supply Event Date\n";
            cboDay.parentNode.style.backgroundColor = VALIDATE_COLOUR;
        }
        
        if (errorString == "")
        {   var txtCouponKey = GetControlByID('input','txtCouponKey');
            
            if(txtCouponKey.value != "") 
            {
                return confirm('You are about to submit this event with a discount coupon, are you happy to proceed and upgrade this event?');
            }
        
            if (PAID == false)
	    {
                return true;
		//confirm('Free events will no longer be listed in our weekly newsletters.\n\nPress OK to continue posting your FREE Event or Cancel to review other options below.');	
	    }
            else
                return true;
        }
        alert(errorString);
        return false;
    }
    catch(e)
    {
        alert(e);
        return false;
    }
}

function ValidateItem(errorString, control, friendlyName)
{

    control.parentNode.style.backgroundColor = '#ffffff';
    if (control.value == "" )
    {
        errorString+= "Please supply " + friendlyName + "\n";
        control.parentNode.style.backgroundColor = VALIDATE_COLOUR;
    }
    return errorString;
}

function ValidateAmount(errorString, control)
{

    control.parentNode.style.backgroundColor = '#ffffff';
    if (control.value != "0.0" || control.value == "0")
    {
        var val = parseFloat(control.value);
        
        if (isNaN( val ) || val == 0 )
        {
            control.parentNode.style.backgroundColor = VALIDATE_COLOUR;
            errorString+= "Please Supply PayPal Ticket Price\n";
        }
    }
    return errorString;
}

function ValidateEmail(errorString, control, name)
{
        control.parentNode.style.backgroundColor = '#ffffff';
        
        var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
        if (!filter.test(control.value) ) 
        {
            errorString += "Please supply a Valid " + name + " Email Address\n";
            control.parentNode.style.backgroundColor = VALIDATE_COLOUR;
        }
        return errorString;
}

function WordCount(ref)
{
    var txtDesc = GetControlByID("textarea","txtDesc");
    var lblMaxWords = GetControlByID("span","lblMaxWords");

    var count = (4000 - txtDesc.value.length);
       
    lblMaxWords.innerText = count.toString();
    lblMaxWords.textContent = count.toString();
    
    if (count>= 0)
        lblMaxWords.className = 'MaxWords';
    else
      lblMaxWords.className = 'MaxWordsRed';
    
}

function PopulatePayPalClosingDate()
{
    var valDate = GetControlByID("input", "txtDate");
    var valCloseDate = GetControlByID("input", "txtPPCloseDate");
    valCloseDate.value = valDate.value;
    
}

function PopulatePayPalClosingDay(ref)
{
    var cboCloseDay = GetControlByID("select","cboCloseDay");
    cboCloseDay.value = ref.value;
}
function PopulatePayPalClosingMonth(ref)
{
    var cboCloseMonth = GetControlByID("select","cboCloseMonth");
    cboCloseMonth.value = ref.value;
}
function PopulatePayPalClosingYear(ref)
{
    var cboCloseYear = GetControlByID("select","cboCloseYear");
    cboCloseYear.value = ref.value;
}