function countRemaining(textForm, maxLength, divid) {
	var pid = divid;
	//alert(pid);
	var remainCharacter = maxLength - textForm.value.length;

	if(textForm.value.length<=maxLength) $(pid).innerHTML = remainCharacter;
	else {
		alert('enable input '+maxLength+' Word');
		textForm.value = textForm.value.substr(0,maxLength);
	}
}

function chkInputForm() {
	var rform = document.applicationForm;

	// chk Title of Presentation
	if(chkLength(rform.presentationTitle, 'Title of Presentation')==0) { rform.presentationTitle.focus(); return false; }
	if(chkLength(rform.presenterName, 'Name of Presenter1')==0) { rform.presenterName.focus(); return false; }
	if(chkLength(rform.affiliation, 'Affiliation')==0) { rform.affiliation.focus(); return false; }
	if(chkRadioButton(rform.targetArea, 'Target Area')==0) { rform.targetArea[0].focus(); return false; }
	if(chkRadioButton(rform.position, 'Position')==0) { rform.position[0].focus(); return false; }
	if(chkRadioButton(rform.title, 'Title')==0) { rform.title[0].focus(); return false; }
	if(chkEmailForm(rform.mailingPresenter1, 'Presenter1')==0) { rform.mailingPresenter1.focus(); return false; }
	/*
	if(chkLength(rform.mailingPresenter1, 'Mailing List Presenter1')==0) { 
		rform.mailingPresenter1.focus(); 
		return false; 
	} else {
		if(rform.mailingPresenter1.value.indexOf(' ') != -1) { // eMail 공백포함여부 검사
			alert("Checkd Email (Presenter1)");
			rform.mailingPresenter1.focus(); 
			return 0;
		}
		if(rform.mailingPresenter1.value.search(/^\w.*\@\w+.*\.\w{2,3}$/) == -1) { // eMail  유효성 검사
			alert("Checkd Email (Presenter1)");
			rform.mailingPresenter1.focus(); 
			return 0;
		}
	}
	*/
		
	// Presenter3 Email Validation
	 if (chkLengthNotAlert(rform.presenterName6, 'Name of Presenter6')>0) {
		// Presenter6에 대한 사항이 입력되있는지 확인
		if(chkLength(rform.affiliationPresenter6, 'Affiliation Presenter6')==0) { rform.affiliationPresenter6.focus(); return false; }
		if(chkEmailForm(rform.mailingPresenter6, 'Presenter6')==0) { rform.mailingPresenter6.focus(); return false; }
	}
	 if (chkLengthNotAlert(rform.presenterName5, 'Name of Presenter5')>0) {
		// Presenter5에 대한 사항이 입력되있는지 확인
		if(chkLength(rform.affiliationPresenter5, 'Affiliation Presenter5')==0) { rform.affiliationPresenter5.focus(); return false; }
		if(chkEmailForm(rform.mailingPresenter5, 'Presenter5')==0) { rform.mailingPresenter5.focus(); return false; }
	}
	 if (chkLengthNotAlert(rform.presenterName4, 'Name of Presenter4')>0) {
		// Presenter4에 대한 사항이 입력되있는지 확인
		if(chkLength(rform.affiliationPresenter4, 'Affiliation Presenter4')==0) { rform.affiliationPresenter4.focus(); return false; }
		if(chkEmailForm(rform.mailingPresenter4, 'Presenter4')==0) { rform.mailingPresenter4.focus(); return false; }
	}

	if(chkLengthNotAlert(rform.presenterName3, 'Name of Presenter3')>0) {
		// Presenter2에 대한 사항이 입력되있는지 확인
		if(chkLength(rform.presenterName2, 'Name of Presentar2')==0) { rform.presenterName2.focus();  return false; }
		if(chkLength(rform.affiliationPresenter2, 'Affiliation Presenter2')==0) { rform.affiliationPresenter2.focus(); return false; }
		if(chkEmailForm(rform.mailingPresenter2, 'Presenter2')==0) { rform.mailingPresenter2.focus(); return false; }
		// Presenter3에 대한 사항이 입력되있는지 확인
		if(chkLength(rform.affiliationPresenter3, 'Affiliation Presenter3')==0) { rform.affiliationPresenter3.focus(); return false; }
		if(chkEmailForm(rform.mailingPresenter3, 'Presenter3')==0) { rform.mailingPresenter3.focus(); return false; }
	} else if (chkLengthNotAlert(rform.presenterName2, 'Name of Presenter2')>0) {
		// Presenter2에 대한 사항이 입력되있는지 확인
		if(chkLength(rform.affiliationPresenter2, 'Affiliation Presenter2')==0) { rform.affiliationPresenter2.focus(); return false; }
		if(chkEmailForm(rform.mailingPresenter2, 'Presenter2')==0) { rform.mailingPresenter2.focus(); return false; }
	}

	if(rform.country.options[rform.country.selectedIndex].value.length==0) {alert('Select country.'); rform.country.focus(); return false;}
	if(chkRadioButton(rform.address_kind, 'Address_kind')==0) { rform.address_kind[0].focus(); return false; }
	if(chkLength(rform.address, 'Address')==0) { rform.address.focus(); return false; }
	if(chkRadioButton(rform.phone_kind, 'phone_kind')==0) { rform.phone_kind[0].focus(); return false; }
	if(chkLength(rform.phoneWork, 'Phone Work')==0) { rform.phoneWork.focus(); return false; }
	/*
	if(chkLength(rform.phoneHome, 'Phone Home')==0) { rform.phoneHome.focus(); return false; }
	*/
	if(chkLength(rform.phoneCell, 'Cell Phone')==0) { rform.phoneCell.focus(); return false; }
	if(chkRadioButton(rform.topicArea, 'Topic Area')==0) { rform.topicArea[0].focus(); return false; }
	if(chkRadioButton(rform.typeOfPresentation, 'Type of Presentation')==0) { rform.typeOfPresentation[0].focus(); return false; }
	if(chkRadioButton(rform.equimentRequired, 'Equiment Required')==0) { rform.equimentRequired[0].focus(); return false; }
	if(chkLength(rform.abstractPresentation, 'Abstract of your presentation')==0) { rform.abstractPresentation.focus(); return false; }

	if(countBlank(rform.abstractPresentation)>260) { alert('A 250-word Abstract of your presentation'); rform.abstractPresentation.focus(); return false;}

	if(chkLength(rform.biographicalData, 'Biographical data')==0) { rform.biographicalData.focus(); return false; }
	if(countBlank(rform.biographicalData)>61) { alert('An 60-word Biographical data'); rform.biographicalData.focus(); return false;}

	rform.titleStr.value = makeCheckStr(rform.title);
	rform.equimentRequiredStr.value = makeCheckStr(rform.equimentRequired);
	
	if(rform.submitType.value=='modify') {
		rform.action="./applicationRegist_Modify_newForm.php";
	} else {
		rform.action="./applicationRegist.php";
	}
	
	//alert('Run submit()');
	rform.submit();

}

function makeCheckStr(inputForm) {
	var titleLength = inputForm.length;
	var chkStr="";
	var chkStart=0;

	for(i=0; i<titleLength; i++) {
		if(inputForm[i].checked==true) {
			if(chkStart>0) chkStr += "|";
			chkStr += inputForm[i].value;
			chkStart++;
			//alert(inputForm[i].value);
		}
	}

	return chkStr
}
String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/gi,"");
}
function chkLength(inputForm, fieldName) {
	inputForm.value = inputForm.value.trim();
	if(inputForm.value.length==0) {
		if(fieldName == "Mailing List Presenter1") {
			alert("Input email address of presenter1");
		} else if(fieldName == "Mailing List Presenter2") {
			alert("Input email address of presenter2");
		} else if(fieldName == "Mailing List Presenter3") {
			alert("Input email address of presenter3");
		} else if(fieldName == "Mailing List Presenter4") {
			alert("Input email address of presenter4");
		} else if(fieldName == "Mailing List Presenter5") {
			alert("Input email address of presenter5");
		} else if(fieldName == "Mailing List Presenter6") {
			alert("Input email address of presenter6");
		} else if(fieldName == "Phone Work") {
			alert("Input phone number.");
		} else if(fieldName == "Cell Phone") {
			alert("Input cell phone number.");
		} else {
			alert('Input '+fieldName+".");
		}
		return 0;
	} else {
		return 1;
	}
}

function chkLengthNotAlert(inputForm, fieldName) {
	inputForm.value = inputForm.value.trim();
	if(inputForm.value.length==0) {
		//alert('Input '+fieldName);
		return 0;
	} else {
		return 1;
	}
}
function chkEmailForm(emailForm, fieldName) {
	if(chkLength(emailForm, 'Mailing List '+fieldName)==0) { 
		emailForm.focus(); 
		return false; 
	} else {
		if(emailForm.value.indexOf(' ') != -1) { // eMail 공백포함여부 검사
			alert("Checkd Email ("+fieldName+")");
			emailForm.focus(); 
			return 0;
		}
		if(emailForm.value.search(/^\w.*\@\w+.*\.\w{2,3}$/) == -1) { // eMail  유효성 검사
			alert("Checkd Email ("+fieldName+")");
			emailForm.focus(); 
			return 0;
		}
	}
}
function chkRadioButton(inputForm, fieldName) {
	var formLength = inputForm.length;
	for(i=0; i<formLength; i++) {
		if(inputForm[i].checked==true) {
			var position_val = $("input:radio[name=position]:checked").val();
			if(position_val == '6' && $("#position_other").val().length==0) {
				alert('Input Other');
				$("#position_other").focus();
				return 0;
			}
			return 1;
		}
	}
	alert('Check '+fieldName+'.');
	return 0;
}

function countBlank(inputForm) {
	// blank를 체크 하기는 한데 여러개의 blank가 연속으로 있는것 까지 체크를 하니 문제다.
	var str=inputForm.value;

	str=str.replace(/,/g, " ");
	str=str.replace(/\./g, " ");
	str=str.replace(/\!/g, " ");
	str=str.replace(/\?/g, " ");
	str=str.replace(/\n/g, " ");
	str=str.replace(/\r/g, " ");
	str=str.replace(/\'/g, " ");
	str=str.replace(/\"/g, " ");
	str=str.replace(/\:/g, " ");
	str=str.replace(/\;/g, " ");
	str=str.replace(/-/g, " ");
	str=str.replace(/_/g, " ");
	str=str.replace(/\[/g, " ");
	str=str.replace(/\]/g, " ");
	str=str.replace(/\(/g, " ");
	str=str.replace(/\)/g, " ");
	str=str.replace(/\</g, " ");
	str=str.replace(/\>/g, " ");
	str=str.replace(/\~/g, " ");
	str=str.replace(/\\/g, " ");
	str=str.replace(/\//g, " ");

	str=str.replace(/     /g, " ");
	str=str.replace(/    /g, " ");
	str=str.replace(/   /g, " ");
	str=str.replace(/  /g, " ");
	str=str.replace(/  /g, " ");
	str=str.replace(/  /g, " ");
	str=str.replace(/  /g, " ");
	str=str.replace(/  /g, " ");	// 공란 두개를 한개로 변환

	//var formLength = inputForm.value.length;
	//var arrStr = inputForm.value;
	var formLength = str.length;
	var arrStr = str;
	var blankCount=0;

	for(i=0; i<formLength; i++) {
		if(arrStr[i]==' ') blankCount++;
	}
	//alert(blankCount);
	return blankCount;
}
