var valid2 = " &#12395;&#28961;&#21177;&#12394;&#25991;&#23383;&#12364;&#21547;&#12414;&#12428;&#12390;&#12356;&#12414;&#12377;&#12290;";
var mandatory =" &#12434;&#12372;&#20837;&#21147;&#12367;&#12384;&#12373;&#12356;&#12290;";

function isFieldValid(fieldStr,fieldName,fieldMinLength,fieldMaxLength,fieldType,customReg) {
	var result = "";
	var error = 0;

	if (isEmpty(fieldMaxLength)) {
		if (fieldType =="text") {
			fieldMaxLength = 50;
		} else if (fieldType == "num") {
			fieldMaxLength = 12;
		} else if (fieldType == "email") {
			fieldMaxLength = 80;
		} else {
			fieldMaxLength = 500;
		}
	}

   if (fieldStr.length > 0) {
		//remove trailing space for $str value
		var replaceReg = /[ ]*$/;
		fieldStr = fieldStr.replace(replaceReg,"");		
		// Check whether string match the regular expression
		
		if (fieldType == "num") {
			var regPattern = /(^[\d$-]([\d\s-,\.]*)$)/;
			if (regPattern.test(fieldStr) == false) {
				result = "<strong>" + fieldName + " </strong>" + valid2 + "<br /><br />";
			}
		} else if (fieldType =="email") {
			var regPattern = /(\w{1}[\w-_\.]*)\@(\w{1})([\w-_\.]+\.\w+)/;
			if (regPattern.test(fieldStr) == false) {
				result = "<strong>" + fieldName + " </strong> &#12450;&#12489;&#12524;&#12473;&#12364;&#27491;&#12375;&#12367;&#12354;&#12426;&#12414;&#12379;&#12435;&#12290;<br /><br />";
			}
		}
			
		if ((fieldStr.length > fieldMaxLength) || (fieldStr.length < fieldMinLength)) {
			if (fieldMaxLength == fieldMinLength) {
				result = result + "<strong>" + fieldName + "</strong>" + " &#12399; " + fieldMinLength + " &#25991;&#23383;&#12391;&#12372;&#20837;&#21147;&#12367;&#12384;&#12373;&#12356;&#12290<br /><br />";
			} else {
				result = result + "<strong>" + fieldName + "</strong>" + " &#12399; " + fieldMinLength + " - " + fieldMaxLength + " &#25991;&#23383;&#12391;&#12372;&#20837;&#21147;&#12367;&#12384;&#12373;&#12356;&#12290<br /><br />";
			}
			error = 1;
		}
	} else {
		//if the field is mandatory, display error message
		if (fieldMinLength != 0) {
			if (fieldType == "optionsList") {
				result = "<strong>" + fieldName + "</strong>" + valid2 + "<br /><br />";
			} else {
					result = "<strong>" + fieldName + "</strong>" + mandatory + "<br /><br />";
			}
			error = 1;
		} 
	}
	return result;
 }

function getSelectedOptionValue(selectField) {
	var selectedValue = "";
	selectedValue = selectField.options[selectField.selectedIndex].value;
	return selectedValue;

}

function getRadioButtonValue(radio) {
	var cValue = "";
	for (var i = 0; i < radio.length; i++) {
		if (radio[i].checked) { cValue = radio[i].value; break }
	}

	return cValue
}

function getCheckBoxValue(box) {
	var cBoxValue = "";
	for (var i = 0; i < box.length; i++) {   
		if (box[i].checked) { cBoxValue = box[i].value; break }
    }

	return cBoxValue
}

// Check whether string s is empty.
function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or whitespace characters only.
function isWhitespace (s) {
	var i;

	// Is s empty?
	if (isEmpty(s)) return true;

	// Search through string's characters one by one until we find a
	// non-whitespace character. When we do, return false; if we don't, return true.
    for (i = 0; i < s.length; i++) {
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	}

    // All characters are whitespace.
    return true;
}
