$('head').append('<link rel="stylesheet" href="http://www.firstbeatmedia.info/teligence/teligence.css" type="text/css" />');

$.getScript('http://www.firstbeatmedia.info/teligence/jquery.placeholder.js', function(){
    $('input').placeholder();
});


$('#teligence-ctc p:first').html('In seconds your phone will ring and connect you to local singles. By entering your number above you agree to receive text alerts from RedHot Dateline. Standard Message & Data Rates May Apply. Text <span class="bold">STOP</span> to <span class="bold">89888</span> to cancel. Text <span class="bold">HELP</span> to <span class="bold">89888</span> for help.');

var area = false;
var phone = false;

var codes = [201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 212, 213, 214, 215, 216, 217, 218, 219, 224, 225, 226, 228, 229, 231, 234, 239, 240, 242, 246, 248, 250, 251, 252, 253, 254, 256, 260, 262, 264, 267, 268, 269, 270, 276, 281, 284, 289, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 323, 325, 330, 334, 336, 337, 339, 340, 345, 347, 351, 352, 360, 361, 386, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 412, 413, 414, 415, 416, 417, 418, 419, 423, 424, 425, 430, 432, 434, 435, 438, 440, 441, 443, 450, 456, 469, 473, 478, 479, 480, 484, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 512, 513, 514, 515, 516, 517, 518, 519, 520, 530, 540, 541, 551, 559, 561, 562, 563, 567, 570, 571, 573, 574, 580, 585, 586, 600, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 612, 613, 614, 615, 616, 617, 618, 619, 620, 623, 626, 630, 631, 636, 641, 646, 647, 649, 650, 651, 660, 661, 662, 664, 670, 671, 678, 682, 684, 700, 701, 702, 703, 704, 705, 706, 707, 708, 709, 710, 712, 713, 714, 715, 716, 717, 718, 719, 720, 724, 727, 731, 732, 734, 740, 754, 757, 758, 760, 762, 763, 765, 767, 769, 770, 772, 773, 774, 775, 778, 780, 781, 784, 785, 786, 787, 800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 810, 812, 813, 814, 815, 816, 817, 818, 819, 828, 829, 830, 831, 832, 843, 845, 847, 848, 850, 856, 857, 858, 859, 860, 862, 863, 864, 865, 866, 867, 868, 869, 870, 876, 877, 878, 888, 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 912, 913, 914, 915, 916, 917, 918, 919, 920, 925, 928, 931, 936, 937, 939, 940, 941, 947, 949, 951, 952, 954, 956, 970, 971, 972, 973, 978, 979, 980, 985, 989];

function validate(input)
{
    
    var self = $('#teligence-ctc form input[name=' + input + ']');
    
    if(input == 'area_code')
    {    
        var val = parseInt( self.val(), 10 );
        var x = $.inArray( val, codes );
        if(x == -1)
        {
            self.addClass('red');
        }
        else
        {
            area = true;
            self.removeClass();
            self.addClass('green');
        }
    }
    
    if(input == 'phone_number')
    {    
        if(self.val().length == 7)
        {
            phone = true;
            self.removeClass();
            self.addClass('green');
        }
        else
        {
            phone = false;
            self.addClass('red');
        }
    }

}

function submit()
{

    var message;

    validate('area_code');
    validate('phone_number');

    if(area == false || phone == false) return;

	$('#teligence-response').html('<center><img src="http://www.firstbeatmedia.info/teligence/loader.gif"></center>').show(0);
	
    var response = $.ajax({
                          url: "/post.php",
                          global: false,
                          type: "POST",
                          data: $('#teligence-ctc form').serialize(),
                          async: false,
                          success: function(msg){
                          
                          }
                       }
                    ).responseText;
	
    if(response == '200')
	{
        message = 'You will receive a call soon to connect you to the chat line.';
		var image = $('<img />').attr('src', 'http://www.firstbeatmedia.info/teligence/image.png?' + $('#teligence-ctc form').serialize());
		$('body').append(image);
    }
	else
	{
        message = 'Something went wrong please try again!';
    }
    
    $('#teligence-response').html(message);

}

function partner(data)
{
    $('input[name=partnerID]').val(data.group);
}

$(document).ready(function()
{
    
	$('#teligence-submit').removeAttr('onclick').attr('href', 'javascript:submit();');   
    
    $('input').attr('autocomplete', 'off').removeAttr('maxlength');
    
    $('input[name=area_code],input[name=phone_number]').change(function() {
        var val = $(this).val();
		val = val.replace(/\D+/g, '');
        $(this).val(val);
        validate($(this).attr('name'));
    });

    var url = window.location.href;
    url = url.match(/[\w\-]{5,}/);
    $('input[name=atid]').val(url);
    
    var keywords = $('meta[name=keywords]').attr('content').replace(/,/g, '');
    $('input[name=Keyword]').val(keywords);
    
    var url = window.location.href;
    var url = url.match(/([\w\-]+)\.(com|net|info|org)/g, '');
    $.getJSON("http://www.firstbeatmedia.info/teligence/group.php?domain=" + url + "&callback=?", function(data){
        partner(data);
    });
    
});

function include(url, callback)
{
    var head = document.getElementsByTagName('head')[0];
    var element = document.createElement('script');
    element.src = url;

    element.onreadystatechange = callback;
    element.onload = callback;

    head.appendChild(element);
}

