
// JavaScript Document

/* Globals */
var gTimemoutID;


// After page loads do all these functiuons 
$(document).ready(function(){
  				
	InitApp();
    SetAppVarsOnCFM();	
	SetPrice();		   
						   
/////dialog boxes
	$('#dialogStateSelect').dialog({
			height: 400,
			width: 600,
			modal: true,
			autoOpen: false,
			title: 'Select States',	
			position: 'center',
			buttons: { 
        "Continue": CloseDialogStateSelect}
		});
	$('#dialogTerms').dialog({
			height: 800,
			width: 700,
			modal: true,
			autoOpen: false,
			title: 'Terms and Conditions',	
			position: 'center'
			//,
			//buttons: { 
        //"Continue": ContinueTerms,
		//"Cancel": CancelTerms
		//}
		});
// scrollable anchors
$("a.anchorLink").anchorAnimate()

///////// Bind Events
	$('#selectstate').click(OpenStateSelect);
    $('#btnstep2').click(OpenDialogTerms);
	 $('#useragreement').click(OpenDialogTerms);
	 $('#useragreement2').click(OpenDialogTerms);
	$('#btnstep1').click(ProcessStep1);
//////////////////////////////////////
}) // end document ready

function InitApp()
/* Initializes/Resets App object */
{
     App = new objApp();
}
function objApp()
/* Appobject */
{
     this.appid = '';
	 this.sessionid = '';
     this.firstname = '';
     this.lastname = '';
	 this.vendor='';
	 this.ipstamp = '';
	 this.hostname= '';
	 this.pathinfo = '';
	 this.refstring = '';
	 this.querystring = '';
	 this.phone = '';
	 this.email ='';
	 this.address = '';
	 this.city = '';
	 this.state = '';
	 this.zip ='';
	 this.licno = '';
	 this.agreement = '';
	 this.subscribedstates = '';
	 this.subscribedprice = '';
	 this.price = '';
	 this.subreferer = '';
	 this.name ='';
	 this.chargedate = '';
	 this.status = '';
	 this.marketingcampaign = '';
	 this.repid = '';
	 this.vendorid = '';
	 this.affid = '';
	 this.campaignid = '';
	 this.promoid = '';
	 this.pricefile = '';
	 this.passthrough = '';
	 this.freeweek='';
	 this.freemonth='';
	 // the values are set for cgi vars on cfm page on cf/code.cfm
}


function ProcessStep1()
{
	//function ValidateFirstName()
//{
	if($("#firstname").val().length < 1)
	   {
	   	alert('Please enter First Name');
		return false;
	   }
	
//}


//function ValidateLastName()
//{
	if($("#lastname").val().length < 2)
	   {
	   	alert('Please enter Last Name');
		return false;
	   }
	
//}

//function ValidateEmail()
//{
 var RE_EMAIL = /^(\w+[\-\.])*\w+@(\w+\.)+[A-Za-z]+$/;
	if(!RE_EMAIL.test($("#email").val()))
		{
			alert('Please enter a valid email address');
			return false;
   		 }
//}	
	
//function ValidatePhone()
//{  
    var RE_NUMONLY =  /^\s*\d+\s*$/;
	if(!RE_NUMONLY.test($("#phone1").val()) || !RE_NUMONLY.test($("#phone2").val()) || !RE_NUMONLY.test($("#phone3").val())
    	|| $("#phone1").val().length != 3 || $("#phone2").val().length != 3 || $("#phone3").val().length != 4)
		{
			alert('Phone enter 10 digit phone number');
			return false;
		}
		
	var phone = '';
//}	
//function ValidateZipCode()
//{
	 var RE_NUMONLY =  /^\s*\d+\s*$/;
	if(!RE_NUMONLY.test($("#zip").val()) || $("#zip").val().length != 5)
	{
		alert('Please enter 5 digit zip code');
		return false;
		
    }
//}
 
 	var address = $('#address').val();
	var strText = jQuery.trim($('#address').val());
	if(strText.split(/\s+/).length < 3 )
	{
	   alert('Enter valid street address include street number, street name and street suffix like ST,DR,AV,PKW');
	    return false;
	}
	
		//function ValidateCity()
//{
	if($("#city").val().length < 2)
	   {
	   	alert('Please enter city.');
		return false;
	   }
	
	if($("#state").val() == 0)
	   {
	   	alert('Please enter state.');
		return false;
	   }
	
	if(App.subscribedstates.length < 2)
	{
		
		alert('You must choose at least one state. Click the Select States Link');
		OpenStateSelect();
		return false;
	}
	
	// Set variables 
	
	var firstname = $('#firstname').val();
	firstname = firstname.toLowerCase().replace(/\b[a-z]/g, convert); 
	function convert() {           return arguments[0].toUpperCase();     } 
	
	
	
	var lastname = $('#lastname').val();
	lastname = lastname.toLowerCase().replace(/\b[a-z]/g, convert); 
	function convert() {           return arguments[0].toUpperCase();     } 
	
	var address = $('#address').val();
	address = address.toLowerCase().replace(/\b[a-z]/g, convert); 
	function convert() {           return arguments[0].toUpperCase();     } 
	
	var city = $('#city').val();
	city = city.toLowerCase().replace(/\b[a-z]/g, convert); 
	function convert() {           return arguments[0].toUpperCase();     } 
	
	App.firstname = firstname;
	App.lastname = lastname;
	App.name = App.firstname + ' ' + App.lastname;
	App.address = address;
	App.city = city;
	App.state = $('#state').val();
	App.email = $('#email').val();
	App.zip = $('#zip').val();
	App.licno = $('#licno').val();
	App.phone = $('#phone1').val() + $('#phone2').val() + $('#phone3').val();
    // Fill in Span text
	$('#spanlegalname').text(App.name);
	
	// Engineering
	$('.step1').hide();
	$('.signup').show();
	//$('.step2').show();
	var statefee = App.price;
	var states = App.subscribedstates;
	//alert(App.price);
	
	
	var proxy = new cfcUHL(); 
	 var data = proxy.InsertClient(App.firstname,App.lastname,App.address,App.city,App.state,App.zip,App.phone,App.email,App.licno,App.appid,App.repid,App.promoid,App.marketingcampaign,states,statefee);
	 	var clientid = data.CLIENTID;
		//$('#newclientid').val(data.CLIENTID);
	
	var ppdesc = 'Unlimited Leads Subscription States| ' + App.subscribedstates +  ' |  $' + App.subscribedprice + '| clientid=' + data.CLIENTID;
	$('#ppdescription').val(ppdesc);
	
		var custom = clientid + '|' + states +  '|' + App.price + '|' +  App.subscribedprice + '|' + App.repid + '|' + App.vendorid + '|' + App.affid + '|' + App.passthrough + '|' + App.promoid;
		//alert(custom);
      $('#custom').val(custom);
	// alert(JSON.stringify(data));
	 //alert($('#newclientid').val());
}

function OpenStateSelect()
{
	 $('#dialogStateSelect').dialog('open');	
}
function CloseDialogStateSelect()
{
	 $('#dialogStateSelect').dialog('close');	
	 CheckStates();
}

function CheckStates()
{
	var price = 0;
	var states = '';
	if($("input[name='ak']").is(":checked"))
	{
	$('#ak').val(1);
	states = states + 'AK,';
	price = price + App.price;
	$('#spanstatecost').text(price);
	}else{
	$('#ak').val(0);
	}	
	if($("input[name='al']").is(":checked"))
	{
	$('#al').val(1);
	states = states + 'AL,';
	price = price + App.price;
	}else{
	$('#al').val(0);
	}	
	if($("input[name='ar']").is(":checked"))
	{
	$('#ar').val(1);
	states = states + 'AR,';
	price = price + App.price;
	}else{
	$('#ar').val(0);
	}	
	if($("input[name='az']").is(":checked"))
	{
	$('#az').val(1);
	states = states + 'AZ,';
	price = price + App.price;
	}else{
	$('#az').val(0);
	}	
	if($("input[name='ca']").is(":checked"))
	{
	$('#ca').val(1);
	states = states + 'CA,';
	price = price + App.price;
	}else{
	$('#ca').val(0);
	}	
	if($("input[name='co']").is(":checked"))
	{
	$('#co').val(1);
	states = states + 'CO,';
	price = price + App.price;
	}else{
	$('#co').val(0);
	}	
	if($("input[name='ct']").is(":checked"))
	{
	$('#ct').val(1);
	states = states + 'CT,';
	price = price + App.price;
	}else{
	$('#ct').val(0);
	}	
	if($("input[name='dc']").is(":checked"))
	{
	$('#dc').val(1);
	states = states + 'DC,';
	price = price + App.price;
	}else{
	$('#dc').val(0);
	}	
	if($("input[name='de']").is(":checked"))
	{
	$('#de').val(1);
	states = states + 'DE,';
	price = price + App.price;
	}else{
	$('#de').val(0);
	}	
	if($("input[name='fl']").is(":checked"))
	{
	$('#fl').val(1);
	states = states + 'FL,';
	price = price + App.price;
	}else{
	$('#fl').val(0);
	}	
	if($("input[name='ga']").is(":checked"))
	{
	$('#ga').val(1);
	states = states + 'GA,';
	price = price + App.price;
	}else{
	$('#ga').val(0);
	}	
	if($("input[name='hi']").is(":checked"))
	{
	$('#hi').val(1);
	states = states + 'HI,';
	price = price + App.price;
	}else{
	$('#hi').val(0);
	}	
	if($("input[name='ia']").is(":checked"))
	{
	$('#ia').val(1);
	states = states + 'IA,';
	price = price + App.price;
	}else{
	$('#ia').val(0);
	}	
	if($("input[name='id']").is(":checked"))
	{
	$('#id').val(1);
	states = states + 'ID,';
	price = price + App.price;
	}else{
	$('#id').val(0);
	}	
	if($("input[name='il']").is(":checked"))
	{
	$('#il').val(1);
	states = states + 'IL,';
	price = price + App.price;
	}else{
	$('#il').val(0);
	}	
	if($("input[name='in']").is(":checked"))
	{
	$('#in').val(1);
	states = states + 'IN,';
	price = price + App.price;
	}else{
	$('#in').val(0);
	}	
	if($("input[name='ks']").is(":checked"))
	{
	$('#ks').val(1);
	states = states + 'KS,';
	price = price + App.price;
	}else{
	$('#ks').val(0);
	}	
	if($("input[name='ky']").is(":checked"))
	{
	$('#ky').val(1);
	states = states + 'KY,';
	price = price + App.price;
	}else{
	$('#ky').val(0);
	}	
	if($("input[name='la']").is(":checked"))
	{
	$('#la').val(1);
	states = states + 'LA,';
	price = price + App.price;
	}else{
	$('#la').val(0);
	}	
	if($("input[name='ma']").is(":checked"))
	{
	$('#ma').val(1);
	states = states + 'MA,';
	price = price + App.price;
	}else{
	$('#ma').val(0);
	}	
	if($("input[name='md']").is(":checked"))
	{
	$('#md').val(1);
	states = states + 'MD,';
	price = price + App.price;
	}else{
	$('#md').val(0);
	}	
	if($("input[name='me']").is(":checked"))
	{
	$('#me').val(1);
	states = states + 'ME,';
	price = price + App.price;
	}else{
	$('#me').val(0);
	}	
	if($("input[name='mi']").is(":checked"))
	{
	$('#mi').val(1);
	states = states + 'MI,';
	price = price + App.price;
	}else{
	$('#mi').val(0);
	}	
	if($("input[name='mn']").is(":checked"))
	{
	$('#mn').val(1);
	states = states + 'MN,';
	price = price + App.price;
	}else{
	$('#mn').val(0);
	}	
	if($("input[name='mo']").is(":checked"))
	{
	$('#mo').val(1);
	states = states + 'MO,';
	price = price + App.price;
	}else{
	$('#mo').val(0);
	}	
	if($("input[name='ms']").is(":checked"))
	{
	$('#ms').val(1);
	states = states + 'MS,';
	price = price + App.price;
	}else{
	$('#ms').val(0);
	}	
	if($("input[name='mt']").is(":checked"))
	{
	$('#mt').val(1);
	states = states + 'MT,';
	price = price + App.price;
	}else{
	$('#mt').val(0);
	}	
	if($("input[name='nc']").is(":checked"))
	{
	$('#nc').val(1);
	states = states + 'NC,';
	price = price + App.price;
	}else{
	$('#nc').val(0);
	}	
	if($("input[name='nd']").is(":checked"))
	{
	$('#nd').val(1);
	states = states + 'ND,';
	price = price + App.price;
	}else{
	$('#nd').val(0);
	}	
	if($("input[name='ne']").is(":checked"))
	{
	$('#ne').val(1);
	states = states + 'NE,';
	price = price + App.price;
	}else{
	$('#ne').val(0);
	}	
	if($("input[name='nh']").is(":checked"))
	{
	$('#nh').val(1);
	states = states + 'NH,';
	price = price + App.price;
	}else{
	$('#nh').val(0);
	}	
	if($("input[name='nj']").is(":checked"))
	{
	$('#nj').val(1);
	states = states + 'NJ,';
	price = price + App.price;
	}else{
	$('#nj').val(0);
	}	
	if($("input[name='nm']").is(":checked"))
	{
	$('#nm').val(1);
	states = states + 'NM,';
	price = price + App.price;
	}else{
	$('#nm').val(0);
	}	
	if($("input[name='nv']").is(":checked"))
	{
	$('#nv').val(1);
	states = states + 'NV,';
	price = price + App.price;
	}else{
	$('#nv').val(0);
	}	
	if($("input[name='ny']").is(":checked"))
	{
	$('#ny').val(1);
	states = states + 'NY,';
	price = price + App.price;
	}else{
	$('#ny').val(0);
	}	
	if($("input[name='oh']").is(":checked"))
	{
	$('#oh').val(1);
	states = states + 'OH,';
	price = price + App.price;
	}else{
	$('#oh').val(0);
	}	
	if($("input[name='ok']").is(":checked"))
	{
	$('#ok').val(1);
	states = states + 'OK,';
	price = price + App.price;
	}else{
	$('#ok').val(0);
	}	
	if($("input[name='or']").is(":checked"))
	{
	$('#or').val(1);
	states = states + 'OR,';
	price = price + App.price;
	}else{
	$('#or').val(0);
	}	
	if($("input[name='pa']").is(":checked"))
	{
	$('#pa').val(1);
	states = states + 'PA,';
	price = price + App.price;
	}else{
	$('#pa').val(0);
	}	
	if($("input[name='ri']").is(":checked"))
	{
	$('#ri').val(1);
	states = states + 'RI,';
	price = price + App.price;
	}else{
	$('#ri').val(0);
	}	
	if($("input[name='sc']").is(":checked"))
	{
	$('#sc').val(1);
	states = states + 'SC,';
	price = price + App.price;
	}else{
	$('#sc').val(0);
	}	
	if($("input[name='sd']").is(":checked"))
	{
	$('#sd').val(1);
	states = states + 'SC,';
	price = price + App.price;
	}else{
	$('#sd').val(0);
	}	
	if($("input[name='tn']").is(":checked"))
	{
	$('#tn').val(1);
	states = states + 'TN,';
	price = price + App.price;
	}else{
	$('#tn').val(0);
	}	
	if($("input[name='tx']").is(":checked"))
	{
	$('#tx').val(1);
	states = states + 'TX,';
	price = price + App.price;
	}else{
	$('#tx').val(0);
	}	
	if($("input[name='ut']").is(":checked"))
	{
	$('#ut').val(1);
	states = states + 'UT,';
	price = price + App.price;
	}else{
	$('#ut').val(0);
	}	
	if($("input[name='va']").is(":checked"))
	{
	$('#va').val(1);
	states = states + 'VA,';
	price = price + App.price;
	}else{
	$('#va').val(0);
	}	
	if($("input[name='vi']").is(":checked"))
	{
	$('#vi').val(1);
	states = states + 'VI,';
	price = price + App.price;
	}else{
	$('#vi').val(0);
	}	
	if($("input[name='vt']").is(":checked"))
	{
	$('#vt').val(1);
	states = states + 'VT,';
	price = price + App.price;
	}else{
	$('#vt').val(0);
	}	
	if($("input[name='wa']").is(":checked"))
	{
	$('#wa').val(1);
	states = states + 'WA,';
	price = price + App.price;
	}else{
	$('#wa').val(0);
	}	
	if($("input[name='wi']").is(":checked"))
	{
	$('#wi').val(1);
	states = states + 'WI,';
	price = price + App.price;
	}else{
	$('#wi').val(0);
	}	
	if($("input[name='wv']").is(":checked"))
	{
	$('#wv').val(1);
	states = states + 'WV,';
	price = price + App.price;
	}else{
	$('#wv').val(0);
	}
	if($("input[name='wy']").is(":checked"))
	{
	$('#wy').val(1);
	states = states + 'WY,';
	price = price + App.price;
	}else{
	$('#wy').val(0);
	}
	var strlength = String(states).length;
    var str1 = states.substr(strlength-1);
    if(str1 == ',')
	{
		states= states.substr(0,strlength-1);
	}
	//test comma extraction on end od states string
	//alert(strlength + 'and '+ str1 + 'states ' + states); 
	
	var strchangestates = ' <span id=changestate onclick=ChangeStates(); class=spanlink>Change</span>' 
	App.subscribedstates = states;
	App.subscribedprice = price;
	//alert(price);
	$('#spanstatecost').html(App.subscribedprice + strchangestates);
	$('#spanmonthlyprice').text(App.subscribedprice);
	$('#spanstatesselected').html(App.subscribedstates + strchangestates);
	$('#spantotaltosignup').text(App.subscribedprice);
	$('#spanstatesordered').html(App.subscribedstates + strchangestates);
	$('#newprice').val(App.subscribedprice);
	$('#a3').val(App.subscribedprice); // pp price
	ProcessStep1();
//alert($('#ppdescription').val());
	//$('.hideaway').show();
	//alert('states' + App.subscribedstates +'price = ' + App.subscribedprice);
	
	//var proxy = new cfcUHL(); 
	} // end function CheckStates()
		
	
function OpenDialogTerms()
{

	 $('#dialogTerms').dialog('open');	

}
function CloseDialogTerms()
{

	 $('#dialogTerms').dialog('close');	

}

function CancelTerms()
{

	CloseDialogTerms();

}
function ContinueTerms()
{
    
	if($('#eSignature').val() ==  App.name)
	{
	 ProcessSubscription();
	}
	else
	{
	alert('Please Type your name as shown  ' + App.name)
	return false;
	}
	CloseDialogTerms();

}
function ProcessSubscription()
{
	alert('processing');
	$('.step2').hide();
	$('.thanks').show();
	
}

function SetPrice()
{
	//alert(App.price);
	$('#spanprice').text(App.price);
	$('#spanprice1').text(App.price);
	$('#spanSetPrice').text(App.price);
	var freemonthhtml = '<h2>Sign up today and the First Month is FREE!</h2>';
	var freeweekhtml = '<h2>Sign up today and the First Week of leads are FREE!</h2>';

	var freeleadhtml = '';
	
	if(App.freemonth == 1)
		{
			$('#spanFreeMonth').html(freemonthhtml);	
			$('.freeaccount').hide();
		}
		if(App.freeweek == 1)
		{
			$('#spanFreeWeek').html(freeweekhtml);	
			$('.freeaccount').hide();
		}
	
	// set chargedate
	var curdate = new Date();
    var mday = curdate.getDate();
    if(mday > 28)
	{
		App.chargedate = 1;
	}
	else
	{
		App.chargedate = mday;
	}
	$('#spanchargedate').text(App.chargedate);
	// SET iP
	$('#spanip').text(App.ipstamp);
// Show content Hide loading
    $('.loading').hide();
	$('.doneloading').show();
	var imagepricehtml = '<img src="images/unlimited.gif" class="unlimited" /><img src="images/leads.gif" class="leads_green"/><img src="images/' + App.pricefile +'" class=price id='+ '" imagePrice' + '"/>'
	
	
	$('#divPrice').html(imagepricehtml);
	
	
}		
function CloseAll()
{
	CloseDialogTerms();
    CloseDialogStateSelect();
}
function ChangeStates()
{
	CloseAll();
	OpenStateSelect();
}
////////////
///This is for scrolling anchor text
jQuery.fn.anchorAnimate = function(settings) {

 	settings = jQuery.extend({
		speed : 1100
	}, settings);	
	
	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")
			
			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				window.location.hash = elementClick
			});
		  	return false;
		})
	})
}
//////////// END SCROLLING anchor text
