// JavaScript Document

function validate_feedbackform ()
{

    valid = true;
    strErrorMessage = fixText("&#27425;&#12398;&#35352;&#20837;&#24517;&#38920;&#27396;&#12364;&#19981;&#23436;&#20840;&#12418;&#12375;&#12367;&#12399;&#28961;&#21177;&#12391;&#12377;:\n\n");
	
    var firstname = document.getElementById("inputFname");
    var email = document.getElementById("inputEmail");
    var industry = document.getElementById("inputIndustry");
    var ecategory = document.getElementById("inputEcategory");
	var customindustry = document.getElementById("inputCustomIndustry");
	var customcategory = document.getElementById("inputCustomCategory");
	var mobileno = document.getElementById("inputMobileNo");
	var state = document.getElementById("inputState");
	var businessno = document.getElementById("inputBusinessNo");
    
	     
	if (firstname.value == "" || firstname == null || isInteger(firstname.value))
	  {
      strErrorMessage += fixText("- &#21517;&#21069;&#12364;&#26410;&#35352;&#20837;&#12363;&#27491;&#12375;&#12367;&#12354;&#12426;&#12414;&#12379;&#12435;.\n"); 
      valid = false;
    }
    if ( (checkEmail(email.value)==false) || email == null || !check_email2(email.value))
	  {
      strErrorMessage += fixText("- E&#12513;&#12540;&#12523;&#12364;&#26410;&#35352;&#20837;&#12363;&#27491;&#12375;&#12367;&#12354;&#12426;&#12414;&#12379;&#12435;.\n"); 
      valid = false;
    }
	if(state != null && state.value != "" && isInteger(state.value))
	{
      strErrorMessage += fixText("- &#20303;&#25152;&#12364;&#26410;&#35352;&#20837;&#12363;&#27491;&#12375;&#12367;&#12354;&#12426;&#12414;&#12379;&#12435;.\n"); 
      valid = false;
	}
    if (industry.value == "0" || industry == null || industry.value == "")
	  {
      strErrorMessage += fixText("- &#26989;&#31278;&#12434;&#36984;&#25246;&#12375;&#12390;&#12367;&#12384;&#12373;&#12356;.\n"); 
      valid = false;
    }
	if (industry.value == "-1" && (customindustry.value == "" || customindustry == null)){
		strErrorMessage += fixText("- &#12381;&#12398;&#20182;&#12398;&#26989;&#31278;&#12364;&#26410;&#35352;&#20837;&#12391;&#12377;.\n"); 
      	valid = false;
	}
    if (ecategory.value == "0" || ecategory == null || ecategory.value == "")
	  {
      strErrorMessage += fixText("- &#12362;&#21839;&#12356;&#21512;&#12431;&#12379;&#20869;&#23481;&#12364;&#36984;&#25246;&#12373;&#12428;&#12390;&#12356;&#12414;&#12379;&#12435;.\n"); 
      valid = false;
    }
    if (ecategory.value == "-1" && (customcategory.value == "" || customcategory == null)){
		strErrorMessage += fixText("- &#12381;&#12398;&#20182;&#12398;&#12362;&#21839;&#12356;&#21512;&#12431;&#12379;&#20869;&#23481;&#12364;&#26410;&#35352;&#20837;&#12391;&#12377;.\n"); 
      	valid = false;
	}
	if(mobileno != null && mobileno.value != "")
	{
		if(!isInteger(mobileno.value))
		{
			strErrorMessage += fixText("- &#25658;&#24111;&#38651;&#35441;&#30058;&#21495;&#12364;&#26410;&#35352;&#20837;&#12363;&#27491;&#12375;&#12367;&#12354;&#12426;&#12414;&#12379;&#12435;.\n"); 
      		valid = false;
		}
	}
	if(businessno != null && businessno.value != "")
	{
		if(!isInteger(businessno.value))
		{
			strErrorMessage += fixText("- &#21220;&#21209;&#20808;&#38651;&#35441;&#30058;&#21495;&#12364;&#26410;&#35352;&#20837;&#12363;&#27491;&#12375;&#12367;&#12354;&#12426;&#12414;&#12379;&#12435;.\n"); 
      		valid = false;
		}
	}

    if (!valid)
    {
    alert (strErrorMessage);
	return false;
    }else{
		 //submitFeedback();
return true;
		
		}    
}
function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}
function checkEmail(nr_emailStr)
{	
   

//	nr_emailStr = eval(nr_emailStr);
	var nr_emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=nr_emailStr.match(nr_emailPat)
	if (matchArray==null) {
		return false;
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
		return false;
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false;
			}
		}
		return true
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		return false;
	}
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
		 return false;
	}
	if (len<2) {
		return false;
	}
	return true;
}
function check_email2(email)
{
	var email1 = email.substring(email.indexOf("@")+1);
	email1 = email1.substring(email1.indexOf(".")+1);
	email1 = email1.substring(email1.indexOf(".")+1);
	var email2 = email1.substring(email1.indexOf(".")+1);
	if(email1 != email2)
	{
		return false;
	}
	return true;
}

function validate_loginform ()
{
	var email= document.getElementById("txtEmail");
    var password = document.getElementById("txtPassword");
	strErrorMessage = "The following required fields were incomplete or invalid:\n\n";

	var valid = true;

 	if ( (checkEmail(email.value)==false) || email == null)
	  {
      strErrorMessage += "- Email not provided or is in the incorrect format.\n"; 
      valid = false;
    }
	if (password.value == "" || password == null)
	{
      strErrorMessage += "- Password not provided.\n"; 
      valid = false;
    }

	if (!valid)
    {
      alert (strErrorMessage);
		return valid;
    }
	return valid;
}
function validate_loginform ()
{
	var email= document.getElementById("txtEmail");
    var password = document.getElementById("txtPassword");
	strErrorMessage = "The following required fields were incomplete or invalid:\n\n";

	var valid = true;

 	if ( (checkEmail(email.value)==false) || email == null)
	  {
      strErrorMessage += "- Email not provided or is in the incorrect format.\n"; 
      valid = false;
    }
	if (password.value == "" || password == null)
	{
      strErrorMessage += "- Password not provided.\n"; 
      valid = false;
    }

	if (!valid)
    {
      alert (strErrorMessage);
		return valid;
    }
	return valid;
}

function validate_forgetpassword ()
{
	var email= document.getElementById("txtEmail");
    
	strErrorMessage = "The following required fields were incomplete or invalid:\n\n";

	var valid = true;

 	if ( (checkEmail(email.value)==false) || email == null)
	  {
      strErrorMessage += "- Email not provided or is in the incorrect format.\n"; 
      valid = false;
    }
	
	if (!valid)
    {
      alert (strErrorMessage);
		return valid;
    }
	return valid;
}

function validate_siform3 ()
{
    valid = true;
    strErrorMessage = "The following required fields were incomplete or invalid:\n\n";
	
    var firstname = document.getElementById("inputFname");
    var lastname = document.getElementById("inputLname");
    var email = document.getElementById("inputEmail");
    var country = document.getElementById("inputCountry");
    //var industry = document.getElementById("inputIndustry");
    var organisation = document.getElementById("inputOrgzn");
    var mobilenumber = document.getElementById("inputMobileNo");
    var mobilecountrycode = document.getElementById("inputMobileCtry");             
    var biznumber = document.getElementById("inputBusinessNo");
    var bizcountrycode = document.getElementById("inputBusinessCtry");
    var bizareacode = document.getElementById("inputBusinessArea");

	var password = document.getElementById("password");
	var cpassword = document.getElementById("password_confirm");
	
    

	  if (firstname.value == "" || firstname == null)
	  {
      strErrorMessage += "- First name not provided.\n"; 
      valid = false;
    }
    if (lastname.value == "" || lastname == null)
	  {
      strErrorMessage += "- Last name not provided.\n"; 
      valid = false;
    }
   // if ( (checkEmail(email.value)==false) || email == null)
	 // {
      //strErrorMessage += "- Email not provided or is in the incorrect format.\n"; 
      //valid = false;
   // }

    if (country.value == "0" || country == null || country.value == "")
	  {
      strErrorMessage += "- Country not selected.\n"; 
      valid = false;
    }
   

    if (( biznumber == null || biznumber.value == "")  ) 
	{
      strErrorMessage += "- Business number is required.\n"; 
      valid = false;
    }    

    if ( (mobilenumber != null && mobilenumber.value != "") && (mobilecountrycode == null || mobilecountrycode.value == "") ) 
	  {
      strErrorMessage += "- Mobile country code not provided.\n"; 
      valid = false;
    }
      
	/*
	if ( (biznumber != null && biznumber.value != "") && (bizcountrycode == null || bizcountrycode.value == "") ) 
	   {
       strErrorMessage += "- Business contact country code not provided.\n"; 
       valid = false;
       }
	*/



	if(password.value == "")
		{
		strErrorMessage += "- Please enter password.\n";
		valid = false;
		} else {
			if(password.value.length < 4)
				{
				strErrorMessage += "- Password length must be minimum 4 chars.\n";
				valid = false;
				} else {

				if(password.value != cpassword.value)
				{
				strErrorMessage += "- Please confirm password.\n";
				valid = false;
				}
				}
	}
   

    if (organisation == null || organisation.value == "")
	  {
      strErrorMessage += "- Organisation not provided.\n"; 
      valid = false;
    }

 var counter=0;

var vindustry =  document.formsi.chkIndustry;

    var vindustryAll = document.formsi.chkIndustryAll;

    if (!vindustryAll.checked)
	{
		for(var i=0;i<vindustry.length;i++)
	    {
	      if (vindustry[i].checked==true)
	      {
	        counter++; 
	      }
	    }
    
	    if (counter==0)
	    {
	      strErrorMessage += "- Choose at least one industry.\n"; 
	      valid = false;
	    }
	     if (counter>5)
	    {
	      strErrorMessage += "- You can choose only up to 5 industries.\n"; 
	      valid = false;
	    }
	}
    
    if (!valid)
    {
      alert (strErrorMessage);
    }
  
    return valid;
}


function validate_siform4 ()
{
    valid = true;
    strErrorMessage = "The following required fields were incomplete or invalid:\n\n";
	
    var firstname = document.getElementById("inputFname");
    var lastname = document.getElementById("inputLname");
    var email = document.getElementById("inputEmail");
    var country = document.getElementById("inputCountry");
    //var industry = document.getElementById("inputIndustry");
    var organisation = document.getElementById("inputOrgzn");
    var mobilenumber = document.getElementById("inputMobileNo");
    var mobilecountrycode = document.getElementById("inputMobileCtry");             
    var biznumber = document.getElementById("inputBusinessNo");
    var bizcountrycode = document.getElementById("inputBusinessCtry");
    var bizareacode = document.getElementById("inputBusinessArea");

var password = document.getElementById("password");
var cpassword = document.getElementById("password_confirm");
	
    

	

    


	  if (firstname.value == "" || firstname == null)
	  {
      strErrorMessage += "- First name not provided.\n"; 
      valid = false;
    }
    if (lastname.value == "" || lastname == null)
	  {
      strErrorMessage += "- Last name not provided.\n"; 
      valid = false;
    }
    if ( (checkEmail(email.value)==false) || email == null)
	  {
      strErrorMessage += "- Email not provided or is in the incorrect format.\n"; 
      valid = false;
    }





    if (country.value == "0" || country == null || country.value == "")
	  {
      strErrorMessage += "- Country not selected.\n"; 
      valid = false;
    }
   
	
	
    if (( biznumber == null || biznumber.value == "")  ) 
	{
      strErrorMessage += "- Business number is required.\n"; 
      valid = false;
    }
    /*
    if ( (mobilenumber == null || mobilenumber.value == "")  ) 
	  {
      strErrorMessage += "- Mobile number number is required.\n"; 
      valid = false;
      } else{

			if(!isInteger(mobilenumber.value))
				{
				strErrorMessage += "- Mobile No Area is in incorrect format.\n"; 
      			valid = false;
				}

       		}   */

    if ( (mobilenumber != null && mobilenumber.value != "") && (mobilecountrycode == null || mobilecountrycode.value == "") ) 
	  {
      strErrorMessage += "- Mobile country code not provided.\n"; 
      valid = false;
      } 

   /*
      if ( (biznumber != null && biznumber.value != "") && (bizcountrycode == null || bizcountrycode.value == "") ) 
	  {
      strErrorMessage += "- Business contact country code not provided.\n"; 
      valid = false;
    }
    */



	if(password.value == "")
		{
		strErrorMessage += "- Please enter password.\n";
		valid = false;
		} else {
			if(password.value.length < 4)
				{
				strErrorMessage += "- Password length must be minimum 4 chars.\n";
				valid = false;
				} else {

				if(password.value != cpassword.value)
				{
				strErrorMessage += "- Please confirm password.\n";
				valid = false;
				}
				}
	}
   

    if (organisation == null || organisation.value == "")
	  {
      strErrorMessage += "- Organisation not provided.\n"; 
      valid = false;
    }

 	var counter=0;

	var vindustry =  document.formsi.chkIndustry;

	var vindustryAll = document.formsi.chkIndustryAll;

    if (!vindustryAll.checked)
	{
		for(var i=0;i<vindustry.length;i++)
	    {
	      if (vindustry[i].checked==true)
	      {
	        counter++; 
	      }
	    }
    
	    if (counter==0)
	    {
	      strErrorMessage += "- Choose at least one industry.\n"; 
	      valid = false;
	    }
	     if (counter>5)
	    {
	      strErrorMessage += "- You can choose only up to 5 industries.\n"; 
	      valid = false;
	    }
	}
    
    if (!valid)
    {
      alert (strErrorMessage);
    }
  
    return valid;
}


/* ---- form submissin --- */


function submitFeedback()
{

var strURL = "/content/edb/sg/en_uk/index/feedback.FeedbackSubmit.jsp";
$("#errMsg").html("<div class=loading>Loading..</div>");
 var strData = "hello world";
     $.ajax({
		type: "POST",
		processData: true,
		url: strURL,
		data: strData,
		contentType: "application/x-www-form-urlencoded",
		success: function(msg){
			BuildTable(msg);
		},
		error: function(msg){
			BuildErrTable(msg);
		}
	});


}


function submitSINews()
	{

/*
var strURL = "/author/edb/sg/en_uk/index/registration.sinewssubmit.html";
$("#errMsg").html("<div class=loading>Loading..</div>");
 var strData = "";



var title = $("#inputTitle").val();
var fname = $("#inputFname").val();
var lname =  $("#inputLname").val(); 
var ctry1 =  $("#inputCountry").val(); 
var email =  $("#inputEmail").val(); 

var ind =  $("#inputIndustry").val();
var indothers =  $("#inputCustomIndustry").val();

var organisation = $("#inputOrgzn").val();

var mobilenumber = $("#inputMobileNo").val();
var mobilecountrycode = $("#inputMobileCtry").val();             

var biznumber = $("#inputBusinessNo").val();
var bizcountrycode =$("#inputBusinessCtry").val();
var bizareacode = $("#inputBusinessArea").val();

var state = $("#inputState").val();



strData = "inputTitle="+ title +"inputFname="+ fname + "&inputLname="+ lname + "&inputCountry="+ ctry1 + "&inputEmail="+ email + "&inputIndustry="+ ind + "&inputCustomIndustry="+ indothers + "&inputOrgzn="+ organisation + "&inputMobileNo="+ mobilenumber + "&inputMobileCtry="+ mobilecountrycode + "&inputBusinessNo="+ biznumber + "&inputBusinessCtry="+ bizcountrycode + "&inputBusinessArea="+ bizareacode + "&state="+ inputState;





     $.ajax({
		type: "POST",
		processData: true,
		url: strURL,
		data: strData,
		contentType: "application/x-www-form-urlencoded",
		success: function(msg){
			BuildTable(msg);
		},
		error: function(msg){
		//	BuildErrTable(msg);
		}
	});

*/
	}


function setIp2Location(str)
{
/*
//alert(str);

	var strURL = "/author/edb/sg/en_uk/index.Ip2Loc.html";
 	var strData = "strCtry=" + str;
     $.ajax({
		type: "POST",
		processData: true,
		url: strURL,
		data: strData,
		contentType: "application/x-www-form-urlencoded",
		success: function(msg){
		//	BuildTable(msg);
		}
	
	});
*/

}



function BuildErrTable(msg)
{

 $("#errMsg").html("<div class=error>"+ msg + "</div>");
}

function BuildTable(msg)
 {
 $("#errMsg").html(msg);
 }

function SetAllCheckBoxes(FormName, FieldName, CheckValue)
{
	if(!document.forms[FormName])
	{
		return;
	}	
	
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	
	if(!objCheckBoxes)
	{
		return;
	}
	
	var countCheckBoxes = objCheckBoxes.length;
	
	if(!countCheckBoxes)
	{
		objCheckBoxes.checked = CheckValue;
	}
	else
	{
		for(var i = 0; i < countCheckBoxes; i++)
		{
			objCheckBoxes[i].checked = CheckValue;
			if (CheckValue)
			{
				objCheckBoxes[i].disabled = true;
			}
			else
			{
				objCheckBoxes[i].disabled = false;
			}
			
		}	
	}
}

function IndustryCheckboxToggle(FormName, FieldName)
{
	if(!document.forms[FormName])
	{
		return;
	}
	
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	
	if(!objCheckBoxes)
	{
		return;
	}
	
	var countCheckBoxes = objCheckBoxes.length;
	
    var counter = 0;
	var j = 0;
	var uncheckedArray = new Array();

	for(var i = 0; i < countCheckBoxes; i++)
	{
		if (objCheckBoxes[i].checked)
		{	
			counter = counter+1;
		}
		else
		{
			uncheckedArray[j] = objCheckBoxes[i];
			j = j+1;
		}	
	}	

	var countCheckBoxes = uncheckedArray.length;
	
	for( j = 0; j < countCheckBoxes; j++)
	{
		if (counter < 5)
		{
			uncheckedArray[j].disabled = false;
		}
		else
		{
			uncheckedArray[j].disabled = true;
		}
	}
}


function fixText(s){
if (!s) return '';
s = s.replace("<li>","");
s = s.replace("</li>","");
var arr = s.split('&#');
var s2='';
for (var i=0;i<arr.length;i++){ //ignore last element, it's empty
str=arr[i];
len=str.length;
j=str.indexOf(';');
if(j >= 0){
st=str.substring(0,j);
s2+=String.fromCharCode(st);
if(len > 6)
s2+=str.substring(j+1,len);
}
else{
s2+=str;
}
}
return s2;
}
