$(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, parameter_7, parameter_8) {
		document.cookie  = "paginate_sort=" + sort;
		document.cookie = "paginate_page=" + page;
		document.cookie = "paginate_count=" + count;
		document.cookie = "paginate_direction=" + direction;
		document.cookie = "search_key_words=" + keywork;
		
		var url_subfix = "";
		if(next_page == "site_admin/product_list.php")
		{
			document.cookie = "category=" + parameter_7;
			document.cookie = "sub_category=" + parameter_8;
			
			url_subfix = "&category="+parameter_7+"&sub_category="+parameter_8;
		}
		else if(next_page == "site_admin/waiting_order_list.php" || next_page == "user/your_profile.php")
		{
			document.cookie = "order_status_filter=" + parameter_7;
			url_subfix = "&order_status_filter="+ parameter_7;
		}
		
		var url = "rpc.php?action=ajax_paginate&next_page="+next_page+"&paginate_sort="+sort+"&paginate_page="+page+"&paginate_count="+count+"&paginate_direction="+direction+"&search_key_words="+keywork+url_subfix;
		http = createRequestObject(); 
        http.onreadystatechange = handleResponse;
		http.open('get', url, true);
		http.send(null);		
}

	
function ajax_paginate_data(sort, page, record_per_page, direction, next_page, keywork, make, model, series) {
		
	var url_subfix = "";
	if(next_page == "product/do_search.php");
	{
		url_subfix = "&make_id="+make+"&model_id="+model+"&series_id="+series;
	}
	var url = "rpc.php?action=ajax_paginate_data&next_page="+next_page+"&paginate_sort="+sort+"&paginate_page="+page+"&paginate_count="+record_per_page+"&paginate_direction="+direction+"&search_key_words="+keywork+url_subfix;
	http = createRequestObject();
    http.onreadystatechange = handleResponse;
	http.open('get', url, true);
	http.send(null);
}

function ajax_paginate_vehicle(page, record_per_page, next_page, model, series) {
          
    var url = "rpc.php?action=ajax_paginate_vehicle&next_page="+next_page+"&paginate_page="+page+"&paginate_count="+record_per_page+"&model_id="+model+"&series_id="+series;
    http.onreadystatechange = handleResponse;
    http.open('get', url, true);
    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"});
			}
		}
	});

}

function update_clearance(active, objname)
{
	if(active.checked == true)
	{
		var obj = document.getElementById(objname);
		if(obj != null){
			return GB_show_reload_parent('Update clearance',obj.href, 200, 500);
		}
	}
}

function check_lowest_price(current_price, entered_price, obj)
{
	if(entered_price > current_price)
	{
		jAlert("Clearance price must smaller than current lowest price!", "Alert Dialog");
		obj.value = current_price;		
	}
};

function loadRemoteContent(elementID, link)
{
    elementID = elementID.replace('#','');
    
    var targetElement = $('#'+elementID);
    
    $.ajax({
        type: "GET",
        url: link,
        data: "",
        success: function(responseHTML)
        {
            targetElement.html(responseHTML);
            
           
        },
        error:function(xhr, ajaxOptions, thrownError)
        {
            switch(xhr.status)
            {
                case 404:
                    targetElement.html("Request Not Found");
                    break;
                case 400:
                    targetElement.html("Bad Request");
                    break;
                case 403:
                    targetElement.html("Forbidden");
                    break;
                case 408:
                    targetElement.html("Request Timeout. Please contact the administrator if this message still appear.");
                    break;
                default:
                    targetElement.html("Error in processing your request, please try again.");
            }
        }
    });
} 


