c2c_hour_array = [];
c2c_date_array = [];
c2c_minute_array = [];
var c2cQueueData = [];

function C2CinitialiseFromJSON(data)
   {
    c2cQueueData = data;
   }

function C2ChourChangeJSON(form)
   {
    var currentDay = form.day.selectedIndex;
    var currentHour = form.hour.selectedIndex;
    var minutes = c2cQueueData.queueDays[currentDay].times[currentHour].minutes;

    for (i = form.minute.length; i >= 0; --i)
       {
        form.minute.remove(i);
       }

    for (minuteIndex in minutes)
       {
        var opt = document.createElement('option');
        opt.text = minutes[minuteIndex];
        opt.value = minutes[minuteIndex];

        try
           {
            form.minute.add(opt, null);
           }
        catch(ex)
          {
           form.minute.add(opt); // IE only
          }
       }
   }

function C2CdayChangeJSON(form)
   {
    var currentDay = form.day.selectedIndex;
    var times = c2cQueueData.queueDays[currentDay].times;

    for (i = form.hour.length; i >= 0; --i)
       {
        form.hour.remove(i);
       }

    for (timeIndex in times)
       {
        var opt = document.createElement('option');
        opt.text = times[timeIndex].hour;
        opt.value = times[timeIndex].hour;

        try
           {
            form.hour.add(opt, null);
           }
        catch(ex)
          {
           form.hour.add(opt); // IE only
          }
       }

    C2ChourChangeJSON(form)
   }

function C2ConLoadFromJSON()
   {
    form = document.click2call;

    if (form.day)
       {
        if (c2cQueueData.disposition == "queue")
            document.getElementById('contactrow').style.display='';
          else
            document.getElementById('callbackrow').style.display='';

        for (dayIndex in c2cQueueData.queueDays)
           {
            var opt = document.createElement('option');
            opt.text = c2cQueueData.queueDays[dayIndex].dayOfWeek.substr(0,3);
            opt.value = c2cQueueData.queueDays[dayIndex].date;

            try
               {
                form.day.add(opt, null);
               }
            catch(ex)
              {
               form.day.add(opt); // IE only
              }

           }

        C2CdayChangeJSON(form);
       }
   }

function C2CinitialiseFromAjax(raw)
   {
    var form = document.click2call;
    var lines = raw.split('\n');

    if (form.day)
       {
        var status = lines.shift();

        if (status == "queue")
            document.getElementById('contactrow').style.display='';
        else
            document.getElementById('callbackrow').style.display='';

        for (day_index in lines)
           {
            var hour_index = -1;
            var minute_index = 0;
            var time_index = 0;

            var times = lines[day_index].split(' ');
            var date = times.shift();
            var day = times.shift().substr(0,3);
            var opt = document.createElement('option');
            c2c_hour_array[day_index] = [];
            c2c_minute_array[day_index] = [];

            opt.text = day;
            opt.value = date;

            for (i in times)
               {
                hour = times[i].substr(0, 2);
                if (c2c_hour_array[day_index][hour_index] != hour)
                   {
                    c2c_hour_array[day_index][++hour_index] = hour;
                    c2c_minute_array[day_index][hour_index] = [];
                    time_index = 0;
                   }
                c2c_minute_array[day_index][hour_index][minute_index++] = times[i].substr(3, 2);
               }

            // Add the day options to the form
            try
               {
                form.day.add(opt, null);
               }
            catch(ex)
              {
               form.day.add(opt); // IE only
              }
           }
       }

    C2Cdaychange(form);
   }

function C2CinitialiseTimes()
   {
    var sid = '7866d8d34c4828a66f833e9724dfe9d9';

    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest)
       {
        self.xmlHttpReq = new XMLHttpRequest();
       }
     else if (window.ActiveXObject)
      {
       // IE
       self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
      }

    self.xmlHttpReq.open('GET', location.protocol.toLowerCase() + '//c2c01.ulterius.net/c2c/v1.3/' + sid + '/callbacktime', true);
    self.xmlHttpReq.onreadystatechange = function()
       {
        if (self.xmlHttpReq.readyState == 4)
           {
            C2CinitialiseFromAjax(self.xmlHttpReq.responseText);
           }
       }

    self.xmlHttpReq.send();
   }

function C2CcheckForm(data)
   {
    var badnum = false;

    if (data.name.value.length < 2)
       {
        alert("Please enter your name.");
        return false;
       }

    if (data.areacode.value.length == 0)
       {
        alert("Please select your area code.");
        return false;
       }

    tmpnum = data.phonenum.value.replace(/[^0-9]/g,"");

    if (data.areacode.value.match(/^\+61/))
       {
        //-------------------------------------------------------------
        // Australia
        if (data.areacode.value == "+614")
           {
            if ((tmpnum.substr(0,2) == "04") && (tmpnum.length == 10))
               {
                tmpnum = tmpnum.substr(2,8);
               }

            if (tmpnum.length != 8 || !data.phonenum.value.match(/^(04){0,1}[0-9\-\.\ ]*[0-9]$/))
               badnum = true;
           }
         else
           {
            if ((tmpnum.length != 8) || !data.phonenum.value.match(/^[2-9][0-9\-\.\ ]*[0-9]$/))
                badnum = true;
           }
       }
    else if (data.areacode.value.match(/^\+64/))
       {
        //-------------------------------------------------------------
        // New Zealand
        if (data.areacode.value == "+642")
           {
            if ((tmpnum.substr(0,2) == "02") && (tmpnum.length >= 9) && (tmpnum.length <= 11))
               {
                tmpnum = tmpnum.substr(2, tmpnum.length - 2);
               }

            if ((tmpnum.length < 7) || (tmpnum.length > 8) || !data.phonenum.value.match(/^(02){0,1}[0-9\-\.\ ]*[0-9]$/))
               badnum = true;
           }
         else
           {
            if ((tmpnum.length != 7) || !data.phonenum.value.match(/^[2-9][0-9\-\.\ ]*[0-9]$/))
                badnum = true;
           }
       }
    else
       {
        //-------------------------------------------------------------
        // Default to Australian National Format
        if (data.areacode.value == "04")
           {
            if ((tmpnum.substr(0,2) == "04") && (tmpnum.length == 10))
               {
                tmpnum = tmpnum.substr(2,8);
               }

            if (tmpnum.length != 8 || !data.phonenum.value.match(/^(04){0,1}[0-9\-\. \ ]*[0-9]$/))
               badnum = true;
           }
         else
           {
            if ((tmpnum.length != 8) || !data.phonenum.value.match(/^[2-9][0-9\-\.\ ]*[0-9]$/))
                badnum = true;
           }

       }

    if (badnum)
       {
        alert("Please enter your telephone number using only numerals, dashes, spaces and dots.\nExamples include:\n\t0412 345 678 for a mobile\n\t1234 5678 for a landline");
        return false;
       }

    data.phone.value = data.areacode.value + tmpnum;

    if (data.day)
       {
        // Queing data in place
        data.queue_t.value = data.day.value + ' ' + data.hour.value + ':' + data.minute.value + ':00';
       }

    return true;
   }

function C2Cpopup()
   {
    window.open(location.protocol.toLowerCase() + '//c2c01.ulterius.net/c2c/7866d8d34c4828a66f833e9724dfe9d9/call','','menubar=no,toolbar=no,status=no,resizable=no,scrollbars=no,width=300,height=375');
   }

function C2CresetForm(data)
   {
    data.name.value='';
    data.areacode.selectedIndex=0;
    data.phonenum.value='';
   }

function C2Cinitialise()
   {
    form = document.click2call;

    if (form.day)
       {
        // Setup Queueing
        for (day in c2c_day_array)
           {
            var opt = document.createElement('option');
            opt.text = c2c_day_array[day];
            opt.value = c2c_date_array[day];
            try
               {
                form.day.add(opt, null);
               }
            catch(ex)
              {
               form.day.add(opt); // IE only
              }
           }

        C2Cdaychange(form);
       }
   }

function C2Cdaychange(form)
   {
    currentday = form.day.selectedIndex;

    for (i = form.hour.length; i >= 0; --i)
       {
        form.hour.remove(i);
       }

    for (hour in c2c_hour_array[currentday])
       {
        var opt = document.createElement('option');
        opt.text = c2c_hour_array[currentday][hour];
        opt.value = c2c_hour_array[currentday][hour];
        try
           {
            form.hour.add(opt, null);
           }
        catch(ex)
          {
           form.hour.add(opt); // IE only
          }
       }

    C2Chourchange(form)
   }

function C2Chourchange(form)
   {
    for (i = form.minute.length; i >= 0; --i)
       {
        form.minute.remove(i);
       }

    for (minute in c2c_minute_array[form.day.selectedIndex][form.hour.selectedIndex])
       {
        var opt = document.createElement('option');
        opt.text = c2c_minute_array[form.day.selectedIndex][form.hour.selectedIndex][minute];
        opt.value = c2c_minute_array[form.day.selectedIndex][form.hour.selectedIndex][minute];
        try
           {
            form.minute.add(opt, null);
           }
        catch(ex)
          {
           form.minute.add(opt); // IE only
          }
       }
   }

