function showLoading(eleName, type){
	if (type == 1){
		$(eleName).set('html','<img src="/img/loading_arrow_small.gif" alt="loading..." />');
	}
}

function toggleElement(eleName){
	if ($(eleName).getStyle('display') == 'none'){
		$(eleName).setStyle('display','block');	
	}else {
		$(eleName).setStyle('display','none');	
	}
}

function toggleLoginBox(option, element, value, isPassword){
	addClass(element, 'loginBoxFocus');
	if (option == 0 && element.value == value){
		element.value = '';
	
	}else if (option == 1 && element.value == ''){
		removeClass(element, 'loginBoxFocus');
		element.value = value;
	}
}

function toggleSearchBox(option, element, value){
	addClass(element, 'searchBoxHighlight');
	if (option == 0 && element.value == value){
		element.value = '';
	
	}else if (option == 1 && element.value == ''){
		element.value = value;
		removeClass(element, 'searchBoxHighlight');
	}
}

function toggleTextarea(option, element, value){

	addClass(element, 'textareaHighlight');
	if (option == 0 && element.value == value){
		element.value = '';
	
	}else if (option == 1 && element.value == ''){
		element.value = value;
		removeClass(element, 'textareaHighlight');
	}
}

function toggleSelectBox(element){
	if (element.value == ''){
		removeClass(element, 'searchBoxHighlight');
	}else {
		addClass(element, 'searchBoxHighlight');
	}
}

function toggleSearchCheckBox(element, checkbox){
	if (checkbox.checked){
		addClass(element, 'searchCheckBoxHighlight');
	}else {
		removeClass(element, 'searchCheckBoxHighlight');
	}
}

function showPasswordBox(){
	$('passDisplay').setStyle('display','none');
	$('password').setStyle('display','inline-block');
	$('password').focus();
}

function bizitLogin(rootUrl, encodedRedirect, encodedCallback){
	top.location.href = rootUrl+"/user/login/"+(encodedRedirect==""?0:encodedRedirect)+"/"+encodedCallback;
}

function goToTop(){
	new Fx.Scroll(window).toTop();
}

function showImage(rootUrl, imageUrl){
	new MooDialog.Iframe(rootUrl+"/home/showImage/"+imageUrl, {
		size: {width:800,height:600}
	});
}

function viewProductDetails(rootUrl, salesproductid){
	new MooDialog.Iframe(rootUrl+"/shop/viewProductDetails/"+salesproductid, {
		size: {width:500,height:600}
	});
}

function bizitProfile(rootUrl, bizitid){
	new MooDialog.Iframe(rootUrl+"/user/bizitprofile/"+bizitid, {
		size: {width:500,height:650}
	});
}

function searchByGear(gearid){
	$('searchGearid').value=gearid;
	$('searchGearForm').submit();
}

function loadMyMartproductCountList(rootUrl){
	var myRequest = new Request({onSuccess:function(responseText){
									var decodedObj = JSON.decode(responseText);

									$('soldlist_total').set('html',decodedObj.soldlist);
									$('normallist_total').set('html',decodedObj.mylist);
									$('qslist_total').set('html',decodedObj.qslist);
									
								}}).get(rootUrl+'/mart/get_countlist/?ts='+new Date().getTime());
}

function loadMyMartproductSoldList(rootUrl, pagenum){
	$('myMartSoldTab').removeClass('mart_dark_tag');
	$('myMartItemTab').removeClass('mart_red_tag');
	$('myMartQsTab').removeClass('mart_grey_tag');
	$('myMartSoldTab').removeClass('mart_dark_tag_selected');
	$('myMartItemTab').removeClass('mart_red_tag_selected');
	$('myMartQsTab').removeClass('mart_grey_tag_selected');
	
	$('myMartSoldTab').addClass('mart_dark_tag_selected');
	$('myMartItemTab').addClass('mart_red_tag');
	$('myMartQsTab').addClass('mart_grey_tag');
	
	$('martMyList').set('html','<img src="/main/images/loading_arrow_small.gif" alt="loading..." />');
	$('martMyListPaging').set('html','');
	var myRequest = new Request({onSuccess:function(responseText){
									var decodedObj = JSON.decode(responseText);
									$('martMyList').set('html', decodedObj.martproductList);
									$('martMyListPaging').set('html', decodedObj.martproductPaging);
								}}).get(rootUrl+'/mart/soldlist/'+pagenum+'?ts='+new Date().getTime());
}

function loadMyMartproductList(rootUrl, pagenum){
	$('myMartSoldTab').removeClass('mart_dark_tag');
	$('myMartItemTab').removeClass('mart_red_tag');
	$('myMartQsTab').removeClass('mart_grey_tag');
	$('myMartSoldTab').removeClass('mart_dark_tag_selected');
	$('myMartItemTab').removeClass('mart_red_tag_selected');
	$('myMartQsTab').removeClass('mart_grey_tag_selected');
	
	$('myMartSoldTab').addClass('mart_dark_tag');
	$('myMartItemTab').addClass('mart_red_tag_selected');
	$('myMartQsTab').addClass('mart_grey_tag');
	
	$('martMyList').set('html','<img src="/main/images/loading_arrow_small.gif" alt="loading..." />');
	$('martMyListPaging').set('html','');
	var myRequest = new Request({onSuccess:function(responseText){
									var decodedObj = JSON.decode(responseText);
									$('martMyList').set('html', decodedObj.martproductList);
									$('martMyListPaging').set('html', decodedObj.martproductPaging);
								}}).get(rootUrl+'/mart/mylist/'+pagenum+'?ts='+new Date().getTime());
}

function loadQsList(rootUrl, pagenum){
	$('myMartSoldTab').removeClass('mart_dark_tag');
	$('myMartItemTab').removeClass('mart_red_tag');
	$('myMartQsTab').removeClass('mart_grey_tag');
	$('myMartSoldTab').removeClass('mart_dark_tag_selected');
	$('myMartItemTab').removeClass('mart_red_tag_selected');
	$('myMartQsTab').removeClass('mart_grey_tag_selected');
	
	$('myMartSoldTab').addClass('mart_dark_tag');
	$('myMartItemTab').addClass('mart_red_tag');
	$('myMartQsTab').addClass('mart_grey_tag_selected');

	$('martMyList').set('html','<img src="/main/images/loading_arrow_small.gif" alt="loading..." />');
	$('martMyListPaging').set('html','');
	var myRequest = new Request({onSuccess:function(responseText){
									var decodedObj = JSON.decode(responseText);
									$('martMyList').set('html', decodedObj.qsList);
									$('martMyListPaging').set('html', decodedObj.qsPaging);
								}}).get(rootUrl+'/mart/qslist/'+pagenum+'?ts='+new Date().getTime());
}

function soldMartproduct(soldStr, cancelStr, confirmStr, martproductid, rootUrl, pagenum){
	if (confirm(soldStr)){
		var myRequest = new Request({onSuccess:function(responseText){
									loadMyMartproductList(rootUrl, pagenum);
								}}).get(rootUrl+'/mart/sold/'+martproductid+'?ts='+new Date().getTime());
	}
	//new MooDialog.Confirm(soldStr,cancelStr,confirmStr,function(){
	//	var myRequest = new Request({onSuccess:function(responseText){
	//								loadMyMartproductList(rootUrl, pagenum);
	//							}}).get(rootUrl+'/mart/sold/'+martproductid+'?ts='+new Date().getTime());
	//});
}

function removeMartproduct(removeStr, cancelStr, confirmStr, martproductid, rootUrl, pagenum){
	if (confirm(removeStr)){
		var myRequest = new Request({onSuccess:function(responseText){
									loadMyMartproductList(rootUrl, pagenum);
								}}).get(rootUrl+'/mart/remove/'+martproductid+'?ts='+new Date().getTime());
	}
	//new MooDialog.Confirm(removeStr,cancelStr,confirmStr,function(){
	//	var myRequest = new Request({onSuccess:function(responseText){
	//								loadMyMartproductList(rootUrl, pagenum);
	//							}}).get(rootUrl+'/mart/remove/'+martproductid+'?ts='+new Date().getTime());
	//});
}

function removeMartproductQs(removeStr, cancelStr, confirmStr, mqid, rootUrl){
	if (confirm(removeStr)){
		var myRequest = new Request({onSuccess:function(responseText){
										window.location.reload();
									}}).get(rootUrl+'/mart/removeQs/'+mqid+'?ts='+new Date().getTime());
	}
	//new MooDialog.Confirm(removeStr,cancelStr,confirmStr,function(){
	//	var myRequest = new Request({onSuccess:function(responseText){
	//								window.location.reload();
	//							}}).get(rootUrl+'/mart/removeQs/'+mqid+'?ts='+new Date().getTime());
	//});
}

function reportMartproduct(rootUrl, martproductid){
	new MooDialog.Iframe(rootUrl+'/mart/report/'+martproductid, {
		size: {width:410,height:350}
	});
}

function contactMartproductSeller(rootUrl, martproductid){
	new MooDialog.Iframe(rootUrl+'/mart/contact/'+martproductid, {
		size: {width:410,height:350}
	});
}

/* --------------- */
/* dom ready event */
/* --------------- */
window.addEvent('domready', function(){
	new Tips('.tip', {text:null, className:'tipOne'});
	new Tips('.lefttip', {text:null, className:'tipOne', showDelay:500});
	milkbox = new Milkbox();
	
	//$$('object').each(function(el){
	//	el.destroy();
	//});
	
	//$(".embedHolder object").append('');
	//$(".embedHolder embed").attr("wmode", "transparent");
	
});


/* --------------- */
/* utilities       */
/* --------------- */
var delimiter1 = "@|%~";

function convertKeyValue(inputStr){
	var keys = new Array;
	var values = new Array;
	var splitOne = inputStr.split("&");
	splitOne.each(function(item, index){
		var splitTwo = item.split("=");
		if (splitTwo.length == 2){
			keys.push(splitTwo[0]);
			values.push(splitTwo[1]);
		}
	});
	return values.associate(keys);
}

function func_htmlspecialchars(string, quote_style) {

   string = string.toString();
  
   string = string.replace(/&/g, '&amp;');
   string = string.replace(/</g, '&lt;');
   string = string.replace(/>/g, '&gt;');
  
   if (quote_style == 'ENT_QUOTES') {
       string = string.replace(/"/g, '&quot;');
       string = string.replace(/\'/g, '&#039;');
   } else if (quote_style != 'ENT_NOQUOTES') {
       string = string.replace(/"/g, '&quot;');
   }
  
   return string;
}

function func_htmlspecialchars_decode(string, quote_style) {

   string = string.toString();
  
   string = string.replace(/&amp;/g, '&');
   string = string.replace(/&lt;/g, '<');
   string = string.replace(/&gt;/g, '>');
  
   if (quote_style == 'ENT_QUOTES') {
       string = string.replace(/&quot;/g, '"');
       string = string.replace(/&#039;/g, '\'');
   } else if (quote_style != 'ENT_NOQUOTES') {
       string = string.replace(/&quot;/g, '"');
   }
  
   return string;
}

function func_stripTag(value) {
	return value.replace(/(<([^>]+)>)/ig,"");
}

function limitText(limitField, limitNum) {
	if ($(limitField).value.length > limitNum) {
		$(limitField).value = $(limitField).value.substring(0, limitNum);
	} 
}

function addClass(element, className){
	if (element.className.indexOf(className) == -1){
		element.className += " " + className;
	}
}

function removeClass(element, className){
	var pos = element.className.indexOf(className);
	if (pos != -1){
		element.className = element.className.substr(0,pos) + element.className.substr(pos+className.length);
	}
}
