﻿var baseprice = 7.50;
var inlcudesWords = 15;
var extrawordPrice = .5;
var numberofRun = 0;
var words = 0;

$(document).ready(function(){

    $('.ad textarea').keyup(classified_changed);    
    $('.schedule-date').change(classified_onDateChanged);
    
    $('.cost-baseprice').html(formatCurrency(baseprice));
    classified_onDateChanged();
    classified_changed();
    
});

function classified_onDateChanged(){
    var input = $('input[id$=hfDates]');
    var arr = [];
    var text = input.val();
    
    if(text.length > 0)
       arr = text.split(',');
    
       
    var date = this.value;
    
    if(this.checked){
       arr.push(date);
    }else{
        var newArr = [];
        for(var i = 0; i < arr.length ; i++){
            if(arr[i] != date)
                newArr.push(arr[i]);
                    
        }
        arr = newArr;
    }
    
    input.val(arr.join(','));
    numberofRun = arr.length;
      
    classified_recalculate(words);
}

function classified_changed(){
    var text = $('.ad textarea');
    
    words = wordCount(text);
    $('.wordcount span').html(words);
   
    classified_recalculate(words)
}



function classified_recalculate(numberOfWords){

    //$('.cost-baseprice').html();
    
    //$('.cost-baseprice').html();
    
    var extraWords = numberOfWords - inlcudesWords;
    var extraWordCost = 0;
    
    if(extraWords > 0){
        extraWordCost = extraWords * extrawordPrice;
        $('.cost-extraword').html(formatCurrency(extraWordCost));
    }else{
        $('.cost-extraword').html(formatCurrency(0));
    }   
    
    var totalPrice = numberofRun * (baseprice + extraWordCost);

    $('input[id$=hfTotalPrice]').val(totalPrice);
    $('input[id$=hfTotalWords]').val(numberOfWords);
    
    $('.cost-numberofspots').html(numberofRun);
    $('.cost-total').html( formatCurrency( totalPrice) );


}

function wordCount(textarea) {
 var prev = "";
 var current = "";
 var totalWords = 0;

 var myText = textarea.val();
 var charLength = parseInt(myText.length);

 // increment word and char counters
 for (i=0; i < charLength; i++) {
 current = myText.charAt(i);
 if ((current == " ") && (prev != " ") && (prev != "\n") && (prev != "\r")) totalWords++;
 if (((current == "\n") || (current == "\r")) && (!(prev == " ") && !(prev == "\n") && !(prev == "\r"))) totalWords++;
 prev = current;
 }

 // account for the last word
 if ((charLength != 0) && (current != " ") && (current != "\n") && (current != "\r")) totalWords++;

 return totalWords;
}


function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$&nbsp;' + num + '.' + cents);
}

function verify(){

    var errors = [];
    var valid = true;
    
    
    var temp = $('input[id$=tbAvertiserName]');
    var txt = temp.val();
    if(txt == null || temp.val().length < 3){
            errors.push('Valid company or individual name');
            temp.addClass('notvalid');
            valid = false;
    }else{
             temp.removeClass('notvalid');
    }
    
    temp = $('input[id$=tbBillingAddress]');
    txt = temp.val();
    if(txt == null || temp.val().length < 3){
            errors.push('Address');
            temp.addClass('notvalid');
            valid = false;
    }else{
             temp.removeClass('notvalid');
    }
    
    temp = $('input[id$=tbCity]');
    txt = temp.val();
    if(txt == null || temp.val().length < 3){
            errors.push('City');
            temp.addClass('notvalid');
            valid = false;
    }else{
             temp.removeClass('notvalid');
    }
    
    temp = $('input[id$=tbPostal]');
    txt = temp.val();
    if(txt == null || temp.val().length != 5){
            errors.push('ZIP Code');
            temp.addClass('notvalid');
            valid = false;
    }else{
             temp.removeClass('notvalid');
    }
    
    
    temp = $('input[id$=tbPhone]');
    txt = temp.val();
    if(txt == null || txt.length < 7){
            errors.push('Day Phone');
            temp.addClass('notvalid');
            valid = false;
    }else{
             temp.removeClass('notvalid');
    }
    
  
    temp = $('input[id$=tbEmail]');
    txt = temp.val();
    if(txt == null || temp.val().length < 4){
            errors.push('Email');
            temp.addClass('notvalid');
            valid = false;
    }else{
            temp.removeClass('notvalid');
    }
    
    temp = $('input[id$=tbEmailVerify]');
    if(txt != temp.val()){
            errors.push('Verify E-Mail');
            temp.addClass('notvalid');
            valid = false;
    }else{
            temp.removeClass('notvalid');
    }
    
    temp = $('input[id$=hfTotalWords]');
    if( temp.val() < 4){
            errors.push('Classified ad text must have more than 5 words');
            $('textarea[id$=tbAdText]').addClass('notvalid');
            valid = false;
    }else{
            $('textarea[id$=tbAdText]').removeClass('notvalid');
    }
    
    temp = $('input[id$=hfDates]');
    txt = temp.val();
      if(txt == null || temp.val().length < 1){
            errors.push('Please select at least one date.');
            $('.schedule').addClass('notvalid');
            valid = false;
    }else{
            $('.schedule').removeClass('notvalid');
    }
    
    if(valid == false){
    if($('.messages').length == 0){
        $('.schedule').after('<div class="messages"></div>');
    }
    
     $('.messages').html('<h3>Please Provide</h3><ul><li>' + errors.join('</li><li>')  + '</li></ul>');
    }

    return valid;

}