function setFormCookie()
{
setCookie("form_first_name", document.getElementById('account_first_name').value, 1);
setCookie("form_last_name", document.getElementById('account_last_name').value, 1);
setCookie("form_country", document.getElementById('account_country').value, 1);
setCookie("form_organisation", document.getElementById('account_organisation').value, 1);
setCookie("form_phone", document.getElementById('account_phone').value, 1);
setCookie("form_email", document.getElementById('account_email').value, 1);
setCookie("form_platform", document.getElementById('account_platform').value, 1);
setCookie("form_agree", document.getElementById('account_agree').value, 1);
setCookie("form_subscription", document.getElementById('account_subscription').value, 1);
return true;
}

function setLoginCookie()
{
setCookie("form_login_platform", document.getElementById('login_platform').value, 1);
setCookie("form_login_user", document.getElementById('login_user').value, 1);
return true;
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);

//document.cookie=c_name+ "=" +escape(value)+
//((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
document.cookie=c_name+ "=" +encodeURIComponent(value)+
((expiredays==null) ? "" : ";domain=gearapp.com;path=/registration/;expires="+exdate.toGMTString());
}

function platformValue()
{
	f = document.getElementById('account_platform');
	f.focus();
	$F = new String(f.value);
	f = document.getElementById('domain');
	f.focus();
	//document.getElementById('domain').innerHTML = 'http://<b>'+$F+'</b>.gearapp.com';
	f.innerHTML = 'http://<b>'+$F+'</b>.gearapp.com';
}
function changeCoutryDeploy()
{
var reg_form = document.reg_form.sbox;
//alert('u selected the value: '+document.reg_form.account_country.value);
if (document.reg_form.account_country.value == "HK" || document.reg_form.account_country.value == "TW" || document.reg_form.account_country.value == "CN")
{
	//alert('Hong Kong Server');
	document.reg_form.action = 'http://hk.gearapp.com/central/register/registrations.html';
	document.reg_login.action= 'http://hk.gearapp.com/central/register/orders.html';
}
else
{
	document.reg_form.action = 'http://us.gearapp.com/central/register/registrations.html';
	document.reg_login.action= 'http://us.gearapp.com/central/register/orders.html';	
}
//document.reg_form.account[country].selectedIndex = 1;
}
function setPlatformLogin()
{
	document.reg_login.action = 'http://'+document.reg_login.login_platform.value+'.gearapp.com/central/register/orders.html';
	//alert(document.reg_login.action);
}
function validate_email(field,alerttxt)
{
with (field)
  {
  apos=value.indexOf("@");
  dotpos=value.lastIndexOf(".");
  if (apos < 1||dotpos-apos<2)
    {//notice_invalid(field, alerttxt);
	return false;}
  else {return true;}
  }
}

function validate_phone(field,alerttxt)
{
//alert("dddddd0");
with (field)
  {
  //alert("dddddd1");
  if ( !isPhoneNumeric(value) )
    {
	//notice_invalid(field, alerttxt);
	return false;}
  else {return true;}
  }
}
function isPhoneNumeric(sText)
{
   var ValidChars = "0123456789()-+ ";
   var IsNumber=true;
   var Char;
	//alert("dddddd");
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i);
		//alert("dddddd");
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}
function isNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function validate_app_country(field,alerttxt)
{
	with (field)
  {
	if ( value == "empty"){return false;} else { return true;}
  }
}

function validate_required(field,alerttxt)
{

with (field)
  {
  if (value==null||value=="")
    {
      //notice_invalid(field, alerttxt);
      return false;
    }
  else
    {
      //notice_invalid(field, "");
      return true;
    }
  }
}

function notice_invalid(field, txt)
{
      if (txt == null)
        txt = "*"

      f = document.getElementById( field.id + "_notice");
      inner = "<font style='color: red;display:inline'>";
      inner += txt;
      inner += "</font>";
      if (f)
        f.innerHTML = inner ;
}


function validate_login(thisform)
{
	with(thisform)
	{

    required = ['login_selected_license', 'login_platform', 'login_user', 'login_password']

    any_empty = false;

    for (var x=0; x < required.length; x++)
    {
      f = document.getElementById(required[x]);
     if (validate_required( f )==false)
      {
        f.focus();
        notice_invalid(f);
        any_empty = true;
      }
      else notice_invalid(f, "");
    }

    if (any_empty)
    {
      notice_invalid(thisform, "The * field(s) are required.");
      return false;
    }

    f = document.getElementById('login_user');
    if (!validate_email(f ))
    {
        f.focus();
        notice_invalid(f);
        notice_invalid(thisform, "Invalid e-mail address.");
        return false;
    }

	}	
}
function validate_form(thisform)
{

notice_invalid( document.getElementById("server_message"), "");
//alert("dddddd0");
with (thisform)
  {
    required = ['account_first_name', 'account_last_name', 'account_country', 'account_organisation', 'account_email', 'account_password', 'account_re_password', 'account_platform', 'account_phone', 'account_selected_license']
//    txt = ["First name is required.", "Last name is required.", "E-mail address is required.", "Organisation is required.", ""]

    any_empty = false;

    for (var x=0; x < required.length; x++)
    {
      f = document.getElementById(required[x]);
     if (validate_required( f )==false)
      {
        f.focus();
        notice_invalid(f);
        any_empty = true;
      }
      else notice_invalid(f, "");
    }

    if (any_empty)
    {
      notice_invalid(thisform, "The * field(s) are required.");
      return false;
    }

    f = document.getElementById('account_email');
    if (!validate_email(f ))
    {
        f.focus();
        notice_invalid(f);
        notice_invalid(thisform, "Invalid e-mail address.");
        return false;
    }

    f1 = document.getElementById('account_password');
    f2 = document.getElementById('account_re_password');
    if (f1.value != f2.value)
    {
      f2.focus();
      notice_invalid(thisform, "Inconsistent passwords.");
      notice_invalid(f1);
      notice_invalid(f2);
      return false;
    }

    f = document.getElementById('account_phone');
	//alert("enter phone");
    if (!validate_phone(f ))
    {
        f.focus();
        notice_invalid(f);
        notice_invalid(thisform, "Invalid phone number.");
        return false;
    }
/*	
	f = document.getElementById('account_selected_license');
	if (!validate_app_country(f))
	{		
		notice_invalid(thisform, "Invalid selected app.");
		return false;
	}
	
	f = document.getElementById('account_country');
	if (!validate_app_country(f)){
		notice_invalid(thisform, "Invalid selected country.");
		return false;
	}
	*/
    f = document.getElementById('account_agree');
    if (!f.checked)
    {
      notice_invalid(f);
      notice_invalid(thisform, "You need to agree the terms of service and privacy policy before being member.");
      return false;
    }
  }
}

