// JavaScript Document
 /********************************************************
 Version		: ShimBi News Feeds (Ver. 1.0.0)
 Author  		: Kailash Kumawat
 Start Date 	: 03 - 07 -2006
 Last Modified 	: 03 - 07- 2006
 File Name 		: form_validation.js
 Purpose 		: form validation functions
 
 ********************************************************/
 
//check for blank
function isBlank(t_obj,t_msg)
{
	if (trimString(t_obj.value)=="")
	{
		alert(t_msg);
		t_obj.focus();	
		return 0;
	}
	else
		return 1;
}

//remove spaces from start and end
function trimString (str) 
{
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}


//check email
function isEmail(t_obj,t_msg)
{
	
	var xx=t_obj.value;

	validchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.@_-";
	var valid="y";
	for (i=0; i<xx.length; i++) 
	{
		badchar = xx.charAt(i);
		if (validchars.indexOf(badchar,0) == -1)
		{
			valid="n";	
			break;
		}
	}
	if ((xx.indexOf("@") == -1) || (xx.indexOf(".") == -1))
	{
		valid="n";
	}
	at_loc=xx.indexOf("@");
	dot_loc=xx.indexOf(".");
	
	if(xx.indexOf("@",at_loc+1)>0)
	{
		valid="n";
	}
	if( xx.substr(at_loc+1,1) == "." || xx.substr(at_loc-1,1) == "." )
	{
		valid="n";
	}
	if (parseInt(xx.indexOf(".",parseInt(xx.indexOf("@"))))==-1)
	{
		valid="n";
	}
	
	if(countChar(xx,'@',0,xx.length-1)!=1)
	{
		valid="n";
	}
	if( (xx.length - xx.lastIndexOf("."))<3)
	{
		valid="n";
	}
	if(xx.indexOf("@")<2)
	{
		valid="n";
	}
	if(xx.indexOf("-")==0 ||xx.indexOf("_")==0)
	{
		valid="n";
	}
	if(xx.lastIndexOf("-")== xx.length-1 ||xx.lastIndexOf("_")== xx.length-1)
	{
		valid="n";
	}
	
	if(valid=="n")
	{
		alert(t_msg);
		//t_obj.focus();	
		return 0;
	}
	return 1;
}
function countChar(t_str,t_char,start_index,end_index)
{
	var count=0;
	for(i=parseInt(start_index);i<parseInt(end_index);i++)
	{
		if(t_str.indexOf(t_char,i)!=-1)
		{
			count++;
			i=t_str.indexOf(t_char,i) ;
		}
	}
	return count;
}

function validate_tob(me)
{
	
	if(isBlank(me.text_2,"貴社名が入力されていません")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_3,"貴サイトURLが入力されていません")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_5,"ご担当者名が入力されていません")==0)
	{
		return false;
	}
	
	
	if(isBlank(me.text_6,"Eメールアドレスが入力されていません")==0)
	{
		return false;
	}
	
	if(isEmail(me.text_6,"有効なメールアドレスを入力ください")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_7,"貴社ご住所が入力されていません")==0)
	{
		return false;
	}
	
	/*if(isBlank(me.text_8,"Address should not be blank.")==0)
	{
		return false;
	}*/
	
	if(isBlank(me.text_9,"TELが入力されていません")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_10,"FAXが入力されていません")==0)
	{
		return false;
	}
	
	return true;
}

function validate_ask(me)
{
	
	if(isBlank(me.text_2,"お問い合わせ内容が入力されていません")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_3,"お名前が入力されていません")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_6,"E-mailが入力されていません")==0)
	{
		return false;
	}
	
	if(isEmail(me.text_6,"有効なメールアドレスを入力ください")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_7,"お電話番号が入力されていません")==0)
	{
		return false;
	}
	
	return true;
}


function validate_week(me)
{
	
	if(isBlank(me.text_1,"メールアドレスが入力されていません")==0)
	{
		return false;
	}
	
	if(isEmail(me.text_1,"有効なメールアドレスを入力ください")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_2,"氏名（漢字）が入力されていません")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_3,"氏名（漢字）が入力されていません")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_4,"氏名（フリガナ）が入力されていません")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_5,"氏名（フリガナ）が入力されていません")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_6,"生年月日が入力されていません")==0)
	{
		return false;
	}
	
	name="select_7"
	check="n";
	for (i = 0; i < document.frmcomment_shimbi.length; i++) 
	{
		// regex here to check name attribute
		var regex = new RegExp(name, "i");
		if (regex.test(document.frmcomment_shimbi[i].getAttribute('name'))) 
		{
			if (document.frmcomment_shimbi[i].checked == true) 
			{
				gender_val=document.frmcomment_shimbi[i].value;
				check="y";
				break;
			} 
		}
	}
	if(check=="n")
	{
		alert("性別を選択してください");
		return false;
	}
	
	name="select_8"
	check="n";
	for (i = 0; i < document.frmcomment_shimbi.length; i++) 
	{
		// regex here to check name attribute
		var regex = new RegExp(name, "i");
		if (regex.test(document.frmcomment_shimbi[i].getAttribute('name'))) 
		{
			if (document.frmcomment_shimbi[i].checked == true) 
			{
				gender_val=document.frmcomment_shimbi[i].value;
				check="y";
				break;
			} 
		}
	}
	if(check=="n")
	{
		alert("メールマガジン購読希望を選択ください");
		return false;
	}
	
	

	if(isBlank(me.text_9,"郵便番号が入力されていません")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_10,"ご住所が入力されていません")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_11,"お電話番号が入力されていません")==0)
	{
		return false;
	}
	return true;
}

function validate_comment(me)
{
	var formblock;
	var forminputs;
	
	val=0;
	name="select_1"
	check="n";
	for (i = 0; i < document.frmcomment_shimbi.length; i++) 
	{
		// regex here to check name attribute
		var regex = new RegExp(name, "i");
		if (document.frmcomment_shimbi[i].getAttribute('name')==name) 
		{
			if (document.frmcomment_shimbi[i].checked == true) 
			{
				check="y";
				val=document.frmcomment_shimbi[i].value;
				cat_val=document.frmcomment_shimbi[i].alt;
				break;
			} 
		}
	}
	
	if(check=="n")
	{
		alert("オプションを選択してください");
		return false;
	}
	if(val=="その他" && me.text_2.value=="")
	{
		alert("「その他」はフィールドに記入ください");
		return false;
	}
	if(me.select_3.value=="")
	{
		alert("評価項目を選択してください");
		return false;
	}
	
	name="select_4"
	check="n";
	for (i = 0; i < document.frmcomment_shimbi.length; i++) 
	{
		// regex here to check name attribute
		var regex = new RegExp(name, "i");
		if (regex.test(document.frmcomment_shimbi[i].getAttribute('name'))) 
		{
			if (document.frmcomment_shimbi[i].checked == true) 
			{
				score_val=document.frmcomment_shimbi[i].value;
				check="y";
				break;
			} 
		}
	}
	if(check=="n")
	{
		alert("評価点数を選択してください");
		return false;
	}
	
	if(me.text_6.value=="")
	{
		alert("メールアドレスが入力されていません");
		me.text_6.focus();
		return false;
	}
	
	if(isEmail(me.text_6,"有効なメールアドレスを入力ください")==0)
	{
		return false;
	}
	
	return true;
}

//function to validate fx_trade
function validate_fx_trade(me)
{
	var formblock;
	var forminputs;
	
	val=0;
	name="select_1"
	check="n";
	for (i = 0; i < document.frmcomment_shimbi.length; i++) 
	{
		// regex here to check name attribute
		var regex = new RegExp(name, "i");
		if (document.frmcomment_shimbi[i].getAttribute('name')==name) 
		{
			if (document.frmcomment_shimbi[i].checked == true) 
			{
				check="y";
				val=document.frmcomment_shimbi[i].value;
				cat_val=document.frmcomment_shimbi[i].alt;
				break;
			} 
		}
	}
	
	if(check=="n")
	{
		alert("Please select 年齢");
		return false;
	}
	
	name="select_2"
	check="n";
	for (i = 0; i < document.frmcomment_shimbi.length; i++) 
	{
		// regex here to check name attribute
		var regex = new RegExp(name, "i");
		if (document.frmcomment_shimbi[i].getAttribute('name')==name) 
		{
			if (document.frmcomment_shimbi[i].checked == true) 
			{
				check="y";
				val=document.frmcomment_shimbi[i].value;
				cat_val=document.frmcomment_shimbi[i].alt;
				break;
			} 
		}
	}
	
	if(check=="n")
	{
		alert("Please select 性別");
		return false;
	}
	
	name="select_3"
	check="n";
	for (i = 0; i < document.frmcomment_shimbi.length; i++) 
	{
		// regex here to check name attribute
		var regex = new RegExp(name, "i");
		if (document.frmcomment_shimbi[i].getAttribute('name')==name) 
		{
			if (document.frmcomment_shimbi[i].checked == true) 
			{
				check="y";
				val=document.frmcomment_shimbi[i].value;
				cat_val=document.frmcomment_shimbi[i].alt;
				break;
			} 
		}
	}
	
	if(check=="n")
	{
		alert("Please select 評価");
		return false;
	}
}

//function for other comments pages
function validate_other_comment(me)
{
	var formblock;
	var forminputs;
	
	val=0;
	name="select_1"
	check="n";
	for (i = 0; i < document.frmcomment_shimbi.length; i++) 
	{
		// regex here to check name attribute
		var regex = new RegExp(name, "i");
		if (document.frmcomment_shimbi[i].getAttribute('name')==name) 
		{
			if (document.frmcomment_shimbi[i].checked == true) 
			{
				check="y";
				val=document.frmcomment_shimbi[i].value;
				cat_val=document.frmcomment_shimbi[i].alt;
				break;
			} 
		}
	}
	
	if(check=="n")
	{
		alert("オプションを選択してください");
		return false;
	}
	if(val=="その他" && me.text_2.value=="")
	{
		alert("「その他」はフィールドに記入ください");
		return false;
	}
	
	/*name="select_3"
	check="n";
	for (i = 0; i < document.frmcomment_shimbi.length; i++) 
	{
		// regex here to check name attribute
		var regex = new RegExp(name, "i");
		if (regex.test(document.frmcomment_shimbi[i].getAttribute('name'))) 
		{
			if (document.frmcomment_shimbi[i].checked == true) 
			{
				score_val=document.frmcomment_shimbi[i].value;
				check="y";
				break;
			} 
		}
	}
	if(check=="n")
	{
		alert("評価項目を選択してください");
		return false;
	}*/
	
	name="select_4"
	check="n";
	for (i = 0; i < document.frmcomment_shimbi.length; i++) 
	{
		// regex here to check name attribute
		var regex = new RegExp(name, "i");
		if (regex.test(document.frmcomment_shimbi[i].getAttribute('name'))) 
		{
			if (document.frmcomment_shimbi[i].checked == true) 
			{
				score_val=document.frmcomment_shimbi[i].value;
				check="y";
				break;
			} 
		}
	}
	if(check=="n")
	{
		alert("評価点数を選択してください");
		return false;
	}
	
	if(me.text_6.value=="")
	{
		alert("メールアドレスが入力されていません");
		me.text_6.focus();
		return false;
	}
	
	if(isEmail(me.text_6,"有効なメールアドレスを入力ください")==0)
	{
		return false;
	}
	
	return true;
}

//validation for bestkomi
function validate_bestkomi(me)
{
	
	if(isBlank(me.text_1,"動画のURL should not be blank.")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_2,"動画のタイトル should not be blank.")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_3,"おすすめコメン should not be blank.")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_4,"E-mailが入力されていません")==0)
	{
		return false;
	}
	
	if(isEmail(me.text_4,"有効なメールアドレスを入力ください")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_5,"年齢  should not be blank.")==0)
	{
		return false;
	}
	
	return true;
}

//validation for affibloask
function validate_affibloask(me)
{
	
	return true;
}

//validation for kuchikomi
function validate_kuchikomi(me)
{
	
	return true;
}

//validation for tellme
function validate_tellme(me)
{
	
	if(isBlank(me.text_1,"口コミのジャンルは？ should not be blank.")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_2,"口コミしたい もの・サービスは？ should not be blank.")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_3,"口コミ should not be blank.")==0)
	{
		return false;
	}
	
	if(isBlank(me.text_4,"メールアドレスが入力されていません")==0)
	{
		return false;
	}
	
	if(isEmail(me.text_4,"有効なメールアドレスを入力ください")==0)
	{
		return false;
	}
	
	return true;
}
