/* JQuery */

$(function() {
	$('a.lightbox').lightBox(); // Select all links with lightbox class
	
    $("select#topcat").change(function(){
		$.getJSON("/index.php",{catid: $(this).val(), pg: 'category-json'}, function(j){
		  var options = '<option value="0">All Sub Categories</option>';
		  
		  for (var i = 0; i < j.length; i++) {
			options += '<option value="' + j[i].value + '">' + j[i].title + '</option>';
		  }
		  $("select#subcat").html(options);
		})
	})
	
});



/* end JQuery */

function subscribe_newsletter() {
	
	var name=$("#subscribe_name").attr('value');
	var email=$("#subscribe_email").attr('value');
	
	var fail=0; error="";
	
	if ((name=="") || (name=="Enter your name")) { error='Please enter your name\n'; fail=1; }
	if ((email=="") || (email=="Enter your email address")) { error+='Please enter your Email Address'; fail=1; }
	if ((!isValidEmail(email)) && (email!="Enter your email address")) { error+='Please enter correct Email Address'; fail=1; }

	if (fail==0) {
		var dataString="pg=subscribe&name=" + name + "&email=" + email;
		
		$.ajax({  
		   type: "GET",  
		   url: "index.php",  
		   data: dataString,  
		   success: function(msg) {  
				document.getElementById('message').innerHTML='<span class="message-text">' + msg + '</span>';
		   }  
		});  
	} else {
		alert(error);
	}
     
}

function sub_focus(id,obj) {

	switch (id) {
		case 1: if (obj.value=="Enter your name") { obj.value=''; } break;
		case 2: if (obj.value=="Enter your email address") { obj.value=''; } break;
	}
}

function sub_blur(id,obj) {

	switch (id) {
		case 1: if (obj.value=="") { obj.value='Enter your name'; } break;
		case 2: if (obj.value=="") { obj.value='Enter your email address'; } break;
	}
}



function DoDel(iserial)
{
	if (confirm("Are you sure?"))
	{
		window.location.href=iserial;
	}
}

function DoQuit()
{
	if (confirm("Exit without save changes?"))
	{
		window.location.href='my_refarm.php';
	}
}

function show_window(sid,h,w)
{

  var popup = window.open("aa", "News",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=' + h + ',height=' + w);
  popup.location = sid;

}

function show_bar_window(sid,h,w)
{

  var popup = window.open("", "News",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=' + h + ',height=' + w);
  popup.location = sid;

}


function checkUncheckAll(theElement) {
	 //documentation for this script at http://www.shawnolson.net/a/693/
    var theForm = theElement.form, z = 0;
    while (theForm[z].type == 'checkbox' && theForm[z].name != 'checkall') {
     theForm[z].checked = theElement.checked;
     z++;
    }
}




function group_action(obj) {

	document.frm_list.run.value=obj.value;
	document.frm_list.submit();
	
}

/* Update cart price when choose different plan */
function update_cart_price(price) {
	
	var tmp=price.split("-")
		
	var total=document.getElementById('cart_total').value;
	var temp=parseFloat(total)+parseFloat(tmp[0]);
	
	document.getElementById('cart_price').innerHTML=formatCurrency(temp);
	total.value=temp;
	
}

/* update store content on store box */
function update_store(id,total) {
	
	for (i=1;i<=total;i++) {
		document.getElementById('store' + i).style.display='none';
	}

	document.getElementById('store' + id).style.display='';
	
}

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)?'':'-') + '$' + num + '.' + cents);
}

// Cart-Step 3 toggle billing form
function toggle_form() {
	var frm=document.getElementById('billing-table');
	
	if (frm.className=='') { frm.className='close'; } else { frm.className=''; }
	
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

	
