var active = false;

// HORIZONTAL TICKER
$(document).ready(function() {
    $("ul#ticker01").liScroll(); 
});

// LIMIT CHARS
function textCounter(field, cntfield, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	} else {
		cntfield.value = maxlimit - field.value.length;
	}
}

// SUBMIT ACTIONS
function submitAction(action) {
	document.resFrm.action.value = action;
	if (action == "edit") {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++) {
			if (inputs[i].type == 'checkbox' && inputs[i].checked == 1 && inputs[i].value != "on") {
				document.resFrm.edit.value = inputs[i].value;
			}
		}
	}
	document.resFrm.submit();
}

// CHECK/UNCHECK ALL
function selectAll() {
	var fr = document.getElementsByTagName('input');
	for (var i = 0; i < fr.length; ++i)
		if (fr[i].type == 'checkbox') {
			fr[i].checked = document.getElementById('selectall').checked;
		}
}

function selectAllById(select_id, element_id) {
	var fr = document.getElementsByTagName('input');
	for (var i = 0; i < fr.length; ++i)
		if (fr[i].type == 'checkbox') {
			if (fr[i].id.substr(0,3) == element_id) {
				fr[i].checked = document.getElementById(select_id).checked;
			}
		}
}

// DELETE CUSTOM ROUTE
function deleteRoute(id) {
	$.ajax({
	    url: 'ajax.php?function=deleteroute',
	    type: 'POST',
	    cache: false,
	    dataType: 'text',
	    data: 'id='+id,
	    error: function(){
	        alert('Network error. Please retry.');
	    },
	    success: function(response) {
	    	if (response == 'ok') {
				$('#route_'+id).fadeOut(function (){ $(this).remove(); }); 
			}
	    }
	});
}

// ADD NEW ROUTE FIELD
function newRoute() {
	var content = '<tr><td class="noborder"><input type="text" class="form_input" name="prefix[]" value="" onblur="this.className=\'form_input\'" onfocus="this.className=\'form_input_hover\'" style="width: 120px;" /></td><td>';
	var allroutes = $('#allroutes').html();
	content += allroutes;
	content += '</td><td><input type="text" class="form_input" name="charge[]" value="0.00" onblur="this.className=\'form_input\'" onfocus="this.className=\'form_input_hover\'" style="width: 50px;" /></td><td align="center"></td><input type="hidden" name="route_type[]" value="new" /></tr>';
	$(content).hide().appendTo("#custom_routes").fadeIn("slow");
}

// HOMEPAGE SLIDER
function slidePic(id) {
	$('.btn').removeClass('select');
	$('#btn'+id).addClass('select');
	var src = "imgs/slide"+id+".jpg";
	$("#current_slide").fadeOut(200, function(){
		$("#current_slide").attr('src',src).bind('readystatechange load', function(){
			if (this.complete) $(this).fadeIn(400);
		});
	});
}

// PAYPAL REDIRECT
function PayPal(userid) {
	if (document.getElementById('amount').value < 10) {
		alert('The minimum amount is 10 EUR.');
	} else {
		var amount = document.getElementById('amount').value;
		var paypal_email = "paypal@itechlogic.net";
		var return_url = "http://www.smscable.com/my-account.html";
		var cancel_url = "http://www.smscable.com/index.html";
		var notify_url = "http://www.smscable.com/paypal_ipn.php";
		$.ajax({
		    url: 'ajax.php?function=calcvat',
		    type: 'GET',
		    cache: false,
		    dataType: 'text',
		    data: 'amount='+amount,
		    error: function(){
		        alert('Network error. Please retry.');
		    },
		    success: function(response) {
		    	amount = response;
		    	var redirect = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business="+escape(paypal_email)+"&item_name=SMS%20Credits&amount="+escape(amount)+"&no_shipping=1&no_note=1&currency_code=EUR&lc=US&bn=PP-BuyNowBF&charset=UTF-8&on0=uid&os0="+userid+"&return="+escape(return_url)+"&cancel_return="+escape(cancel_url)+"&notify_url="+escape(notify_url);
		    	window.location = redirect;
		    }
		});
	}
}

function startProgressTimer(cid) {
	if (!active) {
		active = !active;
		$('#progressbar').everyTime(1000, 'controlled', function() {
			$.ajax({
			    url: 'ajax.php?function=checkprogress',
			    type: 'GET',
			    cache: false,
			    dataType: 'text',
			    data: 'cid='+cid,
			    error: function(){
			        // error here
			    },
			    success: function(response){
			    	$('#progressbar').progressBar(response);
			        if (response == '100') {
			        	stopProgressTimer();
			        }
			    }
			});
		});
	}
}

function stopProgressTimer(cid) {
	if (active) {
		active = !active;
		$('#progressbar').stopTime('controlled');
	}
}

function addtext(id) {
	var newtext = '#'+$('#'+id).val()+'#';
	$('#message').val($('#message').val()+newtext);
	$('#message').focus();
}
