
  
var speed = 1000; 
var wait = 3; 


function pausemillis(millis)
{
	var date = new Date();
	var curDate = null;
	
	do { curDate = new Date(); }
	while(curDate - date < millis);
} 

function updateinfo(){
  if(wait == 0){
	  document.getElementById("id_acceptButton").value = "I Agree";
	  document.getElementById("id_acceptButton").disabled = false;
  }
  else{
	document.getElementById("id_acceptButton").value = "Read Terms ("+wait+")";
    wait--;
    window.setTimeout("updateinfo()",speed);
    document.getElementById("id_acceptButton").disabled = true;
  }
}

function showregform() {
	var div_regform = document.getElementById("id_userprofile");
	var div_userterms = document.getElementById("id_userterms");
	div_userterms.style.display = "none";
	div_regform.style.display = "block";
}


function showterms() {
	var div_regform = document.getElementById("id_userprofile");
	var div_userterms = document.getElementById("id_userterms");
	div_userterms.style.display = "block";
	div_regform.style.display = "none";
}

function showshopdetail() {
	var div_storelogo = document.getElementById("id_storelogo");
	var div_personalurl = document.getElementById("id_personalurl");
	var div_storedesc = document.getElementById("id_storedesc");
	var div_storecategory = document.getElementById("id_storecategory");
	var div_storebusiness = document.getElementById("id_storebusiness");
	//var div_country = document.getElementById("id_country");
	//var div_zone = document.getElementById("id_zone");
	var div_gender = document.getElementById("id_gender");
	var div_dob = document.getElementById("id_dob");
	div_storelogo.style.display = "block";
	div_personalurl.style.display = "block";
	div_storedesc.style.display = "block";
	div_storecategory.style.display = "block";
	div_storebusiness.style.display = "block";
	//div_country.style.display = "block";
	//div_zone.style.display = "none";
	div_gender.style.display = "none";
	div_dob.style.display = "none";
}

function hideshopdetail() {
	var div_storelogo = document.getElementById("id_storelogo");
	var div_personalurl = document.getElementById("id_personalurl");
	var div_storedesc = document.getElementById("id_storedesc");
	var div_storecategory = document.getElementById("id_storecategory");
	var div_storebusiness = document.getElementById("id_storebusiness");
	//var div_country = document.getElementById("id_country");
	//var div_zone = document.getElementById("id_zone");
	var div_gender = document.getElementById("id_gender");
	var div_dob = document.getElementById("id_dob");
	div_storelogo.style.display = "none";
	div_personalurl.style.display = "none";
	div_storedesc.style.display = "none";
	div_storecategory.style.display = "none";
	div_storebusiness.style.display = "none";
	//div_country.style.display = "none";
	//div_zone.style.display = "block";
	div_gender.style.display = "block";
	div_dob.style.display = "block";
	
	dwr.util.setValue("id_imagefile", "");
	dwr.util.setValue("id_vshopname", "");
	dwr.util.setValue("id_shopDescription", "");
	dwr.util.setValue("id_shopBusiness", "");
	dwr.util.setValue("id_invalidVShopName", "");
}


  function init() {
	  readTerms = true;
	  disableSaveButton();
	  //updateinfo();
  }
  
  var readTerms = true;
  
  function noreadterms() {
	  readTerms = false;
	  disableSaveButton();
  }
  
  function havereadterms() {
	  readTerms = true;
	  enableSaveButton();
  }
 
  function disableSaveButton() {
	  var btn = document.getElementById("save");
	  readTerms = false;
	  btn.disabled = true;
  }

  function enableSaveButton() {
	  var btn = document.getElementById("save");
	  readTerms = true;
	  btn.disabled = false;
  }

  window.onload = init;
  
  //window.onload = function() {
	//  alert("onload");
  //}
  
  function checkstoredescription() {
	  var str = dwr.util.getValue("id_shopDescription");
	  var len = str.length;
	  if(len > 5000) {
		  dwr.util.setValue("id_shopdescriptiontoolong", "The descritpion is too long - English: 5000 chars, Chinese: 100 chars.");
	  }
	  else {
		  dwr.util.setValue("id_shopdescriptiontoolong", "");
	  }
  }
  
  function checkstorebusiness() {
	  var str = dwr.util.getValue("id_shopBusiness");
	  var len = str.length;
	  if(len > 8000) {
		  dwr.util.setValue("id_shopbusinesstoolong", "The instruction is too long - English: 8000 chars, Chinese: 1200 chars.");
	  }
	  else {
		  dwr.util.setValue("id_shopbusinesstoolong", "");
	  }
  }
  
  /*
   * This is an important function. checkAccount() is triggered by "Register Now" button
   * not the challAll() function.
   */
  function checkAccount() {
	  var email = dwr.util.getValue("id_email");
	  var password = dwr.util.getValue("id_password");
	  var password2 = dwr.util.getValue("id_password2");
	  var userName = dwr.util.getValue("id_userName");
	  var telephone = dwr.util.getValue("id_telephone");
	  var shopName = dwr.util.getValue("id_vshopname");
	  //var shop = dwr.util.getValue("id_shop");
	  
	  var emailValid = false;
	  var passwordValid = false;
	  var password2Valid = false;
	  var userNameValid = false;
	  var telephoneValid = false;
	  var shopNameValid = false;
	  
	  var stillWaitingData = true;
	  
	  
	  
	  /*
	  //Shop name
	  
	  GenericValidator.checkShopName(shopName, function(data){
		  if(data == true)
		  {
			  dwr.util.setValue("id_invalidVShopName", "");
			  shopNameValid = true;
		  }
		  else
		  {
			  dwr.util.setValue("id_invalidVShopName", "Invalid store name or already occupied");
			  
		  }
		  
		  stillWaitingData = false;
		  
	  } );
	  
	  
	  	  //check email
	  
	  
	  GenericValidator.checkEmail(email, function(data) {
		  if(data == true)
		  {
			  
		  	  dwr.util.setValue("id_invalidEmailMsg", "");
		  	  emailValid = true;
		  	  
		  }
		  else
		  {
			  
			  dwr.util.setValue("id_invalidEmailMsg", "Invalid email format or the email address already occupied!");
			  
		  }
	  });
	 
	  
  	  //check password
	  GenericValidator.checkPassword(password, function(data){
		  if(data == true)
		  {
			  dwr.util.setValue("id_invalidPasswordMsg", "");
			  passwordValid = true;
		  }
		  else
		  {
			  dwr.util.setValue("id_invalidPasswordMsg", "Invalid password, password must be at least 6 characters");
			  
		  }
	  } );
	  
	  //check password2
	  GenericValidator.checkPasswords(password, password2, function(data){
		  if(data == true)
		  {
			  dwr.util.setValue("id_invalidPassword2Msg", "");
			  password2Valid = true;
		  }
		  else
		  {
			  dwr.util.setValue("id_invalidPassword2Msg", "Invalid confirm password, confirm password must be same as password");
			  
		  }
	  } );
	  
	  //check user name
	  GenericValidator.checkUserName(userName, function(data){
		  if(data == true)
		  {
			  dwr.util.setValue("id_invalidUserNameMsg", "");
			  userNameValid = true;
		  }
		  else
		  {
			  dwr.util.setValue("id_invalidUserNameMsg", "Invalid user name or already exists");
			  
		  }
	  } );
	  
	  //check telephone
	  GenericValidator.checkTelephone(telephone, function(data){
		  if(data == true)
		  {
			  dwr.util.setValue("id_invalidTelephoneMsg", "");
			  telephoneValid = true;
		  }
		  else
		  {
			  dwr.util.setValue("id_invalidTelephoneMsg", "Invalid telephone");
			  
			  //disableSaveButton();
		  }
	  } );
	 */
	  
	  
	  //pausemillis(5000);
	  //alert('waiting 1? '+stillWaitingData);
	  
	  /*
	  var aNumber = 0;
	  do{
		  aNumber = 1 * 1;
	  }while(stillWaitingData);
		 */
	  
	  
	  //alert('waiting 2 ? '+stillWaitingData);
	  
	    
			
	  /*
	  if(emailValid && passwordValid && password2Valid
	  && userNameValid && telephoneValid && shopNameValid){
		  
		  return true;
	  }
	  
      
	  return false;
	  
	 */
	  return true;
  }

  
  function checkAll() {
	  //alert("ok");
	  var email = dwr.util.getValue("id_email");
	  var password = dwr.util.getValue("id_password");
	  var password2 = dwr.util.getValue("id_password2");
	  var userName = dwr.util.getValue("id_userName");
	  var telephone = dwr.util.getValue("id_telephone");
	  var shopName = dwr.util.getValue("id_vshopname");
	  
	  GenericValidator.checkAll(email, password, password2, userName, telephone, shopName, function(data) {
		  
		  var btn = document.getElementById("save");
		  if(data && readTerms)
		  {
			  btn.disabled = false;
			  return true;
		  }
		  else
		  {
			  //btn.disabled = true;
			  return false;
		  }
	  });
	  
	  
  }
  
  function checkEmail() {
	  var email = dwr.util.getValue("id_email");
	  GenericValidator.checkEmail(email, loadinfo);
  }
  
  function loadinfo(data) {
	  if(data == true)
	  {
	  	  dwr.util.setValue("id_invalidEmailMsg", "");
	  	  //checkAll();
	  }
	  else
	  {
		  dwr.util.setValue("id_invalidEmailMsg", "Invalid email format or the email address already occupied!");
		  //disableSaveButton();
	  }
  	}

  function checkPassword() {
	  var password = dwr.util.getValue("id_password");
	  GenericValidator.checkPassword(password, function(data){
		  if(data == true)
		  {
			  dwr.util.setValue("id_invalidPasswordMsg", "");
			  //checkAll();
		  }
		  else
		  {
			  dwr.util.setValue("id_invalidPasswordMsg", "Invalid password, password must be at least 6 characters");
			  //disableSaveButton();
		  }
	  } );
	  
  }
  
  function checkPassword2() {
	  var password = dwr.util.getValue("id_password");
	  var password2 = dwr.util.getValue("id_password2");
	  GenericValidator.checkPasswords(password, password2, function(data){
		  if(data == true)
		  {
			  dwr.util.setValue("id_invalidPassword2Msg", "");
			  //checkAll();
		  }
		  else
		  {
			  dwr.util.setValue("id_invalidPassword2Msg", "Invalid confirm password, confirm password must be same as password");
			  //disableSaveButton();
		  }
	  } );
  }
  
  function checkShopName() {
	  var shopName = dwr.util.getValue("id_vshopname");
	  GenericValidator.checkShopName(shopName, function(data){
		  if(data == true)
		  {
			  dwr.util.setValue("id_invalidVShopName", "");
			  //checkAll();
		  }
		  else
		  {
			  dwr.util.setValue("id_invalidVShopName", "Invalid store name or already occupied");
			  //disableSaveButton();
		  }
	  } );
  }
  
  function checkUserName() {
	  var userName = dwr.util.getValue("id_userName");
	  GenericValidator.checkUserName(userName, function(data){
		  if(data == true)
		  {
			  dwr.util.setValue("id_invalidUserNameMsg", "");
			  //checkAll();
		  }
		  else
		  {
			  dwr.util.setValue("id_invalidUserNameMsg", "Invalid name or already exists");
			  //disableSaveButton();
		  }
	  } );
  }
  
  function checkTelephone() {
	  var telephone = dwr.util.getValue("id_telephone");
	  GenericValidator.checkTelephone(telephone, function(data){
		  if(data == true)
		  {
			  dwr.util.setValue("id_invalidTelephoneMsg", "");
			  //checkAll();
		  }
		  else
		  {
			  dwr.util.setValue("id_invalidTelephoneMsg", "Invalid telephone");
			  //disableSaveButton();
		  }
	  } );
  }
  

  function resetImageFile(imageFileID, fileID, logoID)
  {
  	dwr.util.setValue(imageFileID, "");
  	dwr.util.setValue(fileID, "");
  	
	var div_logo = document.getElementById(logoID); 
	div_logo.style.background = "url('/GiftExCentos/images/highstreet_store.jpg')";
	//div_logo.style.backgroundRepeat="no-repeat";
	//div_logo.style.backgroundPosition="center center";
  }
  
  function checkShopDescriptionLength() {
	   // Limit the length of the textarea
	var maximumLength = 5000;
	
		var dataLength = CKEDITOR.instances.id_shopDescription.getData().length;
		
		if(dataLength > maximumLength){

			var needRemove = dataLength - maximumLength;									
			alert('About My Store excceds limit of '+maximumLength+' characters. Please remove '+needRemove+' characters.');
			CKEDITOR.instances.id_shopDescription.focus();
			return false;
		}
		
		return true;
}

function isShopDescriptionEmpty() {
	   
	
		var dataLength = CKEDITOR.instances.id_shopDescription.getData().length;
		
		if(dataLength < 3){
			alert('Please provide the description of your store.');	
			CKEDITOR.instances.id_shopDescription.focus();
			return true;
		}
		
		return false;
}


