var ie=navigator.appName=="Microsoft Internet Explorer";
var nn=navigator.appName=="Netscape";
var font_family="Arial, Helvetica, sans-serif";
var font_size=11;

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function RemovePx(thetext){
	re=/px/;
	return Number(thetext.replace(re,""));
}

function PopupImage(thefile){
	var re=/_big./;
	thefile=document.getElementById(thefile).src.replace(re,".");
	PopupWin2(thefile,640,480);
}

function PopupWin(bookURL){
	var winTop = (screen.height /2) - 90;
	var winLeft = (screen.width /2) - 150;
	var winFeat = "menubar=no,status=yes,toolbar=no,scrollable=yes,height=150,width=340";
	winFeat = winFeat+",top="+winTop+",left="+winLeft;
	var newWin = window.open(bookURL,'popup'+Math.round(Math.random()*99),winFeat);
}

function PopupWin2(bookURL,w,h){
	var winLeft = (screen.width /2) - (w/2);
	var winTop = (screen.height /2) - (h/2);
	var winFeat = "menubar=no,status=yes,toolbar=no,resizable=yes,scrollbars=yes,height="+h+",width="+w;
	winFeat = winFeat+",top="+winTop+",left="+winLeft;
	var newWin = window.open(bookURL,'popup'+Math.round(Math.random()*99),winFeat);
}

function ToThumb(image){
	dot=image.lastIndexOf(".");
	if(dot!=-1){
		filename=image.substr(0,dot);
		format=image.substr(dot+1,image.length-dot);
		return filename+"_thumb."+format;
	}else{
		return image;
	}
}

function ShowHide(target){
	var idtarget=document.getElementById(target);
	if(idtarget.style.display=="none"){
		idtarget.style.display=""
	}else{
		idtarget.style.display="none";
	}
}

function FontMin(target_id){
	target=document.getElementById(target_id);
	if(target!=null){
		if(font_size>12){
			font_size-=2;
			//target.style.fontSize=font_size+"px";
			targetchildren=target.getElementsByTagName("*");
			for (i=0; i<targetchildren.length; i++){
				targetchildren[i].style.fontFamily=font_family;
				targetchildren[i].style.fontSize=font_size+"px";
			}
		}
	}
}

function FontPlus(target_id){
	target=document.getElementById(target_id);
	if(target!=null){
		if(font_size<23){
			font_size+=2;
			//target.style.fontSize=font_size+"px";
			targetchildren=target.getElementsByTagName("*");
			for (i=0; i<targetchildren.length; i++){
				targetchildren[i].style.fontFamily=font_family;
				targetchildren[i].style.fontSize=font_size+"px";
			}
		}
	}
}

function HideSuperTop(){
	ShowHide('super_top_wrapper_1');
	document.getElementById('btn_maximize').style.display="inline";
}

function ShowSuperTop(){
	ShowHide("super_top_wrapper_1");
	document.getElementById('btn_maximize').style.display="none";
}

function HideTop(){
	ShowHide('top_wrapper_1');
	document.getElementById('top_liner_1').style.display="inline";
}

function ShowTop(){
	ShowHide("top_wrapper_1");
	document.getElementById('top_liner_1').style.display="none";
}


function AlertAndBack(the_message){
	alert(the_message);
	history.back();
}

function AlertAndGoto(the_message,the_url){
	alert(the_message);
	location.href=the_url;
}

function ChooseTheme(){
	location.href="../../store_choose_theme.php";
}

function ShowMessageLoading(){
	location.href="#";
	target=document.getElementById("popup_message");
	target.style.display="";
	target.style.height=document.body.parentNode.clientHeight+"px";
	document.getElementById("popup_message_content").innerHTML="<div align='center' style='padding-top:40px; height:40px;'><img src='../../images/loading.gif'></div>";
}

function ShowWindowLoading(){
	location.href="#";
	target=document.getElementById("popup_window");
	target.style.display="";
	target.style.height=document.body.parentNode.clientHeight+"px";
	document.getElementById("popup_window_content").innerHTML="<div align='center' style='padding-top:120px; height:120px;'><img src='../../images/loading.gif'></div>";
}

function ShowWindowProcess(){
	document.getElementById("input_process").style.display="none";
	document.getElementById("loading_process").style.display="";
}

function AddCartDone(the_url){
	target=document.getElementById("popup_window");
	target.style.display="";
	target.style.height=document.body.parentNode.clientHeight+"px";
	document.getElementById("popup_window_content").innerHTML="<div align='center' style='padding-top:120px; height:120px;'><img src='../../images/loading.gif'></div>";
	ajaxpage(the_url,"popup_window_content");
}

function MessageLoad(the_url){
	ShowMessageLoading();
	ajaxpage(the_url,"popup_message_content");
}

function WindowLoad(the_url){
	ShowWindowLoading();
	ajaxpage(the_url,"popup_window_content");
}

function CustomizeCSS(){
	WindowLoad("../../store_edit_css_ajax.php");
}

function ChangeWelcomeMessage(){
	ShowWindowLoading();
	ajaxpageanddofunction("../../store_edit_welcome_ajax.php", "popup_window_content", "ActivateCKEditor");
	//WindowLoad("../../store_edit_welcome_ajax.php");
}

function ChangeAboutUs(){
	ShowWindowLoading();
	//WindowLoad("../../store_edit_about_ajax.php");
	ajaxpageanddofunction("../../store_edit_about_ajax.php", "popup_window_content", "ActivateCKEditor");
}

function ChangeInfo(){
	ShowWindowLoading();
	ajaxpageanddofunction("../../store_edit_info_ajax.php", "popup_window_content", "ActivateCKEditor");
	//WindowLoad("../../store_edit_info_ajax.php");
}

function ChangeAnnouncement(){
	ShowWindowLoading();
	ajaxpageanddofunction("../../store_edit_announcement_ajax.php", "popup_window_content", "ActivateCKEditor");
	//WindowLoad("../../store_edit_announcement_ajax.php");
}

function ActivateCKEditor(){
	CKEDITOR.replace( 'myckeditor' );	
}

function ChangeHeader(){
	WindowLoad("../../store_upload_header_ajax.php");
}

function ChangeFooter(){
	WindowLoad("../../store_upload_footer_ajax.php");
}

function Refresh(){
	location.href="../../store_refresh.php";
}

function ClosePopupWindow(){
	if(typeof CKEDITOR!="undefined"){
		if(CKEDITOR.instances.myckeditor){
			CKEDITOR.instances.myckeditor.destroy();
		}
	}
	target=document.getElementById("popup_window");
	target.style.display="none";
}

function ClosePopupMessage(){
	target=document.getElementById("popup_message");
	target.style.display="none";
}

function CheckCommentForm(){
	if( !NoBlank("form_comment","txt_name","Please fill the name") )return false;
	if( !NoBlank("form_comment","txa_comment","Please fill the comment") )return false;
	if(document.getElementById("txt_comment_security_code")!=null){
		if( !NoBlank("form_comment","txt_comment_security_code","Please fill the security code from image on the left") )return false;
	}else{
		if( !NoBlank("form_comment","txt_security_code","Please fill the security code from image on the left") )return false;
	}
	return true;
}

function CheckProductCommentForm(){
	if( !NoBlank("form_product_comment","txt_product_name","Please fill the name") )return false;
	if( !NoBlank("form_product_comment","txa_product_comment","Please fill the comment") )return false;
	if(document.getElementById("txt_product_security_code")!=null){
		if( !NoBlank("form_product_comment","txt_product_security_code","Please fill the security code from image on the left") )return false;
	}else{
		if( !NoBlank("form_product_comment","txt_security_code","Please fill the security code from image on the left") )return false;
	}
	return true;
}

function CheckTestimonialForm(){
	if( !NoBlank("form_testimonial","txt_name","Please fill the name") )return false;
	if( !NoBlank("form_testimonial","txa_testimonial","Please fill the testimonial") )return false;
	if(document.getElementById("txt_testimonial_security_code")!=null){
		if( !NoBlank("form_testimonial","txt_testimonial_security_code","Please fill the security code from image on the left") )return false;
	}else{
		if( !NoBlank("form_testimonial","txt_security_code","Please fill the security code from image on the left") )return false;
	}
	return true;
}

function CheckContactForm(){
	if( !NoBlank("form_contact","txt_contact_name","Please fill the name") )return false;
	if( !EmailValid("form_contact","txt_contact_email") )return false;
	if( !NoBlank("form_contact","txt_contact_subject","Please fill the subject") )return false;
	if( !NoBlank("form_contact","txa_contact_message","Please fill the message") )return false;
	if(document.getElementById("txt_contact_security_code")!=null){
		if( !NoBlank("form_contact","txt_contact_security_code","Please fill the security code from image on the left") )return false;
	}else{
		if( !NoBlank("form_contact","txt_security_code","Please fill the security code from image on the left") )return false;
	}
	return true;
}

function CheckOrderForm(the_item_count){
	if(the_item_count==0){
		alert("Shopping cart still empty");
		return false;
	}
	if( !NoBlank("form_order","txt_order_name","Please fill the name") )return false;
	if( !EmailValid("form_order","txt_order_email") )return false;
	if( !NoBlank("form_order","txt_order_subject","Please fill the subject") )return false;
	if( !NoBlank("form_order","txa_order_message","Please fill the message") )return false;
	if( !NoBlank("form_order","txa_delivery_address","Please fill the address") )return false;
	if(document.getElementById("txt_order_security_code")!=null){
		if( !NoBlank("form_contact","txt_order_security_code","Please fill the security code from image on the left") )return false;
	}else{
		if( !NoBlank("form_order","txt_security_code","Please fill the security code from image on the left") )return false;
	}
	return true;
}

function CheckInputCart(){
	Field=document.getElementById("txtQty");
	if(isNaN(Field.value)){
		alert("Input error");
		Field.focus();
		return false;
	}
	/*if(Field.value=="0"){
		alert("Please input valid quantity");	
		Field.focus();
		return false;
	}*/
	return true;
}

function CheckConfirmPayment(){
	if( !NoBlank("formConfirmPayment","txtAmount","Please fill the amount") )return false;
	if( !NoBlank("formConfirmPayment","txtInvoice","Please fill the invoice number") )return false;	
	if( !NoBlank("formConfirmPayment","txtName","Please fill the name") )return false;
		var payment_date=document.getElementById("selMonth").value+"/"+document.getElementById("selDate").value+"/"+document.getElementById("selYear").value;
		if(!isDate(payment_date,1)){
			alert("Payment date is not valid");
			field=document.getElementById("selDate");
			field.focus();
			return false;
		}	
	return true;
}

function CheckRegistrationForm(){
	if( !NoBlank("form_register","txtName","Please fill the name") )return false;
	if( !EmailValid("form_register","txtEmail") )return false;
	if( !MustSame("form_register","txtEmail","txtEmail2","Email inconsistent") )return false;
	if( !NoBlank("form_register","txtUsername","Please fill the username") )return false;
	if( !NoBlank("form_register","txtPassword","Please fill the password") )return false;
	if( !MustSame("form_register","txtPassword","txtPassword2","Password inconsistent") )return false;
	if( !NoBlank("form_register","txtSecurityCode","Please fill the security code from what you see the image on the left") )return false;
	return true;
}

function ConfirmPayment(the_store_id,the_next_url){
	ShowWindowLoading();
	WindowLoad("../../store_confirm_payment_ajax.php?sid="+the_store_id+"&next="+the_next_url);
}

function CommentGotoPage(the_page){
	
}

function AddToCart(the_store_id,the_id,the_next_url){
	ShowWindowLoading();
	WindowLoad("../../store_add_cart_ajax.php?sid="+the_store_id+"&id="+the_id+"&next="+the_next_url);
}

function EditCart(the_store_id,the_id,the_next_url){
	ShowWindowLoading();
	WindowLoad("../../store_edit_cart_ajax.php?sid="+the_store_id+"&id="+the_id+"&next="+the_next_url);
}

function DeleteComment(the_id,the_next_url){
	ShowMessageLoading();
	MessageLoad("../../store_public_delete_comment_ajax.php?id="+the_id+"&next="+the_next_url);
}

function DeleteTestimonial(the_id,the_next_url){
	ShowMessageLoading();
	MessageLoad("../../store_public_delete_testimonial_ajax.php?id="+the_id+"&next="+the_next_url);
}

function Bookmark(the_id,the_next_url){
	ShowMessageLoading();
	MessageLoad("../../store_bookmark_ajax.php?id="+the_id+"&next="+the_next_url);
}
function BookmarkRegister(the_id,the_next_url){
	MessageLoad("../../store_bookmark_register_ajax.php?id="+the_id+"&next="+the_next_url);
}

function StoreRatingUp(the_id){
	ShowMessageLoading();
	MessageLoad("../../store_rating_up_ajax.php?id="+the_id);
}

function StoreRatingDown(the_id){
	ShowMessageLoading();
	MessageLoad("../../store_rating_down_ajax.php?id="+the_id);
}

function Logout(){
	location.href='../../member_login.php?logout=true';
}

function ViewCart(){
	location.href="cart.php";
}

function PreviewImage(the_image){
	document.getElementById("product_detail_image").src=the_image;
}

function PreviewArticleImage(the_image){
	document.getElementById("article_detail_image").src=the_image;
}