
function trim(untrimmed) {
  return untrimmed.replace('/(^\s+)|(\s+$)/g', '');
}

function submitOnEnter(event,inForm,register) {
  var oldBrowser = (document.all) ? false : true;
  var keyCode = 0;
  if (oldBrowser) {
    keyCode = event.which;
  } else {
    keyCode = event.keyCode;
  }
  if (keyCode == 13) {
    if (register)
      registerScript(inForm);
    else
      submitScript(inForm);
  }
  if (oldBrowser) {  
    if (keyCode==13) return false; //event.which = 0;
  } else {
    if (keyCode==13) return false; //event.keyCode = 0;
  }  
  keyCode = 0;
  return true;
}

function checkFirstCaps(inval) {
  var pos;
  for (pos = 0; pos < inval.length; pos++) {
    if (pos==0) {
      inval = inval.charAt(0).toUpperCase() + inval.substring(1);
    } else {
      if (inval.charAt(pos-1) == ' ' || inval.charAt(pos-1) == '-') {
        inval = inval.substr(0,pos)+inval.charAt(pos).toUpperCase()+inval.substring(pos+1);
      } else {
        inval = inval.substr(0,pos)+inval.charAt(pos).toLowerCase()+inval.substring(pos+1);
      }
    }
  }
  return inval;
}

function submitScript(inForm) {
  submitValid=true;  
  for (var x=0;x < inForm.length;x++) {
    if  (inForm[x].type=="text" || inForm[x].type=="password") {
      if (inForm[x].getAttribute("required")=='true') {
        var t = inForm[x].value;
        if (trim(t)=="") {
          alert(inForm[x].getAttribute("fullname") + ' is missing.');
          submitValid=false;
        }
      } else if (inForm[x].getAttribute("required")=='login') {
        var t = (inForm[x].getAttribute("value"));
        re = new RegExp("^[-a-zA-Z0-9]+$","g");
        if (!re.test(t)) {
          alert(inForm[x].getAttribute("fullname") + ' is incorrect.');          
          submitValid=false;
        }
      } else if (inForm[x].getAttribute("required")=='text') {
        var t = (inForm[x].getAttribute("value"));
        re = new RegExp("^[a-zA-Z0-9]+$","g");
        if (!re.test(t)) {
          alert(inForm[x].getAttribute("fullname") + ' is incorrect.');          
          submitValid=false;
        }
      } else {
        //alert(inForm[x].getAttribute("fullname") + ' is not required.');
      }  
      
      if ( inForm[x].getAttribute("dataType")=="int" && trim(inForm[x].value)!="" && !isInteger(inForm[x].value)) {
        alert(inForm[x].getAttribute("fullname") + ' is not an integer.');          
        submitValid=false;
      }  
      if ( inForm[x].getAttribute("dataType")=="real" && trim(inForm[x].value)!="" && isNaN(inForm[x].value)) {
        alert(inForm[x].getAttribute("fullname") + ' is not a number.');          
        submitValid=false;
      }  

      if (inForm[x].getAttribute("dataType")=="currency" && trim(inForm[x].value)!="") {
        if (!currencyValid(trim(inForm[x].value))) {
          alert(inForm[x].getAttribute("fullname") + ' is not a valid amount of money.');                    
          submitValid=false;        
        }
      }   

      if (inForm[x].getAttribute("dataType")=="email" && trim(inForm[x].value)!="") {
        if (!emailValid(trim(inForm[x].value))) {
          alert(inForm[x].getAttribute("fullname") + ' is not a valid e-mail address.');                    
          submitValid=false;        
        }
      }   
            
      if (inForm[x].getAttribute("dataType")=="date" && trim(inForm[x].value)!="") {
        var tempDate = trim(inForm[x].value);
        var tempName = trim(inForm[x].name);
        var tdo = parseDate(tempDate);
        if (tdo) {
          //alert  (  'field_hidden_'+tempName.substring( tempName.indexOf('_')+1)     ) ;
          document.getElementById('field_hidden_'+tempName.substring( tempName.indexOf('_')+1)).setAttribute('value',tdo.getFullYear()+'-'+ (tdo.getMonth()+1)+'-'+tdo.getDate());
        } else {
          alert(inForm[x].getAttribute("fullname") + ' is invalid.');                    
          submitValid=false;
        } 
      }  
    } else if  (inForm[x].type=="file") {
      if (inForm[x].getAttribute("required")=='true') {
        var t = inForm[x].value;
        if (trim(t)=="") {
          alert(inForm[x].getAttribute("fullname") + ' is missing.');
          submitValid=false;
        }
      }    
    } else if (inForm[x].type=='textarea') {
      if (inForm[x].getAttribute("required")=='true') {
        var t = inForm[x].value;
        if (trim(t)=="") {
          alert(inForm[x].getAttribute("fullname") + ' is missing.');
          submitValid=false;
        }
        if (inForm[x].getAttribute("maxlength") != "" && inForm[x].getAttribute("maxlength") != null) {
          if (parseInt(inForm[x].getAttribute("maxlength"))<=inForm[x].value.length) {
            longVars += inForm[x].getAttribute("fullname") + "\n";
            submitValid=false;           
          }
        }
        
      }  
    } else if (inForm[x].type=='select-one') {
      if (inForm[x].getAttribute("required")=='true') {
        var t = inForm[x].value;
        if (trim(t)=="") {
          alert(inForm[x].getAttribute("fullname") + ' is missing.');
          submitValid=false;
        }
      }
     
    }
      
      
    
    
  }  
  if (!submitValid) 
    alert('Cannot submit form...');
  else
    inForm.submit();
}  

function checkPassword(p1,p2) {
  passwordValid = true;
  if (p1.value!=p2.value) {
    alert('Passwords are not equal.');
    passwordValid=false;
  } 
  else if (p1.value.length < 4) {
    alert('Password requires at least 4 characters.');
    passwordValid=false;
  }
  return passwordValid;
}


function isDigit()
{
	return ((event.keyCode >= 48) && (event.keyCode <= 57));
	//return ((kc >= 48) && (kc <= 57));
}

function charIsDigit (c){
  return ((c >= "0") && (c <= "9"));
}

function emailValid(emailAddress) {
  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/ ;
  return filter.test(emailAddress); 
}
function currencyValid(currency) {
  //var filter1 = '/^\$?\d{1,3}(,?\d{3})*(\.\d{1,2})?$/' ;
  //var filter2 = '/^\$?\d{1.3}(.?\d{3})*(\,\d{1.2})?$/' ;
  //return filter1.test(currency) || filter2.test(currency); 
  return true;
}

function convertMultipleSelect(refForm,refSelect,prefix) {
	var hiddenInput;
	for (var x = 0; x < refSelect.length; x++) {
		if (refSelect[x].selected) {
			hiddenInput = document.createElement('input');
			hiddenInput.setAttribute('type','hidden');
			hiddenInput.name = prefix + refSelect[x].value;
			hiddenInput.id = prefix + refSelect[x].value;
			hiddenInput.value = 'on';
			refForm.appendChild(hiddenInput);
			
				
		}
	}
	
}

function removeMultipleSelect(refForm,refSelect,prefix) {
	var hiddenInput;
	var parent;
	var childElement;
	for (var x = 0; x < refSelect.length; x++) {
		childElement=document.getElementById(prefix + refSelect[x].value);
		if (childElement) {
	    	parent=childElement.parentNode;
	    	parent.removeChild(childElement);
    	
		}
				
	}
	
}

function isInteger(num) {
  var i;
  var isInt = true;
  for (i = 0; i < num.length; i++) {
    if (!charIsDigit(num.substring(i,i+1)))
      isInt = false;
  }
  return isInt;
}

//srmonth = (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );


function registerScript(inForm) {
  var instType;
  //for (var x=0;x < inForm.length;x++) {
  //  if (inForm[x].getAttribute("name")=="inst_type")
  //    if (inForm[x].getAttribute("checked"))
  //      instType=inForm[x].getAttribute("value");
  //}
  if(document.getElementsByName('inst_type')[1].checked)
    instType='new';
  else
    instType='existing';
  
  var displayEmail = document.getElementsByName('displayEmail');
  submitValid = false;
  for (var x = 0; x < displayEmail.length; x++)
  {
  	if (displayEmail[x].checked)
  		submitValid = true;
  }
  
  if (!submitValid)
  {
  	alert('Please choose a privacy option');
  	return false;
  }

  if (document.getElementById('ex_institute').value=='' && instType=='existing') {
    submitValid=false;
    alert('Please choose an institute.');
  }

  for (var x=0;x < inForm.length;x++) {
    if  (inForm[x].type=="text" || inForm[x].type=="password") {
      if (inForm[x].getAttribute("required")=='true') {
        var t = inForm[x].value;
        var tn = inForm[x].getAttribute("name");
        
        if (  (trim(t)=="" && tn != 'ex_institute' && tn != 'new_institute' && tn !='new_city' && tn !='new_postalcode') 
           || (trim(t)=="" && instType=='existing' && tn == 'ex_institute' )
           || (trim(t)=="" && instType=='new' && (tn=='new_city' || tn == 'new_institute' || tn=='new_postalcode' ) )
        
           ) {
          alert(inForm[x].getAttribute("fullname") + ' is missing.');
          submitValid=false;
        }
      } else if (inForm[x].getAttribute("required")=='login') {
        var t = (inForm[x].getAttribute("value"));
        re = new RegExp("^[-a-zA-Z0-9]+$","g");
        if (!re.test(t)) {
          alert(inForm[x].getAttribute("fullname") + ' is incorrect.');          
          submitValid=false;
        }
      } else if (inForm[x].getAttribute("required")=='text') {
        var t = (inForm[x].getAttribute("value"));
        re = new RegExp("^[a-zA-Z0-9]+$","g");
        if (!re.test(t)) {
          alert(inForm[x].getAttribute("fullname") + ' is incorrect.');          
          submitValid=false;
        }
      } else {
        //alert(inForm[x].getAttribute("fullname") + ' is not required.');
      }  
      
      if ( inForm[x].getAttribute("dataType")=="int" && trim(inForm[x].value)!="" && !isInteger(inForm[x].value)) {
        alert(inForm[x].getAttribute("fullname") + ' is not an integer.');          
        submitValid=false;
      }  
      if ( inForm[x].getAttribute("dataType")=="real" && trim(inForm[x].value)!="" && isNaN(inForm[x].value)) {
        alert(inForm[x].getAttribute("fullname") + ' is not a number.');          
        submitValid=false;
      }  

      if (inForm[x].getAttribute("dataType")=="currency" && trim(inForm[x].value)!="") {
        if (!currencyValid(trim(inForm[x].value))) {
          alert(inForm[x].getAttribute("fullname") + ' is not a valid amount of money.');                    
          submitValid=false;        
        }
      }   

      if (inForm[x].getAttribute("dataType")=="email" && trim(inForm[x].value)!="") {
        if (!emailValid(trim(inForm[x].value))) {
          alert(inForm[x].getAttribute("fullname") + ' is not a valid e-mail address.');                    
          submitValid=false;        
        }
      }   
            
      if (inForm[x].getAttribute("dataType")=="date" && trim(inForm[x].value)!="") {
        var tempDate = trim(inForm[x].value);
        var tempName = trim(inForm[x].name);
        var tdo = parseDate(tempDate);
        if (tdo) {
          //alert  (  'field_hidden_'+tempName.substring( tempName.indexOf('_')+1)     ) ;
          document.getElementById('field_hidden_'+tempName.substring( tempName.indexOf('_')+1)).setAttribute('value',tdo.getFullYear()+'-'+ (tdo.getMonth()+1)+'-'+tdo.getDate());
        } else {
          alert(inForm[x].getAttribute("fullname") + ' is invalid.');                    
          submitValid=false;
        } 
      }  
    } else if (inForm[x].type=="checkbox") {
      if (inForm[x].getAttribute("name")=="agreeTerms" && !inForm[x].checked) {
        alert('You must agree to the terms and conditions to register.');
        submitValid=false;
      }            
    } else if (inForm[x].type=='textarea') {
      if (inForm[x].getAttribute("required")=='true') {
        var t = inForm[x].value;
        var tn = inForm[x].getAttribute("name");
        if (   (trim(t)=="" && instType=='new' && tn == 'new_mailing' )
        
           ) {
          alert(inForm[x].getAttribute("fullname") + ' is missing.');
          submitValid=false;
        }
        
      }  
      
    } else if (inForm[x].type=='select-one') {
      if (inForm[x].getAttribute("required")=='true') {
        var t = inForm[x].value;
        var tn = inForm[x].getAttribute("name");
        if (   (trim(t)=="" && instType=='new' && tn == 'org_country' )
        
           ) {
          alert(inForm[x].getAttribute("fullname") + ' is missing.');
          submitValid=false;
        }
      }
     
    }
  }  
  if (!submitValid) 
    alert('Can not submit form...');
  else
    inForm.submit();

}

function InvoiceDetailsFormOnSubmit()
{
	var errorMessage = '';
	if (document.getElementById('details_organisation').value=='')
		errorMessage += 'Organisation name is missing.\n';
	if (document.getElementById('details_address').value=='')
		errorMessage += 'Address is missing.\n';
	if (document.getElementById('details_postalcode').value=='')
		errorMessage += 'Postalcode is missing.\n';
	if (document.getElementById('details_city').value=='')
		errorMessage += 'City is missing.\n';
	if (document.getElementById('details_country').value=='')
		errorMessage += 'Country is missing.\n';
	if (document.getElementById('details_email').value=='')
		errorMessage += 'E-mail is missing.\n';
		
	if (!document.getElementById('payment_paypal').checked && !document.getElementById('payment_banktransfer').checked)
		errorMessage += 'Payment method has not been chosen.\n';
				
	if (errorMessage !='')	
	{
		alert('Errors in form:\n' + errorMessage);				
		return false;
	}
	else
	{
		return true;
	}
	
}

function setInvoiceType()
{
	var regTypeU = document.getElementById('regTypeU');
	var instTypeNew = document.getElementById('inst_type_new');
	var placeholder = document.getElementById('subscription_type_placeholder');
	
	if (!instTypeNew.checked && !regTypeU.checked)
	{
		placeholder.style.display = 'table';
	}
	else
	{
		placeholder.style.display = 'none';		
	}
		
}

