﻿//<!--
/// <reference path="jquery-1.3.2-vsdoc2.js" />

function postEventSubscription() {
    if (!validateEvents()) return false;
    var btnSubmit = document.getElementById('btnSubmit');
    btnSubmit.style.display = 'none';

    var answers = '';

    var ddl = document.getElementById(_d);
    if (ddl.selectedIndex == 0) { alert('Please select a payment type'); return false; }

    answers += 'fname=' + $("#" + _fn).attr('value');
    answers += '&lname=' + $("#" + _ln).attr('value');
    answers += '&work=' + $("#" + _work).attr('value');
    answers += '&cell=' + $("#" + _cell).attr('value');
    answers += '&email=' + $("#" + _email).attr('value');
    answers += '&day=' + $("#" + _day).attr('value');
    answers += '&month=' + $("#" + _month).attr('value');
    answers += '&year=' + $("#" + _year).attr('value');
    answers += '&newsletter=' + $("#" + _newsletter).attr('checked');
    answers += '&tickets=' + $("#" + _tickets).val();
    answers += '&eventID=' + $("#" + _e).attr('value');
    answers += '&payopt=' + ddl[ddl.selectedIndex].value;


    var eQuestions = '"(';
    if (_ids[0] != null) {
        for (i = 0; i < _ids.length; i++) {

            if ($("#" + _ids[i] + "_1").html() == null) {
                var qID = _ids[i].substring(_ids[i].lastIndexOf("_") + 1);
                var answer = $("#" + _ids[i]).attr('value');
                eQuestions += qID + '`' + answer + '|';
            }
            else {

                if ($("#" + _ids[i] + "_1").is("input:checkbox")) {
                    var checkboxIndex = '';
                    var checkboxqID;

                    var cb = _ids[i].split('_');
                    checkboxqID = cb[2];

                    for (c = 0; c < 7; c++) {
                        if ($("#" + _ids[i] + "_" + c.toString()) != null && $("#" + _ids[i] + "_" + c.toString()).attr('checked') == true) {
                            checkboxIndex += c + ',';
                        }
                    }
                    eQuestions += checkboxqID + '`' + checkboxIndex.substring(0, checkboxIndex.length - 1) + '|';
                }
                else if ($("#" + _ids[i] + "_1").is("input:radiobutton")) {
                    var radioIndex;
                    var radioqID;

                    var q = _ids[i].split('_');
                    radioqID = q[2];

                    for (d = 0; d < 7; d++) {
                        if ($("#" + _ids[i] + "_" + d.toString()) != null && $("#" + _ids[i] + "_" + d.toString()).attr('checked') == true) {
                            radioIndex = d;
                        }
                    }
                    eQuestions += radioqID + '`' + radioIndex + '|';
                }

            }

        }
        eQuestions = eQuestions.substring(0, eQuestions.length - 1);
    }
    eQuestions += ')"';
    answers += '&eventQuestions=' + eQuestions;    
    return postToRegistration(answers, ddl[ddl.selectedIndex].value);   
    
}

function postToRegistration(qs, ddl) {    
    $.ajax({
        type: "POST",
        url: "/Content/Events/register_event.ashx?" + qs,
        data: qs,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {            
            if (result.status == 'true') {
                if (ddl == 'cc') {
                    $('form').after("<form method='post' name='frmCC' action='https://www.vcs.co.za/vvonline/ccform.asp'><input type='hidden' name='p1' value='" + result.id + "' /><input type='hidden' name='p2' value='" + result.ref + "' /><input type='hidden' name='p3' value='" + result.desc + "' /><input type='hidden' name='p4' value='" + result.amount + "' /><input type='hidden' name='p5' value='" + result.curr + "' /><input type='hidden' name='p10' value='http://www.cosmopolitan.co.za/eventbookings/cancelled' /></form>");
                    document.frmCC.submit();
                }
                else {
                    window.location = '/eventbookings/Thanks';
                }
            }
            else { alert(result.status); }
        }
        , error: function(data) { alert(data.responseText); }
    });
    
    return false;
}

function validateEvents() {
    if (clean(_fn) == '') { Alert('first name', _fn); return false; }
    if (clean(_ln) == '') { Alert('last name', _ln); return false; }
    if (clean(_work) == '') { Alert('work number', _work); return false; }
    if (clean(_cell) == '') { Alert('cellphone number', _cell); return false; }
    if (clean(_email) == '') { Alert('email address', _email); return false; }
    if (clean(_email2) == '') { Alert('email address', _email2); return false; }
    if ($("#" + _email).val() != $("#" + _email2).val()) {alert('Your email addresses do not match');clean(_email); return false; }
    if (clean(_day) == '' || clean(_day) == '0') { Alert('date of birth', _day); return false; }
    if (clean(_month) == '' || clean(_month) == '0') { Alert('date of birth', _month); return false; }
    if (clean(_year) == '' || clean(_year) == '0') { Alert('date of birth', _year); return false; }
    if (clean(_tickets) == '' || clean(_tickets) == '0') { Alert('ticket number', _tickets); return false; }
    
    var ddl = document.getElementById(_d);
    if (ddl.selectedIndex == 0) { alert('Please select your payment method');clean(_d);  return false; }

    return true;
}

function clean(elem) {
    var f = document.getElementById(elem);
    f.focus();
    f = f.value.replace(/^\s+|\s+$/g, "");
    return f;
}
function Alert(control, field) {
    var f = document.getElementById(field);
    alert('Please enter your ' + control);
    f.focus();
}

function StringBuilder(value)
{
    this.strings = new Array("");
    this.append(value);
}

// Appends the given value to the end of this instance.

StringBuilder.prototype.append = function (value)
{
    if (value)
    {
        this.strings.push(value);
    }
}

// Clears the string buffer

StringBuilder.prototype.clear = function ()
{
    this.strings.length = 1;
}

// Converts this instance to a String.

StringBuilder.prototype.toString = function ()
{
    return this.strings.join("");
}

function postDatingCriteria() 
{
var sb = new StringBuilder();


    var ddlgender = document.getElementById(_gender);
    var ddlmGender = document.getElementById(_mGender);  
    var ddlmAgeMin = document.getElementById(_mAgeMin);
    var ddlmAgeMax = document.getElementById(_mAgeMax);
    var select = document.getElementById('_mProvinceID');
   

         for(i=0; i < select.options.length; i++)
         {    
            if(select.options[i].selected==true)
            {
            sb.append("<input type='hidden' name='mProvinceID[]' id='mProvinceID[]' value='" + select.options[i].value  +"' />");  
            }
         }
        var s = sb.toString();
   

    answers1 =  ddlgender[ddlgender.selectedIndex].value;
    answers2 =  ddlmGender[ddlmGender.selectedIndex].value;
    answers3 =  ddlmAgeMin[ddlmAgeMin.selectedIndex].value;
    answers4 =  ddlmAgeMax[ddlmAgeMax.selectedIndex].value;    
    
    
  
    $('form').after("<form method='post' name='frmMM' target='winMM' action='http://www.cosmopolitandating.co.za/s/find/search.php?'><input type='hidden' name='sub' id='sub' value='yes' /><input type='hidden' name='xqs' value='yes' /><input type='hidden' name='aID' id='aID' value='16968' /><input type='hidden' name='rqs' value='rqs' /><input type='hidden' name='mCountryID[]' value='za' />" + s  + "<input type='checkbox' name='mHasPhoto' id='mHasPhoto' value='1' checked='checked' style='display:none;' /><input type='hidden' name='gender' id='gender' value='"+ answers1 +"' /><input type='hidden' name='mGender'  id='mGender' value='"+ answers2 +"' /><input type='hidden' name='mAgeMin' id='mAgeMin' value='"+ answers3 +"' /><input type='hidden' name='mAgeMax' id='mAgeMax' value='"+ answers4 +"' /><input type='hidden' name='quickSearch' id='quickSearch' value='1' /> </form>");
        document.frmMM.submit();
}
    



