function isValidNumber(number) {
	var numbers = "0123456789";

	for(var i=0; i<number.length; i++) {
		if(numbers.indexOf(numbers.substring(i,i+1))==-1) {
			return false;
		}
	}

	return true;
}

function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    }
	
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

function resetFilter( apply ) {
	if  (document.adminForm.filter_assign) {
		document.adminForm.filter_assign.value = "M";
	}
	if  (document.adminForm.id_client) {
		document.adminForm.id_client.value = "0";
	}
	if  (document.adminForm.id_status) {
		document.adminForm.id_status.value = "WIP";
	}
	if  (document.adminForm.filter_status) {
		document.adminForm.filter_status.value = "WIP";
	}
	if  (document.adminForm.id_user) {
		document.adminForm.id_user.value = "0";
	}
	if  (document.adminForm.filter_tickets) {
		document.adminForm.filter_tickets.value = "0";
	}
	if  (document.adminForm.filter_category) {
		document.adminForm.filter_category.value = "";
	}
	if  (document.adminForm.filter_search) {
		document.adminForm.filter_search.value = "";
	}
	if  (document.adminForm.searchuser) {
		document.adminForm.searchuser.value = "";
	}
	// added JP 8.1.2009	
	if  (document.adminForm.filter_workgroup) {
		document.adminForm.filter_workgroup.value = "0";
	}
	if  (document.adminForm.filter_client) {
		document.adminForm.filter_client.value = "0";
	}
	if  (document.adminForm.filter_user) {
		document.adminForm.filter_user.value = "0";
	}
	// added JP 12.1.2009	
	if  (document.adminForm.ac_me) {
		document.adminForm.ac_me.value = "";
	}

	//if (apply == 1) {
	//	document.adminForm.submit();
	//}
}

function onButton_Ticket_replyapply( noreplysummary, labournegative ) {
	var form = document.adminForm;
	if (form.replytime)
	{
		var varreplytime = form.replytime.value;
	}


	if (form.reply_summary && form.replytime && form.replytime.value != "0:00" && form.replytime.value != "0" && form.reply_summary.value == ""){
			alert( noreplysummary );
			return false;
	} else if ( form.replytime && form.replytime.value != "0" && form.replytime.value != "0:00" && (varreplytime.indexOf('-') >= 0)) {
			alert( labournegative );
			return false;
	} else {
		return true;
	}
}

function onButton_Ticket_reply( noreplysummary, labournegative ) {
var form = document.adminForm;
var varreplytime = form.replytime.value;

	if (form.reply_summary && form.replytime && form.replytime.value != "0:00" && form.reply_summary.value == ""){
			alert( noreplysummary );
			return false;
	} else if ( form.replytime && form.replytime.value != "0" && form.replytime.value != "0:00" && (varreplytime.indexOf('-') >= 0)) {
			alert( labournegative );
			return false;
	} else {
		return true;
	}
}

function saveorder_SC( n, action ) {
	checkAll_button_SC( n, action );
}

//needed by saveorder function
function checkAll_button_SC( n, action ) {
	for ( var j = 0; j <= n; j++ ) {
		box = eval( "document.adminForm.cb" + j );
		if ( box ) {
			if ( box.checked == false ) {
				box.checked = true;
			}
		} else {
			alert("You cannot change the order of items, as an item in the list is `Checked Out`");
			return;
		}
	}
	submitform( action );
}

function passwordStrength(password,username)
{
    score = 0 
    
	//password < 4
    if (password.length < 4 ) { return shortPass }
    
    //password == username
    if (password.toLowerCase()==username.toLowerCase()) return badPass
    
    //password length
    score += password.length * 4
    score += ( checkRepetition(1,password).length - password.length ) * 1
    score += ( checkRepetition(2,password).length - password.length ) * 1
    score += ( checkRepetition(3,password).length - password.length ) * 1
    score += ( checkRepetition(4,password).length - password.length ) * 1

    //password has 3 numbers
    if (password.match(/(.*[0-9].*[0-9].*[0-9])/))  score += 5 
    
    //password has 2 sybols
    if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5 
    
    //password has Upper and Lower chars
    if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  score += 10 
    
    //password has number and chars
    if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))  score += 15 
    //
    //password has number and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/))  score += 15 
    
    //password has char and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/))  score += 15 
    
    //password is just a nubers or chars
    if (password.match(/^\w+$/) || password.match(/^\d+$/) )  score -= 10 
    
    //verifing 0 < score < 100
    if ( score < 0 )  score = 0 
    if ( score > 1000 )  score = 1000 
    
    if (score < 34 )  return badPass 
    if (score < 68 )  return goodPass
    return strongPass
}


// checkRepetition(1,'aaaaaaabcbc')   = 'abcbc'
// checkRepetition(2,'aaaaaaabcbc')   = 'aabc'
// checkRepetition(2,'aaaaaaabcdbcd') = 'aabcd'
function checkRepetition(pLen,str) {
    res = ""
    for ( i=0; i<str.length ; i++ ) {
        repeated=true
        for (j=0;j < pLen && (j+i+pLen) < str.length;j++)
            repeated=repeated && (str.charAt(j+i)==str.charAt(j+i+pLen))
        if (j<pLen) repeated=false
        if (repeated) {
            i+=pLen-1
            repeated=false
        }
        else {
            res+=str.charAt(i)
        }
    }
    return res
}