function select_shop(b_id, s_id){
	var e = parent.document.getElementById("show_shop");
	
	e.src = "show_shop.php?board_id=" + b_id + "&shop_id=" + s_id;
}

function sqlInjection(str){
	if(str.search(/\$|\'|\"|\?|\%|\=|select|insert|update|delete/i) != -1)
		return true;
	else
		return false;
}

//text, select-one, radio, textarea, file
function check_form(form, startName, bouncedArray){
	var startIndex = getStartIndex(form, startName);
	var arrBounce = new Array(bouncedArray.length);

	for(var i = 0; i < arrBounce.length; i++){
		arrBounce[i] = bouncedArray[i] + startIndex;
	}
	
	
	for(var i = startIndex; i < form.elements.length; i++){
		var e = form.elements[i], bounce = false;
		
		if(sqlInjection(e.value) && (e.type == "text" || e.type == "textarea")){
			alert("Please do not enter special characters\n請勿使用任何特殊符號或關鍵字，如$");
			e.focus();

			return false;
		}

		for(var j = 0; j < arrBounce.length; j++){
			if(i == arrBounce[j])
				bounce = true;
		}

		if(!bounce){
			if(e.type == "text" || e.type == "textarea" || e.type == "password"){
				if(!check_text(e)){
					alert("Please fill in this field\n請填入所有必填欄位");
					e.focus();
				
					return false;
				}
		
				if(e.name == "email"){
					if(e.value.search(/[\w\-]+(\@)+[\w\-]+\.[\w\-]+/) == -1){
						alert("Invalide email format\n電子郵件地址格式錯誤");
						e.focus();
						
						return false;
					}
				}
			}else if(e.type == "select-one"){
				if(e.selectedIndex == 0){
					alert("Please select this field\n請選擇必填欄位");
					e.focus();
					
					return false;
				}
			}else if(e.type == "radio"){
				var radioChecked = false, arrRadio = eval("form." + e.name);
				
				for(var j = 0; j < arrRadio.length; j++){
					if(arrRadio[j].checked)
						radioChecked = true;
				}
				
				if(!radioChecked){
					alert("Please check this field\n請選擇所有必填欄位");
					e.focus();
					  
					return false;
				}
			}else if(e.type == "file"){
				if(!check_text(e)){
					alert("Please select a picture\n請選擇一張照片");
					e.focus();
					
					return false;
				}
			}
		}
		
		//If it's the field to confirm password then check if user input the same password
		if(e.name == "confirm_password"){
			if(!confirm_password()){
				alert("Please input the same password in \'Password\' and \'Confirm Password\'" +
					  "\n請在\'密碼\'和\'確認密碼\'欄位輸入相同的密碼");
				return false;
			}
		}
	}
	
	return true;
}

function getStartIndex(form, objName){
	var startIndex = 0;
	
	for(var i = 0; i < form.elements.length; i++){
		var e = form.elements[i];
		
		if(e.name == objName){
			startIndex = i;
			break;
		}
	}
	
	return startIndex;
}

function check_text(e){
	if(e.value == null || e.value.length == 0)
		return false;
	else
		return true;
}

function change_iframe(frame_name, frame_url){
	var e = document.getElementById(frame_name);
	e.src = frame_url;
}

function set_file_name(){
	var from = document.getElementById("picture");
	var to = document.getElementById("file_name");
	
	var ary = from.value.split("\\");
	to.value = ary[ary.length - 1];
}

function change_lang(e, new_str){
	e.innerHTML = new_str;
}

function confirm_password(){
	var password_1 = document.getElementById("password");
	var password_2 = document.getElementById("confirm_password");
	
	if(password_1.value != password_2.value)
		return false;
	else
		return true;
}