// JavaScript Document
function fConfirm(deletepageurl, id, displayOrder, message){
	if (confirm(message)){
		location.href = deletepageurl + "?id=" + id + "&order=" + displayOrder;
	}
}

function openCenteredWindow(url,w,h) {
	var width = w;
	var height = h;
	var left = parseInt((screen.availWidth/2) - (width/2));
	var top = parseInt((screen.availHeight/2) - (height/2));
	var windowFeatures = "width=" + width + ",height=" + height + ",resizable=yes, scrollbars=yes,left=" + left + ",top=" + top + ",screenX=" + left + ",screenY=" + top;
	myWindow = window.open(url, "subWind", windowFeatures);
}

function viewlargeimage(){
	e = document.getElementById("hdnImg");
	if (navigator.appName == "Microsoft Internet Explorer"){
		openCenteredWindow("viewlargeimage.asp?ssrc=" + e.src, 640, 480);
	}
	else
	{
		w = 640;
		h = 480;
		if (w <= 0) w = screen.availWidth;
		if (h <= 0) h = screen.availHeight;
		if (w > screen.availWidth) w = screen.availWidth;
		if (h > screen.availHeight) h = screen.availHeight;
		{
			openCenteredWindow("viewlargeimage.asp?ssrc=" + e.src, w, h);
		}
	}
}

function fReset(){
	document.myForm.username.value="";
	document.myForm.password.value="";
	document.myForm.fullname.value="";
	document.myForm.address.value="";
	document.myForm.phone.value="";
	document.myForm.cellphone.value="";
	document.myForm.email.value="";
}

// validates that the field value string has one or more characters in it
function isNotEmpty(str) {
    var re = /.+/;
    if(!str.match(re)) {
        return false;
    } else {
        return true;
    }
}
   
//validates that the entry is a positive or negative number
function isNumber(str) {
    var re = /^[-]?\d*\.?\d*$/;
    str = str.toString( );
    if (!str.match(re)) {
        return false;
    }
    return true;
}

// validates that the entry is formatted as an email address
function isEMailAddr(str) {
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!str.match(re)) {
        return false;
    } else {
        return true;
    }
}

function btnUpdateClicked(num){
	var i=0;
	for (i=0;i<num;i++){
		if (document.getElementById("qty" + i)){
			if ((!isNumber(document.getElementById("qty" + i).value)) || (!isNotEmpty(document.getElementById("qty" + i).value))){
				alert("So khong hop le!");
				return false;
			}
		}
	}
	document.getElementById('hidden').value='1';
	document.myForm.submit();
}

function btnAddToCartClicked(){
	if (document.getElementById("qty")){
		if ((!isNumber(document.getElementById("qty").value)) || (!isNotEmpty(document.getElementById("qty").value))){
			alert("So khong hop le!");
			return false;
		}
	}
	document.myForm.submit();
}

function btnSignUpClicked(){
	var fullname, username, password, email, address, phone, cellphone
	username = document.myForm.username.value;
	password = document.myForm.password.value;
	fullname = document.myForm.fullname.value;
	address = document.myForm.address.value;
	phone = document.myForm.phone.value;
	cellphone = document.myForm.cellphone.value;
	email = document.myForm.email.value;
	if (!isNotEmpty(fullname)){
		alert("Ho va Ten khong the trong!");
		return; 
	}
	if (!isNotEmpty(username)){
		alert("Ten dang nhap khong the trong!");
		return; 
	}
	if (!isNotEmpty(password)){
		alert("Mat khau khong the trong!");
		return; 
	}
	if (!isNotEmpty(email)){
		alert("Email khong the trong!");
		return; 
	}
	if (!isEMailAddr(email)){
		alert("Email khong hop le!");
		return; 
	}
	if (!isNotEmpty(address)){
		alert("Dia chi khong the trong!");
		return; 
	}
	if (!isNotEmpty(phone)){
		alert("So dien thoai khong the trong!");
		return; 
	}
	document.myForm.hidden.value=1;
	document.myForm.submit();
}