﻿// JavaScript Document
String.prototype.Trim = function() { 
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
}
function checkMail(email) {
	var x = email;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return true;
	else return false;
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function checkReg(myForm) {
	username = myForm.username;
	password = myForm.password;
	password2 = myForm.password2;
	name1 = myForm.name1;
	name2 = myForm.name2;
	phone = myForm.phone;
	email = myForm.email;
	country = myForm.country;

	var sendFlag = true;
	
	if (username.value.Trim() == "" && sendFlag){
		alert("Please enter an username.");
		sendFlag = false;
		username.focus();
	}
	
	if (password.value.length < 6 && sendFlag){
		alert("Please enter at least 6 characters as Password.");
		sendFlag = false;
		password.focus();
	} else if (password.value != password2.value)
	{
		alert("Confirm Password not consistent. Please re-enter.");
		sendFlag = false;
		password.focus();
	}
	
	if (email.value.Trim() == "" && sendFlag){
		alert("please enter your email");
		sendFlag = false;
		email.focus();
	} else if (!checkMail(email.value) && sendFlag) {
		alert("Invalid email format, please re-enter.");
		sendFlag = false;
		email.focus();
	}	

	if (country.selectedIndex < 0 && sendFlag){
		alert("Please select your country.");
		sendFlag = false;
		country.focus();
	}
	
	if (sendFlag) {
		myForm.method = "POST";
		myForm.action += "?goReg";
		myForm.submit();
	}
}

function checkEdit(myForm) {
	username = myForm.username;
	password = myForm.password;
	password2 = myForm.password2;
	name1 = myForm.name1;
	name2 = myForm.name2;
	phone = myForm.phone;
	email = myForm.email;
	country = myForm.country;

	var sendFlag = true;
	
	if (username.value.Trim() == "" && sendFlag){
		alert("Please enter an username.");
		sendFlag = false;
		username.focus();
	}
	
	if (password.value.Trim() != "" && password.value.length < 6 && sendFlag){
		alert("Please enter at least 6 characters as Password.");
		sendFlag = false;
		password.focus();
	} 
	
	if (password.value.Trim() != "" && password.value != password2.value && sendFlag)
	{
		alert("Confirm Password not consistent. Please re-enter.");
		sendFlag = false;
		password.focus();
	}
	
	if (email.value.Trim() == "" && sendFlag){
		alert("please enter your email");
		sendFlag = false;
		email.focus();
	} else if (!checkMail(email.value) && sendFlag) {
		alert("Invalid email format, please re-enter.");
		sendFlag = false;
		email.focus();
	}	

	if (country.selectedIndex < 0 && sendFlag){
		alert("Please select your country.");
		sendFlag = false;
		country.focus();
	}
	
	if (sendFlag) {
		myForm.method = "POST";
		myForm.action += "?goEdit";
		myForm.submit();
	}
}

function checkAlbum(myForm) {	
	title = myForm.title;
	photo = myForm.photo;
	
	var sendFlag = true;
	
	if (title.value.Trim() == "" && sendFlag){
		alert("Please enter a title.");
		sendFlag = false;
		title.focus();
	}
	if (photo.value.Trim() == "" && sendFlag){
		alert("Please choose a photo to upload.");
		sendFlag = false;
		photo.focus();
	}
	
	if (sendFlag) {
		myForm.method = "POST";
		myForm.action += "?goUpload";
		myForm.submit();
	}	
}

function checkTrade(myForm) {
	cat = myForm.cat;
	title = myForm.title;
	size = myForm.size;
	brand = myForm.brand;
	description = myForm.description;
	
	var sendFlag = true;
	
	if (cat.selectedIndex <= 0 && sendFlag){
		alert("Please select a category.");
		sendFlag = false;
		cat.focus();
	}
	
	if (title.value.Trim() == "" && sendFlag){
		alert("Please enter a title.");
		sendFlag = false;
		title.focus();
	}
	
	if (size.selectedIndex <= 0 && sendFlag){
		alert("Please select the size.");
		sendFlag = false;
		size.focus();
	}
	
	if (brand.selectedIndex <= 0 && sendFlag){
		alert("Please select the brand.");
		sendFlag = false;
		brand.focus();
	}
	
	if (sendFlag) {
		myForm.method = "POST";
		myForm.action = "sell_post.php?goEdit";
		myForm.submit();
	}
}

function checkTrade2(myForm) {
	cat = myForm.cat;
	title = myForm.title;
	size = myForm.size;
	brand = myForm.brand;
	description = myForm.description;
	
	var sendFlag = true;
	
	if (cat.selectedIndex <= 0 && sendFlag){
		alert("Please select a category.");
		sendFlag = false;
		cat.focus();
	}
	
	if (title.value.Trim() == "" && sendFlag){
		alert("Please enter a title.");
		sendFlag = false;
		title.focus();
	}
	
	if (size.selectedIndex <= 0 && sendFlag){
		alert("Please select the size.");
		sendFlag = false;
		size.focus();
	}
	
	if (brand.selectedIndex <= 0 && sendFlag){
		alert("Please select the brand.");
		sendFlag = false;
		brand.focus();
	}
	
	if (description.value.Trim() == "" && sendFlag){
		alert("Please enter some description.");
		sendFlag = false;
		description.focus();
	}
	
	if (sendFlag) {
		myForm.method = "POST";
		myForm.action += "&goPost";
		myForm.submit();
	}
}

function checkReply(myForm) {
	content = myForm.content;	
	var sendFlag = true;
	if (sendFlag) {
		myForm.method = "POST";
		myForm.action += "&goReply";
		myForm.submit();
	}
}

function checkPM(myForm) {
	var title = myForm.title;
	var content = myForm.content;	
	var sendFlag = true;
	
	if (title.value.Trim() == "" && sendFlag){
		alert("Please enter a subject.");
		sendFlag = false;
		title.focus();
	}

	if (sendFlag) {
		myForm.method = "POST";
		myForm.action += "?goPM";
		myForm.submit();
	}
}

function checkLogin(myForm) {
	login = myForm.login;	
	password = myForm.password;
	
	var sendFlag = true;
	if ((login.value.Trim() == "" || login.value.Trim() == "Login Name")&& sendFlag){
		alert("Please enter login name.");
		sendFlag = false;
		login.focus();
		return false;
	}

	if (password.value.Trim() == "" && sendFlag){
		alert("Please enter password.");
		sendFlag = false;
		password.focus();
		return false;
	}
	
	
	if (sendFlag) {
		myForm.method = "POST";
		myForm.action = "member_login.php?goLogin";
		myForm.submit();
	}
}


function checkPost(myForm) {
	title = myForm.title;
	content = myForm.content;
	
	var sendFlag = true;

	if (title.value.Trim() == "" && sendFlag){
		alert("Please enter a subject.");
		sendFlag = false;
		title.focus();
	}

	if (sendFlag) {
		myForm.method = "POST";
		myForm.action += "?goPost";
		myForm.submit();
	}
}

function checkContact(myForm)
{
	name = myForm.name;
	company = myForm.company;
	email = myForm.email;
	tel = myForm.tel;
	message = myForm.message;

	var sendFlag = true;

	if (name.value == "" && sendFlag){
		alert("Please enter your name.");
		sendFlag = false;
		name.focus();
	}
	
	if (company.value == "" && sendFlag){
		alert("please enter your company");
		sendFlag = false;
		company.focus();
	}
	
	if (email.value == "" && sendFlag){
		alert("please enter your email");
		sendFlag = false;
		email.focus();
	} else if (!checkMail(email.value) && sendFlag) {
		alert("Invalid email format, please re-enter.");
		sendFlag = false;
		email.focus();
	}
	
	if (tel.value == "" && sendFlag){
		alert("please enter your phone no.");
		sendFlag = false;
		tel.focus();
	}

	if (message.value == "" && sendFlag){
		alert("Please enter your message.");
		sendFlag = false;
		message.focus();
	}
	
	if (sendFlag) {
		myForm.method = "POST";
		myForm.action = "successmail.php";
		myForm.submit();
	}
}

function checkFg(myForm)
{
	email = myForm.email;
	
	var sendFlag = true;
		
	if (email.value == "" && sendFlag){
		alert("please enter your email");
		email.focus();
		return;
	} else if (!checkMail(email.value) && sendFlag) {
		alert("Invalid email format, please re-enter.");
		email.focus();
		return;
	}

	myForm.method = 'POST';
	myForm.submit();
}


