﻿// JavaScript Document

//select 左右跑
function doMoveOption(f_id, t_id, type) {
	var f = document.getElementById(f_id);
	var to = document.getElementById(t_id);
	
	
	var o;
	
	if (type == 1) {	//all
		
		for (var i=0; i<f.options.length; i++) {
			o = document.createElement("option");
			o.text = f.options[i].text;
			o.value = f.options[i].value;

			try {
				to.add(o, null); // standards compliant
			} catch(ex)	{
				to.add(o); // IE only
			}
			
		}
		f.options.length = 0;
		
	} else if (type == 2) {
		
		for (var i=0; i<f.options.length; i++) {
			
			if (f.options[i].selected) {
				o = document.createElement("option");
				o.text = f.options[i].text;
				o.value = f.options[i].value;
				
				try {
					to.add(o, null); // standards compliant
				} catch(ex)	{
					to.add(o); // IE only
				}
				
			
				f.remove(f.options[i].index);
				i--;
				
			} 
			
		}//for
		
	}
		
}


/*
   檢查 Radio 是否有 checked
*/
function checkInputRadio(radio) {
	var selected = false;
    for (var i=0 ; i < radio.length ; i++) {
         if (radio[i].checked) {
             selected = true
         }
    }

	return selected;
}

/*
	檢查上傳的是否為圖片
*/
function checkPIC(pic){
	var img = pic.substr(pic.lastIndexOf(".")+1);
	var state = false;
	img = img.toLowerCase();
	
	if (img == "png" || img == "jpg" || img == "gif" || img == "jpeg"){
		state = true;
	}else{
		
		state = false;
	}
	return state;
}




//去左右空白 
function trim(strvalue) { 
	/*return rtrim(ltrim(strvalue)); */
	var ptntrim = /(^\s*)|(\s*$)/g; 
	return strvalue.replace(ptntrim,""); 
} 
//去左空白 
function ltrim(strvalue) { 
 var ptnltrim = /^\s*/g; 
 return strvalue.replace(ptnltrim,""); 
} 
//去右空白 
function rtrim(strvalue) { 
 var ptnrtrim = /\s$/g; 
 return strvalue.replace(ptnrtrim,""); 
}

/*
   檢查 E-mail 格式
*/
function checkEmailReg(value) {
     var reg_email = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
	 /*
	 /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/;
	 /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	 */
	 
	 var state = false;
	 
	 if (value != "") {
		 if (reg_email.test(value)){
		     state = true;
		 } else {
			 state = false;    
		 }
	 } else {
		 state = false;
	 }
	 
	 return state;
}

