function showPopup(obj)
{
	$('.product').css('z-index', '1');
	$(obj).css('z-index', '3');
	
	var imgpath = $(obj).find('#prod-img').val();
	var name = $(obj).find('#prod-name').html();
	var price = $(obj).find('#prod-price').html();
	var type = $(obj).find('#prod-type').html();
	var href = $(obj).find('#prod-name a:first').attr('href');
	var html = '<div class="popup">'+
				  '<a href="'+ href +'"><img src="'+ imgpath +'" /></a>'+
				  '<p class="prod-name">'+ name +'</p>'+
				  '<p class="prod-type">'+ type +'</p>'+
				  '<p class="prod-price">'+ price +'</p>'+
				'</div>';
	//alert(html);	
	$(obj).append(html);
	// fix IE bug
	$(obj).click(function(){document.location.href = href});
	
}

function hidePopup(obj){$(obj).find('.popup').css('display', 'none');$(obj).css('z-index', '1');}


function fixIE()
{
	$('#navi li').hover(function(){$(this).find('ul').css('display', 'block');},
						function(){$(this).find('ul').css('display', 'none')});
	
	$('#navi li ul li').hover(function(){$(this).find('ul').attr('class', 'show');},
						function(){$(this).find('ul').attr('class', 'hide')});
	
	$("a[class^='advertise']").hover(function(){$(this).find('div').css('display', 'none')},
									  function(){$(this).find('div').css('display', 'block')});
}

function sendOrder(path)
{
	$.ajax({type:'POST',
				url: path + 'sendorder.php',
				dataType : 'text',
				success: function(msg){ alert(msg); document.location.href = path.replace('actions/', '') }
		});
}

function addtocart(data)
{
	if (typeof data != 'object') return;
	$.ajax({type:'POST',
				url: data.path + 'addtocart.php',
				data: data,
				dataType : 'text',
				beforeSend: function(){ $('#status').html('Loading...');},
				success: function(msg){ $('#status').html(msg); showItemCount(data.path);}
		});
}


function showItemCount(path)
{
	$.ajax({type:'POST',
				url: path + 'addtocart.php',
				data: {action:'count'},
				dataType : 'text',
				success: function(msg){ $('#itemcount').html(msg);}
		});
}

function showOtherProduct(catId, prodId, pIndex, path, relPath)
{
	$.ajax({type:'POST',
				url: path + 'otherproduct.php',
				data:{category_id: catId,
					  product_id: prodId,
					  pageindex : pIndex,
					  path: path,
					  relative_path: relPath},
				dataType : 'text',
				beforeSend: function(){ $('#otherproduct').html('Loading...');},
				success: function(msg){ $('#otherproduct').html(msg);}
		});
}

function checkMatFrm(path)
{
	var bool		= true;
	var frm 		= document.material;
	
	if (frm.new_material_name.value == '' || frm.new_material_name.value == null)
			{ $(frm.new_material_name).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.new_material_name).css('border', '#063 1px solid');}
	
	if (bool) addMat(path, frm);
 	
	return false;
	
}

function addMat(path, frm)
{
	$.ajax({type:'POST',
				url: path + 'material.php',
				data:{new_material_name: frm.new_material_name.value,
					  old_material_name : frm.old_material_name.value},
				dataType : 'text',
				beforeSend: function(){ $('#status').html('Loading...');},
				success: function(msg){ $('#status').html(msg);}
		});
}

function checkMsgFrm(path)
{
	var bool		= true;
	var frm 		= document.message;
	var regEmail 	= /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (frm.fullname.value == '' || frm.fullname.value == null)
			{ $(frm.fullname).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.fullname).css('border', '#063 1px solid');}
	
	if (frm.phone.value == '' || frm.phone.value == null)
			{ $(frm.phone).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.phone).css('border','#063 1px solid');}
/*	
	if (frm.address.value == '' || frm.address.value == null)
			{ $(frm.address).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.address).css('border','#063 1px solid');}
	
	if (!regEmail.test(frm.email.value))
			{ $(frm.email).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.email).css('border','#063 1px solid');}
*/	
	if (frm.securitycode.value == '' || frm.securitycode.value.length != 5)
			{ $(frm.securitycode).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.securitycode).css('border','#063 1px solid');}
	
	if (bool) sendMsg(path, frm);
 	
	return false;
	
}

function sendMsg(path, frm)
{
	$.ajax({type:'POST',
				url: path + 'message.php',
				data:{fullname: frm.fullname.value,
					  email : frm.email.value,
					  phone : frm.phone.value,
					  address : frm.address.value,
					  content: frm.content.value,
					  securitycode :frm.securitycode.value},
				dataType : 'text',
				beforeSend: function(){ $('#status').html('Loading...');},
				success: function(msg){ $('#status').html(msg);}
		});
}


function checkContFrm()
{
	var bool		= true;
	var frm 		= document.contact;
	
	if (frm.yahoo.value == '' || frm.yahoo.value == null)
			{ $(frm.yahoo).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.yahoo).css('border','#063 1px solid');}
	
	if (frm.skype.value == '' || frm.skype.value == null)
			{ $(frm.skype).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.skype).css('border','#063 1px solid');}
	
	return bool;
}

function checkChangePassFrm(path)
{
	var bool		= true;
	var frm 		= document.user;
	
	if (frm.oldpass && (frm.oldpass.value == '' || frm.oldpass.value == null))
			{ $(frm.oldpass).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.oldpass).css('border','#063 1px solid');}
	
	if (frm.newpass.value == '' || frm.newpass.value == null)
			{ $(frm.newpass).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.newpass).css('border','#063 1px solid');}
	
	if (frm.repass.value !=  frm.newpass.value)
			{ $(frm.repass).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.repass).css('border','#063 1px solid');}
	
	if (bool) changePass(path, frm);
 	
	return false;
}

function changePass(path, frm)
{
	$.ajax({type:'POST',
				url: path + 'changepass.php',
				data:{oldpass : frm.oldpass.value ,
					  newpass : frm.newpass.value,
					  repass : frm.repass.value},
				dataType : 'text',
				beforeSend: function(){ $('#status').html('Loading...');},
				success: function(msg){ $('#status').html(msg);}
		});
}

function checkUsrFrm(path)
{
	var bool		= true;
	var frm 		= document.user;
	var regEmail 	= /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (frm.username && (frm.username.value == '' || frm.username.value == null))
			{ $(frm.username).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.username).css('border','#063 1px solid');}
	
	if (frm.pass && (frm.pass.value == '' || frm.pass.value == null))
			{ $(frm.pass).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.pass).css('border','#063 1px solid');}
	
	if (frm.email && !regEmail.test(frm.email.value))
			{ $(frm.email).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.email).css('border','#063 1px solid');}
	
	if (bool) addUser(path, frm);
 	
	return false;
	
}

function addUser(path, frm)
{
		$.ajax({type:'POST',
				url: path + 'user.php',
				data:{username : frm.username ? frm.username.value : null,
					  pass : 	frm.pass ? frm.pass.value : null,
					  fullname: frm.fullname.value,
					  email : 	frm.email ? frm.email.value : null,
					  phone : frm.phone.value,
					  address : frm.address.value,
					  permission: frm.permission ? frm.permission.value : null,
					  active: frm.active ? frm.active.value : null,
					  user_id :frm.user_id.value,
					  act: frm.act.value},
				dataType : 'text',
				beforeSend: function(){ $('#status').html('Loading...');},
				success: function(msg){ $('#status').html(msg);}
		});
}

function checkNewsFrm()
{
	var bool		= true;
	var frm 		= document.news;
	
	if (frm.news_title.value == '' || frm.news_title.value == null)
			{ $(frm.news_title).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.news_title).css('border','#063 1px solid');}
	
	if (frm.category_id.value == '' || frm.category_id.value == 0)
			{ $(frm.category_id).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.category_id).css('border','#063 1px solid');}
	
	return bool;
}



function checkCatFrm(path)
{
	var bool		= true;
	var frm 		= document.category;
	
	if (frm.category_name.value == '' || frm.category_name.value == null)
			{ $(frm.category_name).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.category_name).css('border','#063 1px solid');}
	
	if (bool) addCat(path, frm);
 	
	return false;
}

function addCat(path, frm)
{
	$.ajax({type:'POST',
			url: path + 'category.php',
			data:{category_name  : frm.category_name.value,
				  act			 :frm.act.value,
				  category_id 	 : frm.category_id.value,
				  active 		 : frm.active.value,
				  category_order : frm.category_order.value,
				  category_position : frm.category_position.value,
				  parent_id 		: 	frm.parent_id.value},
			dataType : 'text',
			beforeSend: function(){ $('#status').html('Loading...');},
			success: function(msg){ $('#status').html(msg);}
		});
}

function checkLogFrm(path)
{
	var bool		= true;
	var frm 		= document.login;
	
	if (frm.username.value == '' || frm.username.value == null)
			{ $(frm.username).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.username).css('border','#063 1px solid');}
	
	if (frm.pass.value == '' || frm.pass.value == null)
			{ $(frm.pass).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.pass).css('border','#063 1px solid');}
	
	if (bool) doLog(path, frm);
 	
	return false;
}

function doLog(path, frm)
{
	$.ajax({type:'POST',
			url: path + 'login.php',
			data:{username : frm.username.value,
				  pass : 	frm.pass.value},
			dataType : 'text',
			beforeSend: function(){ $('#status').html('Loading...');},
			success: function(msg){ $('#status').html(msg);}
		});
}

function checkRegFrm(path)
{
	var bool		= true;
	var frm 		= document.register;
	var regEmail 	= /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (frm.username.value == '' || frm.username.value == null)
			{ $(frm.username).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.username).css('border','#063 1px solid');}
	
	if (frm.pass.value == '' || frm.pass.value == null)
			{ $(frm.pass).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.pass).css('border','#063 1px solid');}
	
	if (!regEmail.test(frm.email.value))
			{ $(frm.email).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.email).css('border','#063 1px solid');}
	
	if (frm.securitycode.value == '' || frm.securitycode.value.length != 5)
			{ $(frm.securitycode).css('border', '#FF0000 1px solid'); bool = false;}
	else	{ $(frm.securitycode).css('border','#063 1px solid');}
	
	if (bool) doReg(path, frm);
 	
	return false;
	
}

function doReg(path, frm)
{
		$.ajax({type:'POST',
				url: path + 'register.php',
				data:{username : frm.username.value,
					  pass : 	frm.pass.value,
					  fullname: frm.fullname.value,
					  email : 	frm.email.value,
					  phone : frm.phone.value,
					  address : frm.address.value,
					  securitycode : frm.securitycode.value},
				dataType : 'text',
				beforeSend: function(){ $('#status').html('Loading...');},
				success: function(msg){ $('#status').html(msg);}
		});
}
