// Strip all non-integer characters from a form 
// input. This means spaces too.
function cleanNumberField(theElmId){
	var theElm = document.getElementById(theElmId);
	var numRegExp = /\D/g; // Look for any non-digit ( globally - throughout the field )
	var cleanString; // This will hold the return of the replace.
	var valueString = theElm.value; // the value of the textbox
	cleanString = valueString.replace(numRegExp, ''); // Doesn't change the field value, returns a clean copy of the clean field value
	theElm.value = cleanString; // now set the value of the textbox with the clean value
}

// Remove all crazy characters from the fields
// Mainly to prevent CSS attacks. 
function removeCharacters(theElmId){

	var theElm = document.getElementById(theElmId);
	var cleanRegExp = /\D\W/g;
	var cleanString;
	var valueString = theElm.value;
	
	cleanString = valueString.replace(cleanRegExp, '');
	theElm.value = cleanString;
}

// Remove white spaces from strings. Email addresses should use this
// function.
function removeWhiteSpace(theElmId){
	var theElm = document.getElementById(theElmId);
	var regExp = /\W/g;
	var cleanString;
	var valueString;
	
	valueString = theElm.value;
	cleanString = valueString.replace(regExp, '');
}

// Use this on required text boxes. If the length is 0
// or less, return false, otherwise return true.
function checkReqTextBox(theElmId){

	var theElm = document.getElementById(theElmId);
	var len;
	var returnValue = false; // Assume the worst.
	len = theElm.value.length;
	if( len <= 0 ){
		returnValue = false;
	}else{	
		returnValue = true;
	}
	
	return returnValue;
}

/* Just checks if there is a value in a text box - if reqLen 
    is not null, check to make sure the length of the value is 
    >= the reqLen 
*/
function checkReqText(elmId, reqLen){
    var returnValue = false;
    elm = document.getElementById('elmId');
    if( elm.value.length > 0 ){
        returnValue = true;
    }
    if( reqLen != null ){
        // reqLen was not null, so make sure the input meets the req len
        if(elm.value.length >= reqLen){
            returnValue = true
        }
    }
    return returnValue;
}


/*
    Validation on createAccount.html. The values are checked again in the
    accountCreate servlet. But they need to be checked here to make the 
    process run smoothly
*/
function validateNewAccount(){
    // Three fields ( email, password, confPassword )
    var emailElm = document.getElementById('email');
    var passwordElm = document.getElementById('password');
    var userNameElm = document.getElementById('email');
    var confElm = document.getElementById('confPassword');        
    var retVal = true; // This will be set to false if any of the req's are not me
    
    /* Check there is an email */
    if( userNameElm.value.length < 5 ) {
	  var unError = document.getElementById('userNameError');
	  unError.innerHTML = 'Username must be 5 or more characters.';
	  retVal = false;
    }

    if( emailElm.value.length <= 0 ){
        var emailError = document.getElementById('emailError');
        emailError.innerHTML = 'Required';
        retVal = false;
    }else{ // There was an email, so validate it.
        if(check_email(emailElm.value) == false ){
            var emailError = document.getElementById('emailError');
            emailError.innerHTML = 'Invalid email';
            retVal = false;
        }
   }
    
    // Check the password and conf password values match.
    if( passwordElm.value != confElm.value ){
        var passwordError = document.getElementById('passwordError');
        passwordError.innerHTML = 'Passwords do not match.';
        retVal = false;
    }else if( passwordElm.value.length < 5 ){ // Make sure the password is 5+ chars
        var passwordError = document.getElementById('passwordError');
        passwordError.innerHTML = 'Too short ( 5 char min )';
        retVal = false;
    }    
    return retVal;
}

// Email Validation. Written by PerlScriptsJavaScripts.com
// http://www.perlscriptsjavascripts.com/js/check_email.html
function check_email(e) {
    var ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
    var retValue = true
    var re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
    var re_two =  /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

    for(i=0; i < e.length ;i++){
        if(ok.indexOf(e.charAt(i))<0){ 
            return false;
        }
    }

    if (!(e.match(re)) && (e.match(re_two))) {
        return true;		
    }else{
        return false;
    }

}

     var formURLElm;
     var formIdElm;
     function moveValues(linkId){
	  
	  var nameElm = document.getElementById('name_' + linkId);
	  var urlElm = document.getElementById('url_' + linkId);
	  var formNameElm = document.getElementById('feedName');
	  formURLElm = document.getElementById('feedURL');
	  formIdElm = document.getElementById('feedId');

	  
	  formNameElm.value = nameElm.innerHTML;
	  formURLElm.value = urlElm.innerHTML;
	  formURLElm.size = formURLElm.value.length + 10;
	  formNameElm.size = formNameElm.value.length + 10;
	  formIdElm.value = linkId;


	  document.getElementById('titleHeader').scrollIntoView(false);
     }

     function showId(){
	  var formIdElm = document.getElementById('urlId');
     }

     function submitUpdateForm(linkId){
	  var theForm = document.getElementById('updateForm');
	  formURLElm = document.getElementById('feedURL');
	  formIdElm = document.getElementById('urlId');
	  if(( formURLElm.value.length > 0 ) && (formURLElm.value.length > 0 )){
	       theForm.submit();
	  }else{
	       var errSpan = document.getElementById('errSpan');
	       errSpan.innerHTML = 'Can\'t submit an empty form.';
	  }
          var cb = document.getElementById('makePublic');
	  if( cb.checked == true ){
	       cb.value = 1;
	  }else{
	       cb.value = 0;
	  }
     }