$(function() 
{
	$("input.number_only").keypress(function(e)
		{
			 var unicode=e.charCode? e.charCode : e.keyCode
		            if (unicode!=8)
		            {
		            	//if the key isn't the backspace key (which we should allow)
		                     if (unicode<48||unicode>57) //if not a number
		                     	return false //disable key press
		            }
		}
	);
	$("input#is_use_default_tax").change(function()
		{ 
			var is_checked = $("input#is_use_default_tax:checked").val();
			if(is_checked == undefined)
			{
				$('#override_tax').removeAttr("disabled"); 
			}
			else
			{
				$('#override_tax').attr("disabled", true); 
			}
		}
	);
	if(($('div#tab_container').length > 0))
	{
		$('div#tab_container').tabs({ fxFade: true, fxSpeed: 'fast' });
	}

	if(($('div#tab_container_product_link').length > 0))
	{
		$('div#tab_container_product_link').tabs({ fxFade: true, fxSpeed: 'fast' });
	}

});

$(function()
{
	$("input.phone_format").keypress(function(e)
	{
		var unicode=e.charCode? e.charCode : e.keyCode;
		if (unicode!=8 && unicode!=43 && unicode!=41 && unicode!=40 && unicode!=44 && unicode!=32)
		{
			//if the key isn't the backspace key (which we should allow)
			if (unicode<48||unicode>57) //if not a number
				return false //disable key press
		}
	});
}
);

function createRequestObject() {
		var ro;
		var browser = navigator.appName;
		if(browser == "Microsoft Internet Explorer"){
			ro = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			ro = new XMLHttpRequest();
		}
		return ro;
	}
	
	var http = createRequestObject();
function set_selected_value(select_element_name, value)
{
	$("#"+select_element_name).value = value;
	//alert($("#"+select_element_name).selectedValue = value);
}

function currencyFormat(fld, e) 
{	
	var key = '';
	var i = 0;
	var len;
	var strCheck = '0123456789.,';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if(whichCode == null) whichCode = e.keyCode;
	if (whichCode == 13) return true; // Enter
	if (whichCode == 8) return true; // Delete 
	key = String.fromCharCode(whichCode); // Get key value from key code
	len = fld.value.length;	
	if (strCheck.indexOf(key) == -1) return false; // Not a valid key
		
	if(key == '.')
	{
		if(hasPoint(fld.value))
		{
			return false;
		}
	}
	
	if(len == 0 && key == '.')
	{
		return false;
	}
}

function hasPoint(aux)
{
	if(aux.indexOf('.') != -1)
	{
		return true;
	}
	return false;
}
var iR=0;
function change_Image(maxR,current_item){	
	iR=current_item;
	ShowHideLayer(maxR);
}

function ShowHideLayer(maxR){
	for(var i = 0 ; i < maxR ; i ++) {
			var xControl = document.getElementById('imgR'+i);	
			if(i==iR) {				
				xControl.className="showLayer";				
			}
			else {
				xControl.className="hideLayer";
			}		
		}
}

function confirmDelete()
{
	return confirm("Are you sure you want to delete?");
		
	}

function confirm_delete(url)
{
	jConfirm('Are you sure you want to delete?', 'Confirmation Dialog', function(r) {
	if( r ) 
	{
		window.location = url;
	}
	else { } 
	});
	return false; 
};
	
function check_checkbox(doc)
{
  var j = 0;
  var k = 0;
  var c = new Array();
  c = doc.getElementsByTagName('input');
  for (var i = 0; i < c.length; i++)
  {
    if (c[i].type == 'checkbox' && c[i].checked == true && c[i].name == 'cb[]')
    {
      j++;
    }
	
	if (c[i].type == 'checkbox' && c[i].checked == true && c[i].name == 'attribute')
    {
      k++;
    }
	if (c[i].type == 'checkbox' && c[i].checked == true && c[i].name == 'category')
    {
      k++;
    }
	if (c[i].type == 'checkbox' && c[i].checked == true && c[i].name == 'tax')
    {
      k++;
    }
	if (c[i].type == 'checkbox' && c[i].checked == true && c[i].name == 'option')
    {
      k++;
    }
	if (c[i].type == 'checkbox' && c[i].checked == true && c[i].name == 'series')
    {
      k++;
    }
  }
  
  if(j<=0)
  {
	jAlert("Please select at least one product!", "Alert Dialog");
	return false;		
  }
    
  if(k<=0)
  {
	jAlert("Please select one property to update!", "Alert Dialog");
	return false;
  }
  
  return true;
  
};

function checkAll(doc) {
   
  var c = new Array();
  var selected = doc.frmproductlist.checkall.value;
  c = doc.getElementsByTagName('input');
  for (var i = 0; i < c.length; i++)
  {
	if(selected == "1")
	{
		if (c[i].type == 'checkbox')
		{
		  c[i].checked = true;
		}
	}
	else
	{
		if (c[i].type == 'checkbox')
		{
		  c[i].checked = false;
		}
	}
  }
}

	
function handleResponse() {
		if(http.readyState == 4){
			var response = http.responseText;
			var update = new Array();

			if (response.indexOf('|' != -1)) {
				update = response.split('|');
				document.getElementById(update[0]).innerHTML = update[1];
			}
		}
	}
	
function ajax_paginate(sort, page, count, direction, next_page, keywork, parent_cate_id, sub_cate_id) {
		document.cookie  = "paginate_sort=" + sort;
		document.cookie = "paginate_page=" + page;
		document.cookie = "paginate_count=" + count;
		document.cookie = "paginate_direction=" + direction;
		http.open('get', 'rpc.php?action=ajax_paginate&paginate_sort='+sort+'&paginate_page='+page+'&paginate_count='+count+'&paginate_direction='+direction);
		window.location = "index.php?next_page="+next_page+"&paginate_sort="+sort+"&paginate_page="+page+"&paginate_count="+count+"&paginate_direction="+direction+"&search_key_words="+keywork+"&category="+parent_cate_id+"&sub_category="+sub_cate_id;
		http.onreadystatechange = handleResponse;
		http.send(null);
}

function ajax_update_is_searchable(img_object, attribute_id)
{
	$(img_object).attr({src : "media/images/loading.gif"});
	$.ajax({
		type: "GET",
		url: "rpc.php",
		data: "action=update_attribute_is_searchable&attribute_id="+attribute_id+"&random="+Math.random(),
		success: function(is_searchable)
		{
			if(is_searchable == 0)
			{
				$(img_object).attr({src : "media/images/x.png"});
			}
			else
			{
				$(img_object).attr({src : "media/images/check.png"});
			}
		}
	});

}
