﻿/***************CheckInput Start******************/ 
function checkNull(o,s) {
		if(o.value == "") {
			if (o.selectedIndex != undefined)
			{
				alert("Please select“"+s+"”");
				o.focus();
				return true;
			}
			else
			{
				alert("Please input“"+s+"”");
				o.focus();
				return true;
			}
		}else{
			return false;
		}
	}
function checkNullOther(o,s) {
		if(o.value == "") {
			if (o.selectedIndex != undefined)
			{
				alert("Please select“"+s);
				o.focus();
				return true;
			}
			else
			{
				alert("Please input“"+s);
				o.focus();
				return true;
			}
		}else{
			return false;
		}
	}

function checkSelected(o,num,s) {
	//alert(num);
	var i = 0;
	for(var j = 0; j < o.length; j++)
		if(o[j].checked)
			i++;
	if(i <= num && i > 0) {
		return false;
	}else{
		var str = "";
		str = "Please select“" + s + "”";
		if(num > 1 && num != o.length)
			str = str + "（max " + num + "）";
		alert(str);
		o[0].focus();
		return true;
	}
}

function getValueRadio(o) {
for(i=0;i<o.length;i++)
	{
		if(o[i].checked)
			return o[i].value;
	}

}

function checkInput(o,s,oOther,maxLength){
	if(o.value != undefined){
		if(checkNull(o,s)) return true;
		if(oOther != null && oOther.value != undefined){
			if(o.value.indexOf("其他") >= 0)
				if(checkNullOther(oOther,s+"”的“其他”选项说明")) return true;
		}
	}else if(o.length > 0){
		if(o[0].type == "radio"){
			if(checkSelected(o,1,s)) return true;
		}else if(o[0].type == "checkbox"){
			if(isNaN(maxLength)){
				if(checkSelected(o,o.length,s)) return true;
			}else{
				if(checkSelected(o,maxLength,s)) return true;
			}
		}
		if(oOther != null && oOther.value != undefined){
			if(getValueRadio(o).indexOf("其他") >= 0 || getValueRadio(o).indexOf("说明") >= 0)
				if(checkNullOther(oOther,s+"”的“其他”选项说明")) return true;
		}
	}else{
		alert("Parameter error:input tag not found");
		return true;
	}
	return false;
}

/***************CheckInput End******************/

/***************Initial Form******************/ 
//For Radio 
function selectValue(o,v){
	for(var i = 0; i < o.length; ++i){
		if(o[i].value == v){
			o[i].checked = true;
		}else{
			o[i].checked = false;
		}
	}
}
//For Checkbox
function selectValues(o,v){
	for(var i = 0; i < o.length; ++i){
		if(v.indexOf("," + o[i].value + ",")>=0){
			o[i].checked = true;
		}else{
			o[i].checked = false;
		}
	}
}
//For Text Box and Select
function setValue(o,value){
	o.value = value;
}

/***************Initial End******************/

